CS Quiz 5
What is the output of the following C++ code? num = 10; while (num > 10) num = num - 2; cout << num << endl;
10
What is the output of the following C++ code? int j; for(j = 10; j <=10; j++) cout << j << " "; cout << j << endl;
10 11
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
Assume that all variables are properly declared. The following for loop executes 20 times. for(i = 0; i <= 20; i++) cout << i;
False
The following while loop terminates when j > 20. j = 0; while (j < 20) j++;
False
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.
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 loop is a bool variable.
True
The number of iterations of a counter-controlled loop is known in advance.
True
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
c. do...while loop
Consider the following code. int limit; int reps = 0; cin >> limit; while (reps < limit) { cin >> entry; triple = entry * 3; cout << triple; reps++; } This code is an example of a(n) ___ while loop
counter-controlled
Which of the following statements generates a random number between 0 and 50? a. srand(time(10)); num = rand()/50; b. srand(time(0)); num = rand()50; c. srand(time(10)); num = rand() % 50; d. srand(time(0));num = rand() % 50;
d. srand(time(0));num = rand() % 50;
A(n) ___ -controlled while loop uses a bool variable to control the loop
flag
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;
i = 1
A loop that continues to execute endlessly is called a(n) ___ loop.
infinite
In ___ structures, the computer repeats particular statements a certain number of times depending on some condition(s).
looping
The statement in the body of a while loop acts as a decision maker.
False
___ loops are called posttest loops.
do...while