Unlock access to all the studying documents.
View Full Document
Copyright Cengage Learning. Powered by Cognero.
1. The statement in the body of a while loop acts as a decision maker.
2. The following while loop terminates when j > 20.
j = 0;
while (j < 20)
j++;
3. The number of iterations of a counter-controlled loop is known in advance.
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++;
Copyright Cengage Learning. Powered by Cognero.
5. In a counter-controlled while loop, the loop control variable must be initialized before the loop.
6. In the case of the sentinel-controlled while loop, the first item is read before the while loop is entered.
7. In a sentinel-controlled while loop, the body of the loop continues to execute until the EOF symbol is read.
Copyright Cengage Learning. Powered by Cognero.
15. 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.
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;
Copyright Cengage Learning. Powered by Cognero.
cin >> num;
}
cout << sum << endl;
17. A(n) ____-controlled while loop uses a bool variable to control the loop.
18. Which of the following statements generates a random number between 0 and 50?
srand(time(0));
num = rand() % 50;
srand(time(10));
num = rand()/50;
srand(time(0));
num = rand()50;
srand(time(10));
num = rand() % 50;
19. Consider the following code. (Assume that all variables are properly declared.)
cin >> ch;
while (cin)
{
cout << ch;
Copyright Cengage Learning. Powered by Cognero.
cin >> ch;
}
This code is an example of a(n) ____ while loop.
20. What is the next Fibonacci number in the following sequence?
1, 1, 2, 3, 5, 8, 13, 21, …
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;
22. What is the output of the following C++ code?
Copyright Cengage Learning. Powered by Cognero.
int j;
for (j = 10; j <= 10; j++)
cout << j << ” “;
cout << j << endl;
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;
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;
Copyright Cengage Learning. Powered by Cognero.
25. Which of the following is a repetition structure in C++?
26. Which executes first in a do…while loop?
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);
Copyright Cengage Learning. Powered by Cognero.
28. What is the output of the following loop?
count = 5;
cout << ‘St’;
do
{
cout << ‘o’;
count–;
}
while (count <= 5);
This is an infinite loop.
29. ____ loops are called posttest loops.
30. Which of the following loops does not have an entry condition?
EOF-controlled while loop
sentinel-controlled while loop
Copyright Cengage Learning. Powered by Cognero.
31. Which of the following loops is guaranteed to execute at least once?
counter-controlled while loop
sentinel-controlled while loop
32. The ____ statement can be used to eliminate the use of certain (flag) variables.
33. What executes immediately after a continue statement in a while and do–while loop?
34. When a continue statement is executed in a ____, the update statement always executes.
Copyright Cengage Learning. Powered by Cognero.
35. To generate a random number, you can use the function rand of the header file ____________________.
36. The function eof is a member of the data type ____________________.
37. A semicolon at the end of the for statement (just before the body of the loop) is a(n) ____________________ error.
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.