Free Practice Quiz Question List

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.

20 questions
01
Choose one
1 point

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?

  1. A

    The body executes exactly once.

  2. B

    The body executes zero times.

  3. C

    The condition is ignored on the first check.

  4. D

    The loop automatically changes the condition.

02
True or false
1 point

A C++ do-while loop executes its body at least once, even when its condition is initially false.

  1. A

    True

  2. B

    False

03
Written response
1 point

Which C++ loop statement is most appropriate for processing every integer from 0 through n - 1 when n is known before the loop begins?

04
Fill in the blank
1 point

A while loop tests its condition executing its body.

05
Choose one
1 point

Which statement correctly describes the execution of a C++ for loop?

  1. A

    The update runs before the first condition check.

  2. B

    The condition is checked only after the body runs.

  3. C

    The update expression runs after each completed iteration.

  4. D

    The initialization runs again after every iteration.

06
True or false
1 point

If a break statement runs inside an inner loop nested within an outer loop, it exits only the inner loop.

  1. A

    True

  2. B

    False

07
Choose all
1 point

Select all statements that correctly describe what happens when continue executes in a C++ for loop.

  1. A

    It skips the remaining statements in the current iteration.

  2. B

    It runs the for loop's update expression before the next condition check.

  3. C

    It immediately terminates the entire loop.

  4. D

    It allows the loop condition to be checked again.

08
Written response
1 point

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?

09
Fill in the blank
1 point

In sentinel-controlled input, the special value that signals the end of input is called a .

10
Choose one
1 point

What does this loop print?

for (int number = 1; number <= 10; ++number) {
if (number % 2 == 0) {
continue;
}
std::cout << number << ' ';
}

  1. A

    1 2 3 4 5 6 7 8 9 10

  2. B

    1 3 5 7 9

  3. C

    2 4 6 8 10

  4. D

    No values are printed.

11
Choose all
1 point

Select all recommendations that the material gives for writing reliable and maintainable loops.

  1. A

    Initialize loop-control variables before using them.

  2. B

    Ensure each iteration makes progress toward termination.

  3. C

    Always use break instead of a loop condition.

  4. D

    Use braces even for a one-statement body.

12
Open ended
1 point

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.

13
Choose one
1 point

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 << ") ";
}
}

  1. A

    4

  2. B

    7

  3. C

    12

  4. D

    16

14
Choose one
1 point

What does this C++ code print?

int count = 2;

while (count < 5) {

std::cout << count << ' ';

++count;

}

  1. A

    2 3 4 5

  2. B

    2 3 4

  3. C

    1 2 3 4

  4. D

    3 4 5

15
Choose one
1 point

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?

  1. A

    The body executes zero times

  2. B

    The body executes until choice becomes 1

  3. C

    The body executes exactly once

  4. D

    The program does not compile

16
Choose one
1 point

What does this C++ code print?

for (int number = 1; number <= 5; ++number) {

if (number % 2 == 0) {

continue;

}

std::cout << number << ' ';

}

  1. A

    1 3 5

  2. B

    1 2 3 4 5

  3. C

    2 4

  4. D

    1 3 5 6

17
Choose one
1 point

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';

}

}

  1. A

    1 line

  2. B

    2 lines

  3. C

    6 lines

  4. D

    3 lines

18
True or false
1 point

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.

  1. A

    True

  2. B

    False

19
Written response
1 point

What integer value is stored in total after this loop finishes?

int total = 0;

for (int value = 1; value <= 5; ++value) {

total += value;

}

20
Written response
1 point

What programming term names a variable that stores a running total or combined result while a loop executes?