Free Practice Quiz Question List

4 Loops and Iteration Online Quiz Questions

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

20 questions
01
Choose one
1 point

What values are produced by this loop?

for number in range(2, 6):

    print(number)

  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

02
Choose one
1 point

In a nested loop, a break statement is inside the inner loop. What does the break do?

  1. A

    Both loops stop immediately.

  2. B

    Only the outer loop stops.

  3. C

    Only the inner loop stops.

  4. D

    The break statement is ignored in nested loops.

03
Choose one
1 point

A program repeatedly asks for input until the user enters a special stopping value, but the number of inputs is unknown. Which loop is the best fit?

  1. A

    Use a for loop because every input sequence has a fixed length.

  2. B

    Use a for loop because it automatically validates the input.

  3. C

    Use a while loop because the sentinel determines when repetition ends.

  4. D

    Use a nested loop because the input may contain multiple words.

04
Choose all
1 point

Select all statements that correctly describe Python's loop else clause.

  1. A

    It runs when a for loop exhausts its iterable without executing break.

  2. B

    It runs when a while loop's condition becomes false without executing break.

  3. C

    It always runs whenever the loop body contains an if statement.

  4. D

    It is useful for reporting that a search found no match when no break occurred.

05
Choose all
1 point

Select all recommended approaches when a program needs to remove or filter items from a collection while processing it.

  1. A

    Build a separate collection containing the desired results.

  2. B

    Modify the same collection directly during traversal because the loop automatically adjusts.

  3. C

    Iterate over a copy when changes to the original collection are necessary.

  4. D

    Use a filtering loop to construct a new collection.

06
True or false
1 point

In Python, continue skips the remainder of the current iteration and starts the next iteration of the loop.

  1. A

    True

  2. B

    False

07
True or false
1 point

In for number in range(3):, assigning number = 100 inside the loop changes the next value that range(3) supplies.

  1. A

    True

  2. B

    False

08
Written response
1 point

What term names a special value that signals the end of input in a sentinel-controlled loop?

09
Written response
1 point

A counter starts at 0 and increases by 1 for each score in the list scores = [72, 91, 64, 88, 95] that is at least 70. What is the counter's final value?

10
Fill in the blank
1 point

In range(start, stop, step), the value is excluded from the generated sequence.

11
Fill in the blank
1 point

A loop can repeatedly request input until the user provides an acceptable value.

12
Open ended
1 point

Explain when a programmer should choose a for loop instead of a while loop in Python. Include how the controlling state affects termination.

13
Choose one
1 point

Given products = ["pen", "notebook", "eraser"] and prices = [1.50, 3.25, 0.75], what does zip(products, prices) do when used in a loop?

  1. A

    It pairs every product with every price.

  2. B

    It pairs each product with the price at the same position.

  3. C

    It sorts both lists before pairing them.

  4. D

    It repeats the last price until all products have been processed.

14
True or false
1 point

In a nested loop, the inner loop completes all of its iterations for each one iteration of the outer loop.

  1. A

    True

  2. B

    False

15
Choose one
1 point

Which loop is the most direct choice for printing every item in the list colors = ["red", "green", "blue"]?

  1. A

    Use a while loop because lists cannot be used with for loops.

  2. B

    Use a for loop because it processes items supplied by an iterable.

  3. C

    Use a while loop because for loops are only for numeric calculations.

  4. D

    Use a for loop because it always repeats forever.

16
Written response
1 point

What is the term for a special value that signals the end of input in a repetition pattern?

17
Choose one
1 point

Which expression is the clearest way to obtain both the index and item while iterating through animals = ["cat", "dog", "bird"]?

  1. A

    zip(animals)

  2. B

    enumerate(animals)

  3. C

    range(animals)

  4. D

    len(animals)

18
Choose one
1 point

Which approach is safest when filtering items from a collection during iteration?

  1. A

    Construct a new collection containing the desired items.

  2. B

    Change the list length during every iteration.

  3. C

    Assign a new value to the loop variable and keep modifying the list.

  4. D

    Use a nested loop that removes items from the original list.

19
Choose one
1 point

What does this Python code print?

values = [4, 7, 2, 9]

total = 0

for value in values:

if value % 2 == 0:

total += value

else:

total -= 1

print(total)

  1. A

    4

  2. B

    6

  3. C

    21

  4. D

    22

20
Written response
1 point

What integer does this Python program print?

total = 1

value = 2

while value <= 8:

total *= value

value *= 2

print(total)