Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

12 Questions on Data Structures - Quiz 3 | COSC 350, Exams of Data Structures and Algorithms

Material Type: Exam; Professor: Sloan; Class: Data Structures; Subject: Computer Science; University: Wofford College; Term: Spring 2010;

Typology: Exams

2009/2010

Uploaded on 12/08/2010

regis-36
regis-36 🇺🇸

4 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS350 1 of 5 December 4, 2009 !
!
COSC 350 Fall 2009
Quiz 3: Chapters 8, 9, 10 & 11 on C/C++
Be sure to answer each question carefully and completely.
1. (1 pts) Enter your name: ____________________________________________
2. (6!pts.)!Give!prototypes!(and!just!prototypes)!for!the!following!functions:!
!
a) A!function!named!compare()!that!takes!two!ints,!first!and!second,!as!
arguments!and!returns!true!or!false.!
!
!
!
b) A!procedure!named!bmiRead()!that!prompts!the!user!and!reads!two!ints,!
height!and!weight,!and!then!passes!both!back!to!the!calling!program.!
!
!
!
!
3. (12!pts.)!!What!will!be!printed!by!the!following!code?!
!
int main ( void )
{
int *a, *b, x, y;
x = 1;
y = 2;
a = &x;
b = &y;
*a = 3;
*b = 4;
cout << "Part a: " << x << y << endl;
a = b;
*b = 5;
cout << "Part b: " << x << y << endl;
a = new int;
*a = 3;
a = b;
cout << "Part c: " << *a << *b << endl;
delete a;
}
4. (6!pts.)!Does!the!code!in!the!preceding!problem!have!a!memory!leak?!!Explain!
briefly.!
!
!
pf3
pf4
pf5

Partial preview of the text

Download 12 Questions on Data Structures - Quiz 3 | COSC 350 and more Exams Data Structures and Algorithms in PDF only on Docsity!

CS350 1 of 5 December 4, 2009

COSC 350 Fall 2009

Quiz 3 : Chapters 8, 9, 10 & 11 on C/C++

Be sure to answer each question carefully and completely.

1. (1 pts) Enter your name: ____________________________________________

2. ( 6 pts.) Give prototypes (and just prototypes) for the following functions:

a) A function named compare() that takes two ints , first and second, as

arguments and returns true or false.

b) A procedure named bmiRead() that prompts the user and reads two ints ,

height and weight, and then passes both back to the calling program.

3. ( 12 pts.) What will be printed by the following code?

int main ( void ) { int *a, *b, x, y; x = 1; y = 2; a = &x; b = &y; *a = 3; *b = 4; cout << "Part a: " << x << y << endl; a = b; *b = 5; cout << "Part b: " << x << y << endl; a = new int; *a = 3; a = b; cout << "Part c: " << *a << *b << endl; delete a; }

4. (6 pts.) Does the code in the preceding problem have a memory leak? Explain

briefly.

CS350 2 of 5 October 5, 2009

5. (10 pts.) In the space to the right of the code, trace through the execution of the

code. Identify each time a constructor , copy constructor , or destructor is called.

Include each of the printed messages in your trace as well. Give your answer in

temporal order, not as annotations to the code.

#include "Widget.h" // include class definitions for widgets void foo(Widget &a) { cout << "Foo called" << endl; } void bar(Widget &b) { cout << "Bar called" << endl; } int main ( void ) { Widget c; Widget d(c); cout << "Main going strong" << endl; foo(c); bar(d); cout << "Main calling it quits" << endl; return 0; }

6. (12 pts.) Physicians will often test mental agility by asking patients to count

down from one number by some other number. A patient might be asked to

count down from 1 0 0 by 7. To test the mental agility of your computer, write a

loop that will count down from 1 0 0 by 7 using

a) a while loop

b) a do while loop

c) a for loop

CS350 4 of 5 October 5, 2009

Use this code to answer the next two questions.

#include <iostream.h> class BallotBox { public: BallotBox(void); // initilizer void CastVote(int); // record votes void DisplayResults(void); // display election returns private: int democrat; int republican; }; BallotBox::BallotBox(void) { democrat = 0; republican = 0; } void BallotBox::CastVote(int vote) { if (vote) democrat++; else republican++; } void BallotBox::DisplayResults(void) { cout << "Democrats: " << democrat << endl; cout << "Republicans: " << republican << endl; } int main ( void ) { BallotBox election; election.CastVote(1); election.CastVote(1); election.CastVote(0); election.DisplayResults(); return 0; }

9. ( 3 pts.) What will be printed by the code?

10. ( 9 pts) Write code for a new method stuffBox() that will add 1000 additional

votes to the party of your choice and show how the code will be called.

CS350 5 of 5 December 4, 2009

11. (8 pts.) Give a (header‐file type) declaration for ListNode, a List node class to

be used to create nodes for a linked‐list in C++. (That is, what would you put in

the header file, excluding compiler directives, for such a class? You may assume

that any other class using this class will access variables directly.)

12. (10 pts.) Assuming you have a partial implementation of a class LList, a

Linked‐List class that uses ListNode (define above). Give code for a LList

method that inserts a new node into the list when given the index of the node to

insert before. E.g., lst.insert(data, 3) would add a new node before the

third node in list lst with data as its contents. Identify any assumptions you

must make about the LList class.

Pledged: ______________________________________________________________________________________