Python Control Structures Quiz
Questions: 16 · 10 minutes
1. What value is printed after this loop?
x = 1
while x < 10:
x *= 2
print(x)
8
16
32
10
2. Given age = 18, what value is assigned here?
label = ""adult"" if age >= 18 else ""minor""
True
minor
18
adult
3. A function is being drafted, and one branch must temporarily contain no action while remaining syntactically valid. Which statement belongs in that branch?
continue
break
return
pass
4. What value is printed?
total = 0
for i in range(5):
if i == 2:
continue
total += i
print(total)
6
The code raises an error when continue is reached.
10
8
5. Inside a loop that processes a list of temperatures, you want to skip negative values but continue processing later values. Which statement should run when a negative value is found?
pass
break
continue
return
6. How many times is print(""go"") executed?
for i in range(2):
for j in range(3):
print(""go"")
3
5
6
2
7. Which list is produced by list(range(1, 6, 2))?
[1, 3, 5]
[1, 2, 3, 4, 5]
[1, 3, 5, 7]
[2, 4, 6]
8. What does this code print?
for n in range(10, 30):
if n % 7 == 0:
print(n)
break
14
21
14, 21, and 28
Nothing, because range() excludes 30.
9. What does this code print?
x = 7
if x % 2 == 0:
print(""even"")
else:
print(""odd"")
even
odd
7
It raises an error because % cannot be used in an if condition.
10. What does this conditional print?
x = 10
if x > 5:
print(""A"")
elif x > 8:
print(""B"")
else:
print(""C"")
A, because Python stops after the first matching branch
A followed by B, because both conditions are true
B, because 10 is greater than 8
C, because the conditions overlap
11. What is the output of this loop?
n = 3
while n > 0:
print(n, end="""")
n -= 1
123
3210
321
The loop runs forever.
12. What does this code do?
x = 0
if x != 0 and 10 / x > 1:
print(""A"")
else:
print(""B"")
It raises ZeroDivisionError before choosing a branch.
It prints ""B"" because and short-circuits after x != 0 is false.
It prints ""A"" because 10 is greater than 1.
It prints nothing because the first condition is false.
13. What happens when this code runs?
for n in [2, 4, 6]:
if n % 2 != 0:
break
else:
print(""All even"")
Nothing is printed because every for loop must use an explicit else condition.
The loop stops at 2 because the if condition is false.
It prints ""All even"" once for each number.
It prints ""All even"" because the loop finishes without break.
14. What does this code print?
records = []
if records:
print(""has records"")
else:
print(""empty"")
has records, because records is defined
Nothing, because a list cannot be used as a condition
empty, because an empty list is falsy
It raises a TypeError when evaluating records
15. What grade does this code print when score is 80?
if score >= 90:
print(""A"")
elif score >= 80:
print(""B"")
elif score >= 70:
print(""C"")
else:
print(""D"")
A
B
C
D
16. Which pairs are printed by this nested loop?
for i in range(2):
for j in range(3):
if j == 1:
break
print(i, j)
(0, 0) and (1, 0), because break ends only the inner loop
(0, 0), (0, 2), (1, 0), and (1, 2)
(0, 0), (0, 1), (1, 0), and (1, 1)
Only (0, 0), because break ends both loops