BTE320 Chapter 5
1. The statement in the body of a while loop acts as a decision maker. a. True b. False
ANSWER: False
10. Assume that all variables are properly declared. The following for loop executes 20 times. for (i = 0; i <= 20; i++) cout << i; a. True b. False
ANSWER: False
2. The following while loop terminates when j > 20. j = 0; while (j < 20) j++; a. True b. False
ANSWER: False
3. The number of iterations of a counter-controlled loop is known in advance. a. True b. False
ANSWER: True
4. Assume all variables are properly declared. The output of the following C++ code is 2 3 4 5. n = 1; while (n < 5) { n++; cout << n << " "; } a. True b. False
ANSWER: True
5. In a counter-controlled while loop, the loop control variable must be initialized before the loop. a. True b. False
ANSWER: True
6. In the case of the sentinel-controlled while loop, the first item is read before the while loop is entered. a. True b. False
ANSWER: True
9. The control statements in the for loop include the initial statement, loop condition, and update statement. a. True b. False
ANSWER: True
11. In ____ structures, the computer repeats particular statements a certain number of times depending on some condition(s). a. looping b. branching c. selection d. sequence
ANSWER: a
21. What is the initial statement in the following for loop? (Assume that all variables are properly declared.) int i; for (i = 1; i < 20; i++) cout << "Hello World"; cout << "!" << endl; a. i = 1; b. i < 20; c. i++; d. cout << "Hello World";
ANSWER: a
23. Suppose sum, num, and j are int variables, and the input is 4 7 12 9 -1. What is the output of the following code? cin >> sum; cin >> num; for (j = 1; j <= 3; j++) { cin >> num; sum = sum + num; } cout << sum << endl; a. 24 b. 25 c. 41 d. 42
ANSWER: a
26. Which executes first in a do...while loop? a. the statement b. loop condition c. the expression d. update statement
ANSWER: a
33. What executes immediately after a continue statement in a while and do-while loop? a. loop-continue test b. update statement c. loop condition d. the body of the loop
ANSWER: a
24. Suppose j, sum, and num are int variables, and the input is 26 34 61 4 -1. What is the output of the code? sum = 0; cin >> num; for (int j = 1; j <= 4; j++) { sum = sum + num; cin >> num; } cout << sum << endl; a. 124 b. 125 c. 126 d. 127
ANSWER: b
34. When a continue statement is executed in a ____, the update statement always executes. a. while loop b. for loop c. switch structure d. do...while loop
ANSWER: b
39. The ____________________ statement is typically used for two purposes: • To exit early from a loop. • To skip the remainder of a switch structure.
ANSWER: break
14. A loop that continues to execute endlessly is called a(n) ____ loop. a. end b. unhinged c. infinite d. definite
ANSWER: c
16. Suppose sum and num are int variables, and the input is 18 25 61 6 -1. What is the output of the following code? sum = 0; cin >> num; while (num != -1) { sum = sum + num; cin >> num; } cout << sum << endl; a. 92 b. 109 c. 110 d. 119
ANSWER: c
19. Consider the following code. (Assume that all variables are properly declared.) cin >> ch; while (cin) { cout << ch; cin >> ch; } This code is an example of a(n) ____ while loop. a. sentinel-controlled b. flag-controlled c. EOF-controlled d. counter-controlled
ANSWER: c
22. What is the output of the following C++ code? int j; for (j = 10; j <= 10; j++) cout << j << " "; cout << j << endl; a. 10 b. 10 10 c. 10 11 d. 11 11
ANSWER: c
30. Which of the following loops is guaranteed to execute at least once? a. counter-controlled while loop b. for loop c. do...while loop d. sentinel-controlled while loop
ANSWER: c
31. Which of the following loops does not have an entry condition? a. EOF-controlled while loop b. sentinel-controlled while loop c. do...while loop d. for loop
ANSWER: c
32. The ____ statement can be used to eliminate the use of certain (flag) variables. a. while b. switch c. break d. if
ANSWER: c
12. What is the output of the following C++ code? count = 1; num = 25; while (count < 25) { num = num - 1; count++; } cout << count << " " << num << endl; a. 24 0 b. 24 1 c. 25 0 d. 25 1
ANSWER: d
13. What is the output of the following C++ code? num = 10; while (num > 10) num = num - 2; cout << num << endl; a. 0 b. 6 c. 8 d. 10
ANSWER: d
25. Which of the following is a repetition structure in C++? a. if b. switch c. while...do d. do...while
ANSWER: d
27. What is the value of x after the following statements execute? int x = 5; int y = 30; do x = x * 2; while (x < y); a. 5 b. 10 c. 20 d. 40
ANSWER: d
28. What is the output of the following loop? count = 5; cout << 'St'; do { cout << 'o'; count--; } while (count <= 5); a. St b. Sto c. Stop d. This is an infinite loop.
ANSWER: d
29. ____ loops are called posttest loops. a. break b. for c. while d. do...while
ANSWER: d
38. In a while and for loop, the loop condition is evaluated before executing the body of the loop. Therefore, while and for loops are called ____________________ loops.
ANSWER: pretest
37. A semicolon at the end of the for statement (just before the body of the loop) is a(n) ____________________ error.
ANSWER: semantic