Control flow · 10 sample answers

Python loops: practice questions with answers.

Loops are where a program starts doing repetitive work. The common failures are mechanical: an off-by-one range, an accumulator that never updates, or a while condition that never becomes false.

61 questions in the full bank · 16 choice · 10 fill · 35 output

What this topic tests

  • Predict how many times range and while loops run.
  • Build a total or a filtered list one step at a time.
  • Use break and continue deliberately, not as a substitute for a clear condition.

Common mistakes

  • Assuming range(1, 5) includes 5. The stop value is excluded.
  • Forgetting to update the variable used by a while condition.
  • Changing a list while looping over it and skipping elements.
Sample questions

Sample questions

10

Ten representative questions from the full bank. Try each one before opening the answer.

  1. Multiple choiceq0305

    1. Ming sends coupons to tables 2 through 5. Which table numbers does range(2, 6) produce?

    1. 2,3,4,5,6
    2. 2,3,4,5
    3. 2,4,6
    4. 0,1,2,3,4,5
    Show answer

    Answer

    B. 2,3,4,5

    Why: range(start, stop) includes start and excludes stop, so 2, 3, 4, 5. Reading stop as an inclusive bound is what produces the extra 6.

  2. Multiple choiceq0317

    2. 🧋 What does range(10, 0, -1) produce?

    1. 10,9,8,...,1
    2. 10,9,8,...,0
    3. 0,1,...,10
    4. an empty sequence
    Show answer

    Answer

    A. 10,9,8,...,1

    Why: A negative step counts down and the stop is still excluded, so 0 never appears; range(10, 0, -1) ends at 1. range(10, -1, -1) would include 0.

  3. Multiple choiceq0324

    3. 🧋 How many pairs does zip([1,2,3], ['a','b']) produce?

    1. 3
    2. 2
    3. 1
    4. raises an error
    Show answer

    Answer

    B. 2

    Why: zip stops as soon as the shortest iterable is exhausted, so the extra 3 is dropped without an error. zip_longest keeps it with a fill value.

  4. Multiple choiceq0355

    4. To process each order in a list in turn, which loop is most direct?

    1. match orders
    2. while True
    3. for order in orders
    4. if order in orders
    Show answer

    Answer

    C. for order in orders

    Why: A for loop walks the iterable directly and stops when it is exhausted. while True would need a manual index, an exit test and a break.

  5. Fill in the blankq0307

    5. Ming pairs drink names with prices. Which function iterates two lists together: ___(names, prices)?

    Show answer

    Answer

    zip

    Why: zip walks two iterables in step and yields one tuple per position, which the loop unpacks. It stops at the shorter input, so unequal lengths silently drop data.

  6. Fill in the blankq0319

    6. 🧋 How many elements does range(3) produce?

    Show answer

    Answer

    3

    Why: range(3) yields 0, 1, 2: three values starting at 0. Beginners often answer 2 by counting 1 to 3 instead of counting from 0.

  7. Fill in the blankq0336

    7. Which built-in function produces an integer sequence for a for loop?

    Show answer

    Answer

    range

    Why: range is the built-in that produces an arithmetic sequence lazily; it does not build a list in memory. Wrapping it in list() materialises the values.

  8. Output predictionq0306

    8. What does this code print when the range is converted to a list?

    print(list(range(2, 6)))
    Show answer

    Answer

    [2, 3, 4, 5]

    Why: list() materialises every value the range would yield; 6 is the stop and never appears. range itself is lazy, so printing it directly shows range(2, 6).

  9. Output predictionq0331

    9. A sign prints the letters of "go" in uppercase without spaces. What appears?

    for ch in "go":
        print(ch.upper(), end="")
    Show answer

    Answer

    GO

    Why: Iterating a string yields characters, and upper() changes each one; end='' removes the newline so the letters join with no separator.

  10. Output predictionq0347

    10. What does `factors(12)` return?

    def factors(value):
        factor_list = []
        divisor = 2
        while value > 1:
            while value % divisor == 0:
                factor_list.append(divisor)
                value /= divisor
    
            divisor += 1
    
        return factor_list
    
    print(factors(12))
    Show answer

    Answer

    [2, 2, 3]

    Why: 12 factors into 2, 2, 3; the inner loop exhausts 2 before divisor moves to 3. The order is ascending because divisor only increases.

Common questions

When should I use while instead of for?

Use for when you are iterating over a known collection or range. Use while when the stopping condition is the main thing you know.

Does break skip the else block?

Yes. A loop's else block runs only when the loop finishes without break.

Keep the streak going.

The full bank keeps your place in this browser, filters by topic, and includes a daily question and mistake review.

Open full practice