






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Exam; Professor: Dorf; Class: Elem Prog Concepts; Subject: Electrical Engineering And Computer Science; University: University of Michigan - Ann Arbor; Term: Fall 2011;
Typology: Exams
1 / 10
This page cannot be seen from the preview
Don't miss anything!







Part 1 (80 points)
Closed Book Closed Notes Closed Electronic Devices Closed Neighbor
Turn off Your Cell Phones
Multiple Choice Questions
I have neither given nor received aid on this examination, nor have I concealed any violations of the Honor Code.
int i=0, x;
do { i++; cin >> x; } while (x >= 0);
cout << i << endl;
If the user types 10 20 -30 40 and presses enter at the standard input, what does the above code fragment print? a) 0 b) 1 c) 2 d) 3 e) 4
int i=0, x;
cin >> x; while (x >= 0) { i++; cin >> x; }
cout << i << endl;
If the user types 10 20 -30 40 and presses enter at the standard input, what does the above code fragment print? a) 0 b) 1 c) 2 d) 3 e) 4
int i; for (i = 16; i == 3; i = i / 3) { // do nothing in the loop body } cout << i << endl;
What does the above code fragment print? a) 16 b) 3 c) 1 d) the evaluation goes into an infinite loop e) none of the above
int i; for (i = 16; i != 3; i = i / 3) { // do nothing in the loop body } cout << i << endl;
What does the above code fragment print? a) 16 b) 3 c) 1 d) the evaluation goes into an infinite loop e) none of the above
for (int i = 0; i < 3; i++) { for (int j = 3; j > 0; j --) { cout << i + j << " "; } }
What does the above code fragment print? a) 3 4 5 2 3 4 1 2 3 b) 3 2 1 4 3 2 5 4 3 c) 3 2 1 0 4 3 2 1 5 4 3 2 d) 3 2 1 0 4 3 2 1 5 4 3 2 6 5 4 3 e) none of the above
int a[7] = {1,1,1,1,1,1,1};
for (int i = 2; i < 7; i++) a[i] = a[i-1] + a[i-2];
for (int i = 0; i < 7; i++) cout << a[i] << " ";
What does the above code fragment print? a) 1 1 1 1 1 1 1 b) 1 1 2 2 2 2 2 c) 1 1 2 3 5 8 13 d) the result is unpredictable because the above code makes an out of bounds array access e) none of the above
10.Which of the following C++ expressions evaluates to true if and only if x is a character between ' 0 ' and ' 9 ' inclusive? a) 0 <= x <= 9 b) '0' <= x <= '9' c) 0 <= x && x <= 9 d) '0' <= x && x <= '9'
13.Consider the following program:
#include
void triple(int a0, int a1, int a2, int a3);
int main() { int a0 = 0; int a1 = 1; int a2 = 2; int a3 = 3;
triple(a0,a1,a2,a3);
cout << a0 << a1 << a2 << a3;
return 0; }
void triple(int a0, int a1, int a2, int a3) { a0 = 3 * a0; a1 = 3 * a1; a2 = 3 * a2; a3 = 3 * a3; }
What does the above program print? a) 0000 b) 0123 c) 0369 d) none of the above
14.Consider the following program:
#include
void triple(int& a0, int& a1, int& a2, int& a3);
int main() { int a0 = 0; int a1 = 1; int a2 = 2; int a3 = 3;
triple(a0,a1,a2,a3);
cout << a0 << a1 << a2 << a3;
return 0; }
void triple(int& a0, int& a1, int& a2, int& a3) { a0 = 3 * a0; a1 = 3 * a1; a2 = 3 * a2; a3 = 3 * a3; }
What does the above program print? a) 0000 b) 0123 c) 0369 d) none of the above
18.Consider the following code fragment:
ifstream f; f.open("file.txt");
int x=5, y=5, z=5; f >> x >> y >> z;
cout << z;
What does the above code fragment print, if file.txt does not exist? a) 5 b) the program terminates without printing anything c) the program terminates with an error message indicating that the file does not exist
19.Consider the following C++ function prototype along with its specifications:
// Requires: n >= 0 // Effects: Returns the square root of n
double squareRoot(double n);
Which of the following is an invalid function call? a) double x = squareRoot(0); b) double x = squareRoot(1); c) double x = squareRoot(-1); d) none of the above
20.Consider the following C++ function:
int square(int& n) { n = n * n; return n; }
If a variable x of type int is initialized to 2 , what does the C++ expression square(x) + square(x) evaluate to? a) 8 b) 20 c) none of the above