Related chapter: Control Flow


Q1. How many times does this loop run? for i in range(3):

  • A) 2
  • B) 3 ✓
  • C) 4
  • D) 0

range(3) produces 0, 1, 2.


Q2. What is the output?

  x = 15
if x > 10:
    print("A")
elif x > 20:
    print("B")
else:
    print("C")
  
  • A) A ✓
  • B) B
  • C) C
  • D) AB

elif is only checked if the preceding if was False. Since 15 > 10, only “A” prints.


Q3. What does this list comprehension produce? [x**2 for x in range(5) if x % 2 == 0]

  • A) [0, 1, 4, 9, 16]
  • B) [0, 4, 16]
  • C) [1, 9]
  • D) [4, 16]

Even numbers 0, 2, 4 squared.


Q4. When does a while loop’s else clause execute?

  • A) Always
  • B) When the loop condition becomes False without a break
  • C) On the first iteration
  • D) Never

Q5. What is the output?

  for i in range(5):
    if i == 3:
        break
    print(i, end=" ")
  
  • A) 0 1 2 3 4
  • B) 0 1 2
  • C) 0 1 2 3
  • D) 0 1 2 4

Q6. Which statement skips the rest of the current loop iteration?

  • A) break
  • B) continue
  • C) pass
  • D) return

Q7. What does match/case require (Python 3.10+)?

  • A) Import from typing
  • B) Nothing — it’s built-in ✓
  • C) from pattern import match
  • D) Only works in classes

Q8. How many elements? {x for x in range(10) if x % 3 == 0}

  • A) 10
  • B) 4 ✓
  • C) 3
  • D) 9

Values: 0, 3, 6, 9 — four elements in the set.


Q9. What is a generator expression?

  • A) A function that generates code
  • B) A lazy comprehension using () instead of []
  • C) A type of for loop
  • D) A decorator

Q10. What prints?

  count = 0
while count < 3:
    count += 1
else:
    print("done")
  
  • A) Nothing
  • B) done
  • C) Error
  • D) 0 1 2 done

The else runs after the loop completes without break.


Next: OOP Quiz | Practice: Control Flow Exercises