Learn Without Walls
← Back to Module 5

Module 5 Quiz: Loops

12 questions • Test your understanding

Question 1: What does range(5) produce?

  • 1, 2, 3, 4, 5
  • 0, 1, 2, 3, 4, 5
  • 0, 1, 2, 3, 4
  • 1, 2, 3, 4
Correct! range(5) generates numbers starting from 0 up to (but not including) 5: 0, 1, 2, 3, 4.

Question 2: What is the output?

for i in range(3, 7):
    print(i, end=" ")
  • 3 4 5 6 7
  • 3 4 5 6
  • 4 5 6 7
  • 3 4 5
Correct! range(3, 7) starts at 3 and goes up to but not including 7: 3, 4, 5, 6.

Question 3: When should you use a while loop instead of a for loop?

  • When you do not know how many iterations are needed
  • When you are iterating over a list
  • When you need to count from 1 to 10
  • While loops should always be used instead of for loops
Correct! While loops are ideal when the number of iterations depends on a condition that may change unpredictably, like user input or data-dependent logic.

Question 4: What does break do?

  • Skips the current iteration
  • Pauses the loop temporarily
  • Restarts the loop from the beginning
  • Exits the loop immediately
Correct! break immediately terminates the innermost loop and continues with the code after the loop.

Question 5: What does continue do?

  • Exits the loop
  • Skips the rest of the current iteration and moves to the next
  • Repeats the current iteration
  • Does nothing
Correct! continue skips the remaining code in the current iteration and jumps back to the loop condition check.

Question 6: What will this code print?

for i in range(5):
    if i == 3:
        break
    print(i, end=" ")
  • 0 1 2 3
  • 0 1 2 3 4
  • 0 1 2
  • 0 1 2 4
Correct! When i is 3, break exits the loop before print executes. So only 0, 1, 2 are printed.

Question 7: What will this code print?

for i in range(5):
    if i == 3:
        continue
    print(i, end=" ")
  • 0 1 2
  • 0 1 2 3 4
  • 3
  • 0 1 2 4
Correct! When i is 3, continue skips the print but does NOT exit the loop. All numbers except 3 are printed.

Question 8: How many times will the inner loop body execute?

for i in range(3):
    for j in range(4):
        print("X")
  • 12
  • 7
  • 4
  • 3
Correct! The outer loop runs 3 times. For each outer iteration, the inner loop runs 4 times. Total: 3 × 4 = 12.

Question 9: What is the purpose of pass?

  • To exit a loop
  • To skip an iteration
  • To serve as a placeholder that does nothing
  • To pause execution
Correct! pass is a "do nothing" statement used as a placeholder where Python requires a statement syntactically.

Question 10: What will this while loop print?

x = 1
while x < 8:
    print(x, end=" ")
    x *= 2
  • 1 2 4 8
  • 1 2 4
  • 2 4 8
  • 1 2 4 6
Correct! x starts at 1. Each iteration: print x then double it. x=1 (print, x becomes 2), x=2 (print, x becomes 4), x=4 (print, x becomes 8), x=8 (8 is not < 8, loop ends).

Question 11: What does range(10, 0, -2) produce?

  • 10, 8, 6, 4, 2
  • 10, 8, 6, 4, 2, 0
  • 0, 2, 4, 6, 8, 10
  • 8, 6, 4, 2
Correct! range(10, 0, -2) starts at 10, counts down by 2, and stops before 0: 10, 8, 6, 4, 2.

Question 12: When does a loop's else clause execute?

  • When the loop condition is initially False
  • Every time the loop runs
  • When a break statement is reached
  • When the loop completes without hitting a break
Correct! A loop's else clause runs only when the loop finishes normally (not terminated by break).

Quiz Results

Back to Module 5