Quiz 5 Flashcards
The following while loop terminates when j > 20.
j = 0;
while (j < 20)
j++;
A) True B) False
False
The number of iterations of a counter-controlled loop is known in advance.
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
What is the output of the following C++ code?
count = 1;
num = 25;
while (count < 25)
{
num = num - 1;
count++;
}
cout «_space;count «_space;” “ «_space;num «_space;endl;
A) 24 0
B) 24 1
C) 25 0
D) 25 1
25 1
Consider the following code. (Assume that all variables are properly declared.)
cin»_space; ch;
while (cin)
{
cout «_space;ch;
cin»_space; 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
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»_space; sum;
cin»_space; num;
for (j = 1; j <= 3; j++)
{
cin»_space; num;
sum = sum + num;
}
cout «_space;sum «_space;endl;
A) 24
B) 25
C) 41
D) 42
24
____ loops are called posttest loops.
A) break
B) for
C) while
D) do…while
D) do…while
Consider the following code.
int limit;
int reps = 0;
cin»_space; limit;
while (reps < limit)
{
cin»_space; entry;
triple = entry * 3;
cout «_space;triple;
reps++;
}
cout «_space;endl;
This code is an example of a(n) ____ while loop.
A) flag-controlled
B) counter-controlled
C) EOF-controlled
D) sentinel-controlled
B) counter-controlled
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
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»_space; num;
for (int j = 1; j <= 4; j++)
{
sum = sum + num;
cin»_space; num;
}
cout «_space;sum «_space;endl;
A) 124
B) 125
C) 126
D) 127
125
Assume that all variables are properly declared. The following for loop executes 20 times.
for (i = 0; i <= 20; i++)
cout «_space;i;
A) True
B) False
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 «_space;n «_space;” “;
}
A) True B) False
True
What is the output of the following C++ code?
num = 10;
while (num > 10)
num = num - 2;
cout «_space;num «_space;endl;
A)
0
B) 6
C) 8
D) 10
10
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»_space; num;
while (num != -1)
{
sum = sum + num;
cin»_space; num;
}
cout «_space;sum «_space;endl;
A) 92
B) 109
C) 110
D) 119
110
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;
A) srand(time(0));
num = rand() % 50;
A loop that continues to execute endlessly is called a(n) ____ loop.
A) end
B) unhinged
C) infinite
D) definite
infinite
Which of the following is a repetition structure in C++?
A) if
B) switch
C) while…do
D) do…while
D) do…while