Computer Science 162 – Spring 2000
04/03/2K Midterm Exam #2 Test Form A
Name:
Instructions:
Answer ALL questions on the answer sheet provided.
Hand in TEST, Answer Sheet, and Help Sheet, each with your name on it.
Remember to put the letter of your test form on your answer sheet.
Completion (3
points each)
1. Strings in C++ can be ________; this means that two different strings can be combined or glued together so that one is before the other. For instance “pets” and “smart” could be combined to form “petssmart”.
2. A(n) ________ is used with many data structures to keep track of where we currently are within a data structure. It shares some aspects of a counter (it can be incremented) and some aspects of pointers (it shows where the object is, not what it is).
Multiple
Choice (4 points each)
3. If you
need to add an element at the end of a data structure, which is the most
efficient data structure to add to?
A)
linked list
B)
array
C)
vector
D)
they are
all the same
4. Which of
the following list library functions change the contents of the list?
A)
front
B)
end
C)
empty
D)
push_back
E)
all of the
above.
F)
None of the
above.
Short Answer
(points as shown)
(6 points)
5. What are two advantages of using vectors instead of arrays?
(8 points)
6. Why is it
that elements can be efficiently added to the ends of lists?
Valid/Invalid (4
points each)
For each,
tell whether valid or invalid. If invalid, tell why!
7.
. // with
string class included
string one =
“real important stuff”;
string
two(one);
8.
vector<int>
curr (6);
curr[7] = 3;
9.
vector<Account> mine(10);
int len =
mine[5].size();
10.
list<Song> songList(15);
// assume
these songs are given value somewhere in between these statements …
songList®pop_back();
Doing and
Understanding: (points as shown)
(8 points)
11. What is
the output of the following code fragment?
string test =
“today is the ncaa mens basketball championship game”;
int unus =
find_first_not_of(test,”aeioubdhmnprst”);
cout <<
“Found “ << test[unus] << “ at “ << unus;
(6 points)
12. Indicate
the output from the following fragment of code
string test =
“rest”;
test.resize(8,’#’);
cout <<
test << “ | “ << test.length();
(12 points)
13. What is the output of the following segment of code? (don’t worry too much about the details of << for accounts - it shows something like: Acct: xxxx has balance: 9999.99
vector<Account> myAccts;
Account one(“1111”,125.00);
myAccts.push_back(one);
Account two(“2222”,100);
myAccts.push_back(two);
Account three(“3333”,75);
myAccts.push_back(three);
Account four (“4444”,50.00);
myAccts.push_back(four);
Account five (“5555”,25);
myAccts.push_back(five);
for (int cnt = 0; cnt < myAccts.size(); cnt++ ) {
myAccts[cnt].Deposit(100);
Cout << myAccts[cnt] << endl;
}
Writing Code
(points as shown)
(5
points)
14.
Write a declaration of an iterator variable called ImHereItr that is an
iterator to a list of Cards.
(5
points)
15.
Write as declaration of a vector of songs that will initially have space for 10
songs (which are initially filled with default songs).
(20
points)
16. Assuming
that a class called Room (information
about classrooms) has been defined as below, write a function called MeanRoom
that, given a linked list of rooms, returns the mean (average) capacity of the
rooms in the list.
The function
would NOT be a member of the class (i.e. it would be in the same file as your
main, and would not be object-oriented). Also, show the call from main,
assuming that a linked list of rooms named roomList has been declared and
filled with values.
class Room {
public:
Room ();
// default Room prototype
Room (string name, int holds); // fully specified Room prototype
string GetName(); // report Room name
double GetSpace(); // report Room
capacity
void SetName(string nam); // set or change Room name
void SetSpace (int holds); // set or change Room sixe
private:
string roomName;
int roomCapacity;
};
bool operator
< (const Room & left, const Room & right);