Deck 5: Control Structures II Repetition

Full screen (f)
exit full mode
Question
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
Use Space or
up arrow
down arrow
to flip the card.
Question
A(n) ____-controlled while loop uses a bool variable to control the loop.

A) counter
B) sentinel
C) flag
D) EOF
Question
In a sentinel-controlled while loop, the body of the loop continues to execute until the EOF symbol is read.
Question
The control statements in the for loop include the initial statement, loop condition, and update statement.
Question
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
Question
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
Question
Assume that all variables are properly declared. The following for loop executes 20 times.
for (i = 0; i <= 20; i++)
cout << i;
Question
The number of iterations of a counter-controlled loop is known in advance.
Question
In the case of the sentinel-controlled while loop, the first item is read before the while loop is entered.
Question
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 << " ";
}
Question
The statement in the body of a while loop acts as a decision maker.
Question
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;
Question
In a counter-controlled while loop, the loop control variable must be initialized before the loop.
Question
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
Question
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
Question
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
Question
What is the output of the following C++ code? num = 100;
While (num <= 150)
Num = num + 5;
Cout << num << endl;

A) 150
B) 155
C) 160
D) 165
Question
A loop that continues to execute endlessly is called a(n) ____ loop.

A) end
B) unhinged
C) infinite
D) definite
Question
The following while loop terminates when j > 20.
j = 0;
while (j < 20)
j++;
Question
The control variable in a flag-controlled while loop is a bool variable.
Question
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.
Question
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
Question
____ loops are called posttest loops.

A) break
B) for
C) while
D) do...while
Question
Which executes first in a do...while loop?

A) the statement
B) loop condition
C) the expression
D) update statement
Question
A semicolon at the end of the for statement (just before the body of the loop) is a(n) ____________________ error.
Question
Which of the following is a repetition structure in C++?

A) if
B) switch
C) while...do
D) do...while
Question
A for loop is typically called a counted or ____________________ for loop.
Question
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
Question
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
Question
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
Question
The function srand takes as input a(n) ____________________ int, which acts as the seed for the algorithm.
Question
The ____ statement can be used to eliminate the use of certain (flag) variables.

A) while
B) switch
C) break
D) if
Question
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
Question
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";
Question
The function eof is a member of the data type ____________________.
Question
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
Question
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
Question
To generate a random number, you can use the function rand of the header file ____________________.
Question
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
Question
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
Question
A do...while loop is a(n) ____________________ loop, since the loop condition is evaluated after executing the body of the loop.
Question
The for loop body executes indefinitely if the loop condition is always ____________________.
Question
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.
Question
The ____________________ statement is typically used for two purposes:
• To exit early from a loop.
• To skip the remainder of a switch structure.
Question
Putting one control structure statement inside another is called ____________________.
Question
In a(n) "____________________" problem, either the loop executes one too many or one too few times.
Question
A software ____________________ is a piece of code written on top of an existing piece of code intended to fix a bug in the original code.
Question
A loop ____________________ is a set of statements that remains true each time the loop body is executed.
Question
The ____________________ loop has an exit condition but no entry condition.
Question
If a(n) ____________________ statement is placed in a do...while structure, the loop-continue test is evaluated immediately after this statement.
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/50
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 5: Control Structures II Repetition
1
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
C
2
A(n) ____-controlled while loop uses a bool variable to control the loop.

A) counter
B) sentinel
C) flag
D) EOF
C
3
In a sentinel-controlled while loop, the body of the loop continues to execute until the EOF symbol is read.
False
4
The control statements in the for loop include the initial statement, loop condition, and update statement.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
5
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
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
6
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
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
7
Assume that all variables are properly declared. The following for loop executes 20 times.
for (i = 0; i <= 20; i++)
cout << i;
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
8
The number of iterations of a counter-controlled loop is known in advance.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
9
In the case of the sentinel-controlled while loop, the first item is read before the while loop is entered.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
10
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 << " ";
}
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
11
The statement in the body of a while loop acts as a decision maker.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
12
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;
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
13
In a counter-controlled while loop, the loop control variable must be initialized before the loop.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
14
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
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
15
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
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
16
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
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
17
What is the output of the following C++ code? num = 100;
While (num <= 150)
Num = num + 5;
Cout << num << endl;

A) 150
B) 155
C) 160
D) 165
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
18
A loop that continues to execute endlessly is called a(n) ____ loop.

A) end
B) unhinged
C) infinite
D) definite
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
19
The following while loop terminates when j > 20.
j = 0;
while (j < 20)
j++;
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
20
The control variable in a flag-controlled while loop is a bool variable.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
21
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.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
22
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
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
23
____ loops are called posttest loops.

A) break
B) for
C) while
D) do...while
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
24
Which executes first in a do...while loop?

A) the statement
B) loop condition
C) the expression
D) update statement
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
25
A semicolon at the end of the for statement (just before the body of the loop) is a(n) ____________________ error.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
26
Which of the following is a repetition structure in C++?

A) if
B) switch
C) while...do
D) do...while
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
27
A for loop is typically called a counted or ____________________ for loop.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
28
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
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
29
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
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
30
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
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
31
The function srand takes as input a(n) ____________________ int, which acts as the seed for the algorithm.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
32
The ____ statement can be used to eliminate the use of certain (flag) variables.

A) while
B) switch
C) break
D) if
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
33
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
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
34
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";
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
35
The function eof is a member of the data type ____________________.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
36
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
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
37
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
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
38
To generate a random number, you can use the function rand of the header file ____________________.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
39
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
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
40
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
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
41
A do...while loop is a(n) ____________________ loop, since the loop condition is evaluated after executing the body of the loop.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
42
The for loop body executes indefinitely if the loop condition is always ____________________.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
43
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.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
44
The ____________________ statement is typically used for two purposes:
• To exit early from a loop.
• To skip the remainder of a switch structure.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
45
Putting one control structure statement inside another is called ____________________.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
46
In a(n) "____________________" problem, either the loop executes one too many or one too few times.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
47
A software ____________________ is a piece of code written on top of an existing piece of code intended to fix a bug in the original code.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
48
A loop ____________________ is a set of statements that remains true each time the loop body is executed.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
49
The ____________________ loop has an exit condition but no entry condition.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
50
If a(n) ____________________ statement is placed in a do...while structure, the loop-continue test is evaluated immediately after this statement.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 50 flashcards in this deck.