C++ Chapter Five
What is the output of the following C++ code? int j; for (j = 10; j <= 10; j++) cout << j << " "; cout << j << endl;
10 11 *10 is = 10 so it executes once more*
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; }
125 * j is initialized at 1 and increases till = 4. Therefore the loop only runs 4 times*
What is the output of the following C++ code? count = 1; num = 25; while (count < 25) { num = num - 1; count++; } cout << count << " " << num << endl;
25 1
What is the value of x after the following statements execute? int x = 5; int y = 30; do x = x * 2; while (x < y);
40 5 * 2 = 10 < 30 is true 10 * 2 = 20 < 30 is true 20 * 2 = 40 < 30 is false so loop executes
Suppose sum and num are int variables, and the input is 1 5 6 3 -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. 15 B. 14
A. 15
Which executes first in a do... while loop? A. the statement B. loop condition C. the expression D. update statement
A. The statement
What is the initial statement in the following for loop? 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";
A. i = 1
To generate a random number, you can use the function rand of the header file ____________________. A. fstream B. cstdlib C. cmath D. string
B. cstdlib
____ loops are called posttest loops. A. forloop B. do...while loop C. while loop
B. do...while loop
Suppose j, sum, and num are int variables, and the input is 6 4 1 4 -1. What is the output of the code? sum = 0; cin >> num; for (int j = 1; j <= 3; j++) { sum = sum + num; cin >> num; } cout << sum << endl; A. 10 B. 11 C. 14 D. 15
C. 11
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
C. EOF-controlled
A(n) ____-controlled while loop uses a bool variable to control the loop. A. Counter B. Sentinel C. Flag. D. eof
C. Flag
A loop that continues to execute endlessly is called a(n) ____ loop. A. End B. Unhinged C. Infinite D. definite
C. Infinite
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. 7 D. 10
D. 10 *condition is false so while loop doesn't run and only num is outputted*
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. This is an infinite loop *Always will be decreasing therefore count is always less than 5*
Assume that all variables are properly declared. The following for loop executes 20 times. for (i = 0; i <= 20; i++) cout << i; (T/F)
False * executes 21 times because i has to equal 20*
The statement in the body of a while loop acts as a decision maker. (T/F)
False - The statement in the expression of a while loop
The following for loop executes 5 times. for (i = 0; i <= 5; i++) cout << i; (T/F)
False - executes 6 times
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 << " "; }
True
In a counter-controlled while loop, the loop control variable must be initialized before the loop. (T/F)
True
In the case of the sentinel-controlled while loop, the first item is read before the while loop is entered
True
The control statements in the for loop include the initial statement, loop condition, and update statement.
True
The control variable in a flag-controlled while loop is a bool variable. (T/F)
True
The following while loop terminates when j >= 20. j = 0; while (j < 20) j++; (T/F)
True
The number of iterations of a counter-controlled loop is known in advance. (T/F)
True
Which of the following loops is guaranteed to execute at least once? A. forloop B. do...while loop C. while loop
do... while loop
Which of the following loops does not have an entry condition?
do....while loop
Which of the following is a repetition structure in C++?
do...while
In ____ structures, the computer repeats particular statements a certain number of times depending on some condition(s).
looping