CSCI 1380 Chapter 5
a
The control statements in the for loop include the initial statement, loop condition, and update statement. a. True b. False
b
The control variable in a flag-controlled while loop is a bool variable. Selected Answer: Correct True Answers: a. True b. False
c
A loop that continues to execute endlessly is called a(n) ____ loop. Answers: a. definite b. unhinged c. infinite d. end
a
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 << " "; } Answers: a. True b. False
b
Assume that all variables are properly declared. The following for loop executes 20 times. for (i = 0; i <= 20; i++) cout << i; Answers: a. True b. False
b
Consider the following code. int limit; int reps = 0; cin >> limit; while (reps < limit) { cin >> entry; triple = entry * 3; cout << triple; reps++; } cout << endl; This code is an example of a(n) ____ while loop. Answers: a. sentinel-controlled b. counter-controlled c. EOF-controlled d. flag-controlled
c
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. Answers: a. sentinel-controlled b. flag-controlled c. EOF-controlled d. counter-controlled
c
In ____ structures, the computer repeats particular statements a certain number of times depending on some condition(s). Answers: a. sequence b. selection c. looping d. branching
a
In a counter-controlled while loop, the loop control variable must be initialized before the loop. Answers: a. True b. False
b
In a sentinel-controlled while loop, the body of the loop continues to execute until the EOF symbol is read. Answers: a. True b. False
a
In the case of the sentinel-controlled while loop, the first item is read before the while loop is entered. a. True b. False
b
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
a
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; Answers: a. 24 b. 25 c. 41 d. 42
b
The ____ statement can be used to eliminate the use of certain (flag) variables. a. while b. break c. if d. switch
b
The following while loop terminates when j > 20. j = 0; while (j < 20) j++; Answers: a. True b. False
a
The number of iterations of a counter-controlled loop is known in advance. Answers: a. True b. False
b
The statement in the body of a while loop acts as a decision maker. Answers: a. True b. False
a
What is the next Fibonacci number in the following sequence? 1, 1, 2, 3, 5, 8, 13, 21, ... a. 34 b. 43 c. 56 d. 273
c
What is the output of the following C++ code? int j; for (j = 10; j <= 10; j++) cout << j << " "; cout << j << endl; Answers: a. 10 b. 10 10 c. 10 11 d. 11 11
d
What is the output of the following C++ code? count = 1; num = 25; while (count < 25) { num = num - 1; count++; } cout << count << " " << num << endl; Answers: a. 24 0 b. 24 1 c. 25 0 d. 25 1
d
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
d
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.
d
What is the value of x after the following statements execute? int x = 5; int y = 30; do x = x * 2; while (x < y); Answers: a. 5 b. 10 c. 20 d. 40
a
When a continue statement is executed in a ____, the update statement always executes. Answers: a. for loop b. while loop c. switch structure d. do...while loop
c
Which executes first in a do...while loop? Answers: a. update statement b. the expression c. the statement d. loop condition
b
Which of the following is a repetition structure in C++? Answers: a. while...do b. do...while c. if d. switch
b
Which of the following loops does not have an entry condition? Answers: a. for loop b. do...while loop c. EOF-controlled while loop d. sentinel-controlled while loop
d
Which of the following loops is guaranteed to execute at least once? a. counter-controlled while loop b. sentinel-controlled while loop c. for loop d. do...while loop
d
Which of the following statements generates a random number between 0 and 50? Answers: a. srand(time(10)); num = rand() % 50; b. srand(time(10)); num = rand()/50; c. srand(time(0)); num = rand()50; d. srand(time(0)); num = rand() % 50;
b
____ loops are called posttest loops. Answers: a. break b. do...while c. while d. for