A "*" is given next to the correct answer. 1. In C/C++, what is the value of the arithmetic expression 9/8? A. 0 *B. 1 C. 1.125 D. 3 E. none of A-D 2. In C/C++, what is the value of the arithmetic expression 2 % 3? Notice that this question uses the "modulus operator," written with a percent sign. A. 0 B. 3 C. 0.666 *D. 2 E. none of A-D 3. In C/C++, what is the value of the arithmetic expression 3 + 4 / 2 * 7? A. 3.2857 *B. 17 C. 21 D. 35 E. none of A-D 4. Which of the following does not belong (assume i is type int and has a value): A. printf("%d\n", i); B. cout << i << endl; C. cout << i << "\n"; *D. printf("%d", i); E. All are equivalent 5. Suppose that x is an int variable that contains the value 2, and y is an int variable that contains the value 5. What is the new value of x after this C/C++ statement: x += 2 * y; A. 1 B. 10 *C. 12 D. 21 E. none of A-D 7. What will be printed by the following C/C++ code: if (4 < 1) cout << "I like "; else if ((5 >= 2) || (1 > 7)) cout << "computer science "; else cout << "sucks."; *A. computer science B. I like computer science C. computer science sucks D. sucks 8. What will be printed by the following C/C++ code: if ((4 < 1) || (4 < 6) && (x < 3)) cout << "Study more"; else cout << "Play more"; A. Study more B. Play more C. Study more Play more *D. cannot be determined from the information given 9. For the following code segment, what will be printed? { int i=9; { int j=4; } cout << i << j; } A. 9 4 B. 4 9 C. 9 *D. the code segment won't compile 10. What will be printed by the following C++ code: int x = 20; int y = 14; if (x < 20) cout << "Make it "; else if (y < 20) cout << "so "; else cout << "Engage!"; A. Make it B. Make it so C. Make it so Engage! *D. so E. none of A-D 11. Here is a Boolean expression: (x < y || x > y) What is a simpler way to write an equivalent Boolean expression? A. y >= x *B. x != y C. x >= y D. x < y E. none of A-D 12. What will the following code fragment print? char letter='a'+1; cout << letter; A. 97 B. a C. letter *D. b E. Not enough information to determine the output 13. Here is a small piece of C++ code containing a while-loop: int i = 0; while (i < 999) { cout << i << endl; i++; } How many lines of output will this code produce? A. 0 *B. 999 C. 998 D. 1000 E. none of A-D 14. Here is a small piece of C++ code containing a for-loop: int i; for (i = 42; i <= 42; i++) cout << i << endl; How many lines of output will this code produce? A. 0 *B. 1 C. 2 D. 42 E. none of A-D 15. Here is a small piece of C++ code containing two for-loops: int i, j; for (i = 0; i < 6; i++) { cout << "Hi" << endl; } for (j = 1; j < 4; j++) { cout << "Hi" << endl; } How many lines of output will this code produce? A. 4 B. 24 *C. 9 D. 6 E. none of A-D 16. Here is a small piece of C++ code containing two for-loops: int i, j; for (i = 0; i < 6; i++) { for (j = 0; j < 5; j++) { cout << "Hi" << endl; } } How many lines of output will this code produce? A. 11 B. 42 C. 12 *D. 30 E. none of A-D 17. Here is a small piece of C++ code containing a while loop: float x = 100.0; while (x > 25.0) { x = 0.5 * x; cout << "Hi" << endl; } How many lines of output will this code produce? A. 0 B. 1 *C. 2 D. 3 E. none of A-D 19. In C/C++, what is the value of the arithmetic expression (3/4)*4? *A. 0 B. 0.5 C. 0.75 D. 3 E. none of A-D 20. Consider this code fragment: int i, n; cin >> n; i = n; while (i > 0) { cout << i << endl; i--; } This fragment is equivalent to which of the following fragments? A. C. int i, n; int i, n; cin >> n; cin >> n; do while (i >= 0; { { cout << i << endl; cout << i << endl; i--; i--; } while (i > 0); } *B. D. int i, n; int i, n; cin >> n; cin >> n; for (i = n; i > 0; i--) for (n = i; i > 0; i--) { { cout << i << endl; cout << i << endl; } } 21. What is the output of these statements? float f = 100/3; cout << f; *A. 33 B. 33.333333 C. 1 D. 100 E. none of A-D 22. What is the output of these statements? float f = 100.0/3.0; cout << f; A. 33 *B. 33.333333 C. 1 D. 100 E. none of A-D 23. What is the output of these statements? float f = float(100)/float(3); cout << f; A. 33 *B. 33.333333 C. 1 D. 100 E. none of A-D 24. What is the output of these statements? float f = int(100)/int(3); cout << f; *A. 33 B. 33.333333 C. 1 D. 100 E. none of A-D 25. What is the output of these statements? (Be careful--this is tricky.) int i = 1; if (i || 0) cout << i; else cout << "hello"; A. 0 *B. 1 C. 2 D. "hello" E. none of A-D 26. What is the output of these statements? (Be careful--this is tricky.) int i = 0; if (i && 1) cout << i; else cout << "hello"; A. 0 B. 1 C. 2 *D. "hello" E. none of A-D 28. The prototype "double munge(int x, char y)" describes a function that: A. returns a value of type char B. returns a value of type int *C. returns a value of type double D. returns multiple values E. none of the above 29. What will happen when you compile the following code? (Assume that the necessary .h files have been included.) int main () { int a, b, c; a = 4; b = 7; cout << c = a + b << endl; return a; } A. code outputs 4 B. code outputs 7 C. code outputs c=a+b *D. code outputs 11 E. code will not compile 30. What will happen when you compile the following code? (Assume that the necessary .h files have been included.) int main () { #define PI 3.14159 PI /= 2.0; cout << PI << endl; } A. code oututs 3 B. code outputs 1 C. code outputs 3.14159 D. code outputs 1.570795 *E. code will not compile 31. Which of the following are not valid variable names? A. loop_index B. LoopIndex C. loopIndex2 *D. loop-index E. two or more of A-D 32. Which of the following is guaranteed to set the value of z to 7? *A. do { z = 7; } while (x < y); B. while (x < y) { z = 7; } C. for (i = 0; i < x; ++i) z = 7; D. for (i = 7; i < x; ++i) z = i; E. for (i = 7; i < 7; ++i) z = i; 33. The expression "sqrt(sqrt(100))" returns the value A. 100 B. 10 C. 1 D. the expression does not have a value and will cause an execution error *E. none of the above 34. Consider the following code: int i; double z; z = 1.49; i = int(z*100); How should we modify the last line if we wish to ensure that no rounding error occurs (i.e., variable i is set to 149)? A. i = int(z)*100; B. i = int(z)*100+.5; C. i = int(z)*100-.5; *D. i = int(z*100+.5); E. i = int(z*(100+.5)); 35. To exhange the values of variables x and y, which of the following will work (assume that x, y, s, and t are all of the same type)? A. x=s; B. s=x; C. x=t; D. x=y; *E. t=y; y=t; y=x; y=x; s=x; y=x; s=y; x=s; t=x; y=s; x=t; t=x; 36. What will the following code output? if (7 - 1 < 2 / 3) cout << "enjoying "; cout << "the exam "; cout << "class?"; A. enjoying class? B. enjoying the exam C. enjoying the exam class? D. class? *E. the exam class? 37. What will the following code output? int x, y; x = y = 4; ++x; --y; y = y * 2; cout << y - x << endl; A. 0 B. 6 C. 4 D. 5 *E. none of the above 38. What will "ff(4)-ff(5)" evaluate to, given the following function definition: int ff (int x) { return x * x - 1; } *A. -9 B. -1 C. 15 D. 24 E. 9 39. Given the same definition of ff, what will "ff(ff(2))" evaluate to? A. -1 B. 2 C. 3 *D. 8 E. 9 40. if the function max(x,y) returns the larger of x and y, and the function min(x,y) returns the smaller of x and y, what will "max(min(3,1),min(-1,2))" return? A. -1 B. 0 *C. 1 D. 2 E. 3 41. What is the output of the following code ? const int X_SIZE = 600; const int Y_SIZE = 450; void step(int &ant_x, int &ant_y, int dir) { switch (dir) { case 0: ant_x = (ant_x + 1) % X_SIZE; break; case 1: ant_y = (ant_y + 1) % Y_SIZE; break; case 2: ant_x -= 1; if (ant_x < 0) ant_x += X_SIZE; break; case 3: ant_y -= 1; if (ant_y < 0) ant_y += Y_SIZE; break; default: printf("%d is an undefined direction.\n", dir); exit (EXIT_FAILURE); } } int main() { int x = 100, y = 200, dir = 0; step(x, y, dir); step(x, y, dir++); printf("%d %d", x, y); return 0; } A. 100 200 B. 101 200 C. 101 201 D. 100 201 *E. None of the above 42. What is the output of the following code? char c = 'b'; switch(c) { case 'a': cout << "0"; case 'b': cout << "1"; case 'c': cout << "2"; default: cout << "3"; } A. 01 B. 1 *C. 123 D. 12 E. None of the above