Free Practice Quiz Question List

05. Iteration and Loops Quiz Online Quiz Questions

Use this free practice quiz with 20 questions to review 05. Iteration and Loops, test your knowledge, and prepare for your next test or exam.

20 questions
01
Choose one
1 point

Which statement best explains why a while loop can execute its body zero times?

  1. A

    The body executes once before the condition is checked.

  2. B

    The body may execute zero times because the condition is checked first.

  3. C

    The body always executes at least twice.

  4. D

    The condition is checked only after every iteration.

02
True or false
1 point

True or false: In Python-like syntax, range(1, 5) includes the value 5.

  1. A

    True

  2. B

    False

03
Written response
1 point

What is the name for a special input value that signals the end of a sentinel-controlled loop?

04
Fill in the blank
1 point

In the three-part loop design of initialization, condition, and , which part changes the state so the loop can eventually finish?

05
Choose one
1 point

A program examines [4, 7, 2, 9, 6] and increases a variable only when the current value is even. What is the variable functioning as?

  1. A

    It stores the sum of every number in the list.

  2. B

    It stores the largest number seen so far.

  3. C

    It increases when a specified event is observed.

  4. D

    It ensures that the loop condition is always true.

06
True or false
1 point

True or false: A break statement inside the inner loop of a nested-loop structure automatically terminates both the inner and outer loops.

  1. A

    True

  2. B

    False

07
Written response
1 point

How many times does the innermost body execute?\n\nfor row in range(2):\n for column in range(3):\n process()

08
Fill in the blank
1 point

In a loop, the statement skips the remainder of the current iteration and moves to the next one.

09
Choose one
1 point

A program must apply the same operation to every item in a list whose contents are already available. Which design is most appropriate?

  1. A

    Use a for loop to visit every item in a known list.

  2. B

    Use a while loop and manually track an index even though the list is the direct source of repetition.

  3. C

    Use a sentinel-controlled while loop for every list traversal.

  4. D

    Use an infinite loop because the list length is known.

10
Open ended
1 point

Design and explain a loop that counts the positive values in [-2, 0, 4, 7, -1]. State the initialization, the condition that causes the counter to increase, the final result, and an invariant that remains true after each iteration.

11
Choose one
1 point

A program needs to create a list of even values from an existing list. Which approach is safest?

  1. A

    Remove each matching item from the list during the same traversal.

  2. B

    Build a new collection while traversing the original list.

  3. C

    Insert new items at the current position during every iteration.

  4. D

    Change the list length whenever the loop condition is checked.

12
Choose one
1 point

What values does this loop print?\n\nfor n in range(2, 6):\n print(n)

  1. A

    2, 3, 4, 5, 6

  2. B

    2, 3, 4, 5

  3. C

    1, 2, 3, 4, 5

  4. D

    3, 4, 5, 6

13
Choose one
1 point

What is the result of this code?\n\ni = 0\nwhile i < 0:\n print(i)\n i = i + 1

  1. A

    It prints 0 once.

  2. B

    It prints 0 repeatedly.

  3. C

    It prints nothing.

  4. D

    It raises an error because the condition is false.

14
Choose one
1 point

A sentinel-controlled loop reads the values 4, 7, and then -1, where -1 is the sentinel. What total should the loop produce if it adds each ordinary value and excludes the sentinel?

  1. A

    11

  2. B

    10

  3. C

    12

  4. D

    -1

15
Choose one
1 point

Which initialization is appropriate for total before a loop computes the sum of all values in [3, 5, 2]?

  1. A

    total = 0

  2. B

    total = 1

  3. C

    total = 3

  4. D

    total = 10

16
True or false
1 point

True or false: In a nested loop, a break statement inside the inner loop automatically terminates both the inner and outer loops.

  1. A

    True

  2. B

    False

17
Written response
1 point

What integer does this code print?\n\nvalues = [-2, 0, 4, 7, -1]\npositive_count = 0\n\nfor value in values:\n if value > 0:\n positive_count += 1\n\nprint(positive_count)

18
Written response
1 point

How many numbers does this loop print?\n\nfor number in range(1, 6):\n if number == 3:\n continue\n print(number)

19
Choose all
1 point

A command-processing loop should print every command except "quit", then stop when "quit" is entered. Which two redesigns preserve that behavior while providing a reliable path to termination? Select all correct choices.

  1. A

    Use a condition such as while command != "quit" and read the next command at the end of each non-quit iteration.

  2. B

    Keep an explicit break for the quit command and read the next command after processing each non-quit command.

  3. C

    Add a break immediately after processing the first non-quit command.

  4. D

    Set command to "quit" before entering the loop so that the loop has a guaranteed exit.

  5. E

    Remove every update to command because the loop condition is checked automatically.

20
Choose all
1 point

You are testing a loop that finds the largest value in a list. Which test cases are especially useful for exposing boundary or initialization problems? Select all correct choices.

  1. A

    An empty list, to reveal whether the algorithm handles zero items safely.

  2. B

    A one-item list, to test the smallest nonempty input.

  3. C

    A list whose largest value is the first item.

  4. D

    A list whose largest value is the last item.

  5. E

    Only one typical multi-item list with the largest value somewhere in the middle.