C++ CHA5
The function eof is a member of the data type ____________________
istream
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
loop-continue test
. 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
looping
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.`
pretest
A semicolon at the end of the for statement (just before the body of the loop) is a(n) ____________________ error.
semantic
Which of the following statements generates a random number between 0 and 50? a. srand(time(0));num = rand() % 50; b. srand(time(10));num = rand()/50; c. srand(time(0));num = rand()50; d. srand(time(10));num = rand() % 50;
srand(time(0));num = rand() % 50;
Which executes first in a do...while loop? a. the statement b. loop condition c. the expression d. update statement
the statement
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
true
In a counter-controlled while loop, the loop control variable must be initialized before the loop. a. True b. False
true
In the case of the sentinel-controlled while loop, the first item is read before the while loop is entered. a. True b. False
true
The control statements in the for loop include the initial statement, loop condition, and update statement. a. True b. False
true
The control variable in a flag-controlled while loop is a bool variable. a. True b. False
true
The number of iterations of a counter-controlled loop is known in advance. a. True b. False
true
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
10
. 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
10 11
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
110
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
125
. 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
24
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
25 1
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
34
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
40
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
EOF-controlled
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.
This is an infinite loop.
The ____ statement can be used to eliminate the use of certain (flag) variables. a. while b. switch c. break d. if
break
The ____________________ statement is typically used for two purposes:• To exit early from a loop.• To skip the remainder of a switch structure.
break
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. a. flag-controlled b. counter-controlled c. EOF-controlled d. sentinel-controlled
counter-controlled
. A(n) ____-controlled while loop uses a bool variable to control the loop. a. counter b. sentinel c. flag d. EOF
flag
To generate a random number, you can use the function rand of the header file ____________________.
cstdlib
. ____ loops are called posttest loops. a. break b. for c. while d. do...while
do...while
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
do...while loop
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
do...while loop
Which of the following is a repetition structure in C++? a. if b. switch c. while...do d. do...while`
do...while`
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
false
In a sentinel-controlled while loop, the body of the loop continues to execute until the EOF symbol is read. a. True b. False
false
The following while loop terminates when j > 20. j = 0;while (j < 20) j++; a. True b. False
false
The statement in the body of a while loop acts as a decision maker. a. True b. False
false
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
for loop
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";
i = 1;
A loop that continues to execute endlessly is called a(n) ____ loop. a. end b. unhinged c. infinite d. definite
infinite
A loop ____________________ is a set of statements that remains true each time the loop body is executed.
invariant