Docsity

Quiz 4 for CGS 3464 - Spring 2005, Quizzes of Computer Science

Toggle sidebar
bg1
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
pf2

Partial preview of the text

Download Quiz 4 for CGS 3464 - Spring 2005 and more Quizzes Computer Science in PDF only on Docsity!

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 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/ c. 2/ d. Undefined

Page 2 of 2 Name: __________________________ CourseTool ID: __________________________

  1. Given the following non-member operators for the Date class: const Date operator + (Date d1, int days); // Add that many days to the Date. // Ex. 2/1 + 3 = 2/4, 2/27 + 4 = 3/3 and 1/1 + 45 = 2/ const Date operator + (Date d1, Date d2); // Add the two dates and test for valid date // Ex. 2/1 + 3/1 = 4/2, 2/24 + 3/1 = 5/25, // 1/27 + 1/4 = Invalid date since feb cannot have 31 days What is the output of the code snippet: Date d1(2/23); d1 = d1 + 4; d1.Output(); a. 2/ b. Compiler error c. Run-time error d. Invalid date