Module 5 Quiz: Loops
12 questions • Test your understanding
Question 1: What does range(5) produce?
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=" ")
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?
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?
Correct!
break immediately terminates the innermost loop and continues with the code after the loop.Question 5: What does continue do?
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=" ")
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=" ")
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")
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?
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
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?
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?
Correct! A loop's
else clause runs only when the loop finishes normally (not terminated by break).