CSC1322 Adv Programming in C++ Professor: Zhang Review Summer 2014 Name: Score: Question Points Score 1 5 2 5 3 5 4 5 5 5 6 5 7 5 8 5 9 5 10 5 11 5 12 5 13 5 14 5 15 5 16 5 17 5 18 10 19 10 20 10 21 15 22 20 Total: 150
1. (5 points) True or False? Every C++ program must have a function named main. 2. (5 points) True or False? If a program compiles successfully, it is guaranteed to execute correctly. 3. (5 points) Which one of the following is not a valid identifier in C++? A. Hi There B. top40 C. UpAnDdOwN D. 4BlindMice E. CAPS 4. (5 points) True or False? The C++ compiler considers the identifier gradepointaverage to be the same as the identifier gradepointaverage. 5. (5 points) Given the following program string st; char ch; int a; cin >> st >> a; cin >> ch; cout << st << ch << a << endl; what is printed with the following as input data to the program? csc 1321 - A. 1321-csc B. csc1321- C. csc-1321 D. -csc1321 E. none of the above Page 2 of 7
6. (5 points) Given the two lines of input data The letters are: ABC what value is read into ch2 by the following code? (variable st is of string and variables ch1 and ch2 are of type char.) getline(cin, st); cin.get(ch1); cin.get(ch2); A. A B. B C. C D. h E. none of the above 7. (5 points) After execution of the following code, what will be the value of angle if the input value is 15? cin >> angle; if (angle > 5) angle = angle + 5; if (angle >= 10) angle = angle + 10; A. 5 B. 10 C. 15 D. 25 E. 30 8. (5 points) This question is about short-circuit evaluation of logical expressions. Consider the following expression in some imaginary programming language (not C++): int n; int k; (n > 5) && (k / n < 20) If n equals 0 when this expression is evaluated, which of the following statements about the expression is true? A. It causes a divide-by-zero error only if the language uses short-circuit evaluation. Page 3 of 7
B. It causes a divide-by-zero error whether or not the language uses short-circuit evaluation. C. It causes a divide-by-zero error only if the language does not use short-circuit evaluation. D. It never causes a divide-by-zero error. 9. (5 points) True or False? The expression n >= 8 is equivalent to the expression!(n < 8). 10. (5 points) What is the termination condition for the following while loop? while (beta > 0 && beta < 10) cout << beta << endl; cin >> beta; A. beta < 0 beta > 10 B. beta > 0 && beta < 10 C. beta <= 0 beta >= 10 D. beta >= 0 && beta <= 10 11. (5 points) Given the following code fragment, cout << A ; for (loopcount = 0; loopcount < 2; loopcount++) cout << B ; cout << C ; The result will be: A. the output AC B. the output ABC C. the output ABBC D. the output ABBBC E. an infinite loop 12. (5 points) After execution of the code fragment Page 4 of 7
char ch = B ; int alpha = 3; switch (ch) case A : alpha = alpha + 10; break; case B : alpha = alpha + 20; case C : alpha = alpha + 30; what is the value of alpha? 13. (5 points) What is the output of the following code fragment if the input value is G? cin >> inputchar; switch (inputchar) case A : cout << 4; break; case G : case Q : cout << 3; case M : cout << 2; break; default : cout << 1; 14. (5 points) Write a for loop that is equivalent to the following while loop. (All variables are of type int.) int count = 0; int sum = 0; while (count < 100) sum = sum + count; count = count + 2; 15. (5 points) What is the output of the following code fragment? (All variables are of type int.) n = 1; for (count = 0; count < 2; count++) while (n < 10) n = 2 * n; cout << n << endl; Page 5 of 7
16. (5 points) What is the output of the following code fragment? loopcount = 1; cout << G ; while (loopcount < 5) if ( loopcount == 4 ) cout << d ; else cout << o ; loopcount++; The result will be: A. the output God B. the output Good C. the output Goood D. the output Gooood E. an infinite loop 17. (5 points) Given the function prototype bool IsGreater( int, int ); which of the following statements use valid calls to the IsGreater function? (The data types of the variables are suggested by their names.) A. someboolean = IsGreater(someInt); B. if (IsGreater(5, someint)) intcounter++; C. while (IsGreater(inputInt, 23)) cin >> inputint; D. b and c above E. a, b, and c above 18. (10 points) If the int variables i, j, and k contain the values 10, 3, and 20, respectively, what is the value of the following logical expression: i < 15 j == 3 && i > k A. false B. true Page 6 of 7
19. (10 points) Given the function definition int Trans(int alpha, int beta ) if (alpha > beta) return alpha + 10; else return 2 * beta; what is printed by the following code? cout << Trans(10, Trans(2, 4)) << endl; 20. (10 points) Are the following two logical expressions equivalent? Hint: Use DeMorgan s law or try some values for beta to verify. a)!(beta < 5 && beta > 10) b)!(beta < 5)!(beta > 10) 21. (15 points) What is the output of the following program? Hint: consider the difference between passing-by-value and passing-by-reference. void foo(int a, int & b) a = 2 * a; b = 3 * b; int main() int a = 5; int b = 10; foo(a, b); cout << a << " " << b << endl; return 0; 22. (20 points) Write a program that finds the sum of the first 100 terms of a series given below. (Hint: C++ power function for x y is pow(x, y) and consider rewriting the series as S = 1 2 1 + 1 2 2 + 1 2 3 +... + 1 2 100 ) S = 1 2 + 1 4 + 1 8 +... Page 7 of 7