Instructions.   Read each problem carefully before answering. Circle the selected answer(s) or write your solutions in ink in the spaces provided when this is the appropriate action as indicated below for each problem. Write neatly. Each problem is worth 60 points. All parts of each problem have equal point value. C++ is the programming language used throughout this test. Good luck!
Arrows are used instead of circles here for typesetting reasons... -->Modification member functions Vector indices Loop tests Constant member functions <-- Private data members Constructors <-- Homogeneous functions Destructors
b) A class named VolumeControl has exactly six public member functions, named VolumeControl(), TurnUpANotch(), TurnDownANotch(), TurnOff(), GetVolume(), and IsOn(). Assume that p, q, and r are three objects of type VolumeControl. Circle all statements among those shown below that would be syntactically incorrect if used within a client program of the VolumeControl class. You may assume that the source file that contains these statements provides access to the VolumeControl class through the appropriate #include "volume_control.h" directive.
-->q*TurnUpANotch(); TurnDownANotch = p; <-- p.TurnOff(); VolumeControl z; -->p.q; z = VolumeControl; <--
Declare a vector named thisvector that contains 10 int objects. vector<int> thisvector(10); Place the size of a vector named thisvector in an object named thisroll (no need to declare thisvector). int thisroll = thisvector.size(); Assign the value 5 to the item in position 4 of the vector named thisvector (no need to declare thisvector). thisvector[4] = 5;
b) Assume that the size() method (member function) of the vector class has the following prototype:
int size() const; // returns the number of items in the current vector objectAnswer each of the following:
Is the size() method a constructor, a modifier, or a selector? Answer: selector (because of the const keyword) Give the header only (no function body required) of the size method exactly as it would appear in the implementation file vector.cpp: int vector::size() const
a)
vector<double> row(5); if (row.size()!=1) { cout << "kerplunk"; } else { cout << "yahoo"; } cout << " " << row.size(); Answer: kerplunk 5
b)
vector<int> thisvector(4); for (int i=0; i<thisvector.size(); i++) { thisvector[i] = i*i; } for (int i=0; i<thisvector.size(); i++) { cout << thisvector[3-i]; } Answer: 9410
a) A struct is a class that has
Only constant members At least two modifier methods -->Only public members
b) Vectors satisfy all of the following except which one?
-->The time necessary to access an item of a vector depends on its position Vectors are homogeneous aggregates Vector items are indexed by integers
c) The correct declaration for a 6-sided Dice object named box is:
Dice(6) box; -->Dice box(6); Dice box.(6);
int UFO(vector<int> & v) { int pos = 0; while ( (pos < v.size()) && (v[pos] != 0) ) { v[pos] = -1; pos++; } return pos; }Assume that myvector is a vector containing 10 ints, with myvector[i] equal to 10-i for each i between 0 and 9.
a) Give the value returned by the call
UFO(myvector); answer: 10
b) What output is produced by the following program segment?
for (int i=0; i<10; i++) { myvector[i] = 10 - i; } int count = UFO(myvector); cout << count << endl; for (int i=0; i<10; i++) { cout << myvector[i] << " "; } Answer: 10 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1