Suppose count is initialized to 10. How many times does the body of while (count < 5) { ... } execute, assuming no code outside the shown loop changes count?
04 Loops and Iteration Online Quiz Questions
Use this free practice quiz with 20 questions to review 04 Loops and Iteration, test your knowledge, and prepare for your next test or exam.
A C++ do-while loop executes its body at least once, even when its condition is initially false.
- A
True
- B
False
Which C++ loop statement is most appropriate for processing every integer from 0 through n - 1 when n is known before the loop begins?
A while loop tests its condition executing its body.
Which statement correctly describes the execution of a C++ for loop?
- A
The update runs before the first condition check.
- B
The condition is checked only after the body runs.
- C
The update expression runs after each completed iteration.
- D
The initialization runs again after every iteration.
If a break statement runs inside an inner loop nested within an outer loop, it exits only the inner loop.
- A
True
- B
False
Select all statements that correctly describe what happens when continue executes in a C++ for loop.
- A
It skips the remaining statements in the current iteration.
- B
It runs the
forloop's update expression before the next condition check. - C
It immediately terminates the entire loop.
- D
It allows the loop condition to be checked again.
A loop initializes total to 0 and executes total += number for every integer number from 1 through 10, inclusive. What is the final value of total?
In sentinel-controlled input, the special value that signals the end of input is called a .
What does this loop print?
for (int number = 1; number <= 10; ++number) { if (number % 2 == 0) { continue; } std::cout << number << ' ';}
- A
1 2 3 4 5 6 7 8 9 10 - B
1 3 5 7 9 - C
2 4 6 8 10 - D
No values are printed.
Select all recommendations that the material gives for writing reliable and maintainable loops.
- A
Initialize loop-control variables before using them.
- B
Ensure each iteration makes progress toward termination.
- C
Always use
breakinstead of a loop condition. - D
Use braces even for a one-statement body.
Explain the differences among C++ while, do-while, and for loops. For each type, describe when its condition is tested, whether its body can execute zero times, and a situation in which that loop is an appropriate choice. Also explain why initialization and progress toward termination matter.
How many times does the inner body execute in this code?
for (int row = 1; row <= 3; ++row) { for (int column = 1; column <= 4; ++column) { std::cout << '(' << row << ',' << column << ") "; }}
- A
4
- B
7
- C
12
- D
16
What does this C++ code print?
int count = 2;
while (count < 5) {
std::cout << count << ' ';
++count;
}
- A
2 3 4 5
- B
2 3 4
- C
1 2 3 4
- D
3 4 5
A variable choice is initialized to 0 and then used in this loop:
do {
std::cout << "Menu";
} while (choice != 0);
How many times does the body execute?
- A
The body executes zero times
- B
The body executes until
choicebecomes 1 - C
The body executes exactly once
- D
The program does not compile
What does this C++ code print?
for (int number = 1; number <= 5; ++number) {
if (number % 2 == 0) {
continue;
}
std::cout << number << ' ';
}
- A
1 3 5
- B
1 2 3 4 5
- C
2 4
- D
1 3 5 6
How many lines does this nested-loop program output?
for (int row = 0; row < 3; ++row) {
for (int column = 0; column < 3; ++column) {
if (column == 1) {
break;
}
std::cout << row << ", " << column << '\n';
}
}
- A
1 line
- B
2 lines
- C
6 lines
- D
3 lines
True or false: In a while loop, placing continue before the update of the variable used in the loop condition can cause an infinite loop.
- A
True
- B
False
What integer value is stored in total after this loop finishes?
int total = 0;
for (int value = 1; value <= 5; ++value) {
total += value;
}
What programming term names a variable that stores a running total or combined result while a loop executes?