
Page 1 of 2
Name: __________________________ CourseTool ID: __________________________
CGS 3464 โ Spring 2005
Quiz 4
1. The following code snippet intends to read 5 integers into a vector and then print them out.
However, this code has a bug. What is it?
vector<double> v;
int i, val;
// Input values
for (i=0; i<5; i++) {
cin >> val;
v.push_back(val);
}
// Output values
for (i=0; i<v.capacity(); i++) {
cout << v[i] << endl;
}
a. The vector must be initialized to hold the 5 elements
b. To store items in the vector we should use: v[i] = val; instead of
push_back()
c. The loop to output the values should loop to v.size() instead of v.capacity().
d. We cannot store integer values in a vector of type double.
2. Assume that the class Number is defined and that it has a constructor that takes in one
integer parameter. Which of the following prototypes is invalid?
a. int operator %(Number x, Number y);
b. int operator %(Number x, int y);
c. int operator %(const Number &x, const Number &y);
d. int operator %(int x, int y);
3. You are given the definition of the class Date. What is the output of the following
code snippet:
Date d1;
d1 = 5 โ 3;
d1.Output()
a. Compiler error
b. 1/1
c. 2/1
d. Undefined