Playing as a guest? to earn XP and track your progress.
๐ Flow of Control Quiz
๐ Flow of Control
Which three control structures does Python support for the flow of control?
Functions, modules, packages
Lists, tuples, dictionaries
Sequence, selection, iteration
Input, process, output
Python supports sequence (statements one after another), selection (if statements) and iteration (for and while loops).
Which statement does nothing and is used as a placeholder?
else
continue
break
pass
pass is an empty statement: it keeps the syntax valid where a statement is required but no action is needed.
What happens if you write 'if x > 5:' and leave nothing indented under it?
Nothing โ it is allowed
IndentationError, because the body cannot be empty
The if is skipped automatically
NameError
Python does not allow an empty body after a colon; use pass as a placeholder.
Which of the following is a compound statement?
if marks >= 33: (with an indented body)
pass
x = 5
print(x)
A compound statement (if, for, while) has a header ending with a colon and an indented body.
In Python, how is a block of statements defined?
With curly braces { }
With semicolons
With indentation โ all statements of a block have the same indentation
With BEGIN and END
Indentation defines a block (4 spaces is standard); wrong indentation causes an error or changes the logic.
What is printed when the input is 15?
age = int(input()) if age >= 18: print("You can vote!") print("Thank you")
Nothing
You can vote!
You can vote! Thank you
Thank you
15 >= 18 is False, so the if block is skipped; the last print is outside the if and always runs.
In an if-else statement:
Neither block may run
Exactly one of the two blocks always runs
The else block always runs
Both blocks may run
if-else is a two-way decision: the if block runs when the condition is True, otherwise the else block runs.
Which condition correctly tests for a leap year?
(year % 400 == 0) or (year % 4 == 0 and year % 100 != 0)
year % 4 == 0
year % 4 == 0 and year % 400 != 0
year % 100 == 0
A leap year is divisible by 400, OR divisible by 4 but not by 100.
Using the leap-year rule, which of these is NOT a leap year?
2000
2024
1900
1996
1900 is divisible by 100 but not by 400, so it is not a leap year; 2000, 2024 and 1996 are.
In an if-elif-else chain, which block runs?
Always the else block
Every block whose condition is True
The last True block
The block of the FIRST condition that is True; the rest are skipped
Conditions are checked top to bottom, and only the first True block runs; else runs if none is True.
In the grade program (>=90 A, >=75 B, >=60 C, >=33 D, else E), what grade is printed for 82?
D
B
A
C
82 >= 90 is False; 82 >= 75 is True, so the grade is B.
If 'if marks >= 33:' is checked BEFORE 'marks >= 90' in a grading program, what grade does 95 marks get?
D
A
E
B
Python stops at the first True condition; 95 >= 33 is True, so the grade is wrongly D. Check from highest to lowest.
Which keyword does Python use instead of 'else if'?
ifelse
else-if
elif
elseif
elif is short for 'else if'; writing else if on one line is not valid Python.
How many if, elif and else parts can an if-elif-else statement have?
One if, one elif, one else
One if and at most two elifs
Any number of each
One if, any number of elifs, at most one else
There is exactly one if at the start, any number of elif blocks, and at most one else at the end.
What does the nested-if program (positive even/odd, negative, zero) print for the input 0?
Positive even number
Zero
Negative number
Positive odd number
0 > 0 is False and 0 < 0 is False, so the else block prints 'Zero'.
What numbers does range(5) generate?
0, 1, 2, 3, 4
1, 2, 3, 4, 5
0, 1, 2, 3, 4, 5
5
With one argument, start defaults to 0 and stop (5) is excluded.
What is list(range(1, 10, 2))?
[2, 4, 6, 8]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 3, 5, 7, 9]
[1, 3, 5, 7, 9, 11]
It starts at 1, jumps by 2, and stops before 10.
What is list(range(10, 0, -2))?
[10, 8, 6, 4, 2, 0]
[]
[0, 2, 4, 6, 8]
[10, 8, 6, 4, 2]
It counts down by 2 from 10; the stop value 0 is excluded.
What is list(range(5, 2))?
[5, 4, 3]
[] (empty)
[5, 4, 3, 2]
Error
With the default step of +1, you cannot go up from 5 to 2, so nothing is generated.
What is list(range(2, 11, 3))?
[2, 5, 8]
[2, 5, 8, 11]
[3, 6, 9]
[2, 4, 6, 8, 10]
It starts at 2 and adds 3 each time: 2, 5, 8; the next value, 11, is excluded.
What is list(range(3, 20, 4))?
[3, 7, 11, 15]
[3, 7, 11, 15, 19, 23]
[3, 7, 11, 15, 19]
[4, 8, 12, 16]
Start at 3 and add 4: 3, 7, 11, 15, 19 (19 is less than 20).
What is list(range(20, 3, -5))?
[20, 15, 10, 5, 0]
[20, 15, 10]
[]
[20, 15, 10, 5]
Count down by 5 from 20 while staying above 3: 20, 15, 10, 5.
What is len(range(3, 15))?
11
12
15
13
With step 1, the number of values in range(a, b) is b โ a = 15 โ 3 = 12.
Which range should be used to print the numbers 1 to 10?
range(1, 11)
range(10)
range(1, 10)
range(0, 10)
The stop value is excluded, so range(1, 11) gives 1 to 10.
What happens with range(1, 10, 0)?
It gives [1]
It gives 1 forever
ValueError โ the step cannot be 0
It gives []
A step of 0 raises a ValueError.
What happens with range(1.5, 5)?
It gives 1.5, 2.5, 3.5, 4.5
It gives []
It gives 1, 2, 3, 4
TypeError โ range() works only with integers
range() accepts only integers; a float argument gives a TypeError.
The for loop is described as a:
Conditional loop
Counting loop that repeats once for each item in a sequence
Decision statement
Loop that never ends
The for loop repeats its block once for each item of a sequence such as a range, string or list.
What is the output of this code?
for ch in "PYTHON": print(ch, end="-")
P-Y-T-H-O-N-
P Y T H O N
P-Y-T-H-O-N
PYTHON-
Each character is followed by '-' (the end value), including the last one.
What is the output of this code?
for i in range(3): print(i * i, end=" ")
1 4 9
0 2 4
0 1 4
0 1 2
i takes the values 0, 1, 2, so the squares are 0, 1, 4.
What does the factorial program print for input 6?
fact = 1 for i in range(1, n + 1): fact *= i
5040
36
120
720
6! = 1 ร 2 ร 3 ร 4 ร 5 ร 6 = 720.
When is a while loop preferred over a for loop?
Never โ they are identical
When the number of repetitions is not known in advance
Only for printing tables
When the number of repetitions is known
A while loop repeats as long as a condition is True, which suits cases where the count is unknown (such as summing digits).
What are the four parts of a while loop?
Initialisation, test condition, body, update
Input, output, process, storage
if, elif, else, pass
Start, stop, step, end
A while loop needs initialisation, a test condition (checked before each iteration), a body and an update.
What does the sum-of-digits program print for 4721?
while num > 0: total += num % 10 num = num // 10
16
13
14
4721
The digits 1, 2, 7 and 4 are added: 1 + 2 + 7 + 4 = 14.
In a digit-processing while loop, what does num % 10 give?
The number without its last digit
The first digit
The number of digits
The last digit
num % 10 gives the last digit; num // 10 removes the last digit.
The palindrome program reverses a number with reverse = reverse * 10 + num % 10. For which input does it print 'is a palindrome'?
1122
1221
1234
1210
1221 reversed is 1221 โ the same โ so it is a palindrome.
What happens with this code?
count = 1 while count <= 5: print(count)
It prints 1 forever โ an infinite loop, because count is never updated
SyntaxError
It prints nothing
It prints 1 to 5
Without count += 1, the condition never becomes False.
Which key combination stops an infinite loop in IDLE?
Alt + F4
F5
Ctrl + C
Ctrl + S
Pressing Ctrl + C interrupts a running program in IDLE.
What is printed after this code?
x = 10 count = 0 while x > 0: x = x - 3 count += 1 print(x, count)
-2 3
1 3
0 4
-2 4
x goes 10 โ 7 โ 4 โ 1 โ -2, so the loop runs 4 times.
What is printed after this code?
x = 1 while x < 20: x *= 2 print(x)
20
32
16
64
x goes 1 โ 2 โ 4 โ 8 โ 16 โ 32; at 32 the condition x < 20 is False, so 32 is printed.
Which statement about for and while loops is correct?
A while loop needs its loop variable updated manually; a for loop takes the next item automatically
A for loop cannot use range()
A while loop is a counting loop
A for loop has a high risk of becoming infinite
for updates its variable automatically (low infinite-loop risk); while needs a manual update (higher risk).
Which for loop is equivalent to this code?
i = 5 while i <= 25: print(i) i += 5
for i in range(5, 30):
for i in range(0, 25, 5):
for i in range(5, 26, 5):
for i in range(5, 25, 5):
The loop prints 5, 10, 15, 20, 25, so the stop must be past 25, e.g. range(5, 26, 5).
What does break do inside a loop?
Restarts the loop
Pauses the program
Skips the current iteration only
Immediately terminates the loop; control moves to the statement after the loop
break exits the loop completely.
What does continue do inside a loop?
Repeats the same iteration
Skips the rest of the current iteration and moves to the next iteration
Ends the program
Terminates the loop
continue skips the remaining statements of the current iteration only.
What is the output of this code?
for i in range(1, 8): if i == 4: break print(i, end=" ")
1 2 3
4
1 2 3 4
1 2 3 5 6 7
When i is 4 the loop ends, so only 1 2 3 are printed.
What is the output of this code?
for i in range(1, 8): if i == 4: continue print(i, end=" ")
1 2 3 4 5 6 7
5 6 7
1 2 3 5 6 7
1 2 3
Only i = 4 is skipped; the loop carries on with 5, 6 and 7.
In the prime-check program, why is break used after finding a factor?
break is required in every for loop
To restart the loop
To print all factors
Because once a factor is found the number cannot be prime, so there is no need to check further
One factor is enough to show the number is not prime, so break stops unnecessary checks.
When does the else block attached to a loop run?
Only when the loop condition is False at the start
When the loop ends normally, without a break
Every time the loop body runs
When the if inside the loop is False
The loop else runs whenever the loop finishes without break โ even if the body ran zero times.
What does prime_with_else print for input 17?
for d in range(2, num): if num % d == 0: print(num, "is not prime") break else: print(num, "is prime")
17 is prime
Nothing
17 is not prime
Both messages
No divisor is found, so break never runs and the loop's else prints '17 is prime'.
What is the output of this code?
i = 1 while i <= 3: print(i, end=" ") i += 1 else: print("-> loop over")
1 2 3
1 2 3 4 -> loop over
1 2 3 -> loop over
-> loop over
The loop ends normally when i becomes 4, so the else block also runs.
What is the output of this code?
for i in range(1, 10): if i % 3 == 0: continue if i == 8: break print(i, end=" ") else: print("Done") print("End")
1 2 4 5 7 Done End
1 2 3 4 5 6 7 End
1 2 4 5 7 8 End
1 2 4 5 7 End
3 and 6 are skipped by continue; at i = 8 the loop breaks, so the loop's else (Done) does not run.
In a nested loop, how many times does the inner loop run for each iteration of the outer loop?
Never
Completely (all of its iterations)
Only on the last outer iteration
Once
For every iteration of the outer loop, the inner loop runs completely.
How many lines are printed by this code?
for i in range(1, 4): for j in range(1, 3): print(i, j)
6
3
5
9
The outer loop runs 3 times and the inner loop 2 times each: 3 ร 2 = 6.
How many times does the inner print run?
for i in range(4): for j in range(3): print("*")
7
4
12
3
When the inner count does not depend on the outer variable, total = outer ร inner = 4 ร 3 = 12.
In pattern programs, what does the outer loop usually control?
The colours
The end parameter
The columns
The rows
The outer loop controls the rows; the inner loop controls what is printed in each row.
What is the last line printed by this code?
for i in range(1, 5): for j in range(1, i + 1): print(j, end=" ") print()
1 1 1 1
1 2 3 4
4 3 2 1
4 4 4 4
Row i prints the numbers 1 to i, so the last row (i = 4) is 1 2 3 4.
What is the first line printed by this code?
for i in range(4, 0, -1): print("*" * i)
**
*
The first value of i is 4, so "*" * 4 gives ****; the lines get shorter after that.
What is the output of this code?
for a in range(3): for b in range(a, 3): print(a + b, end=" ") print()
0 1 2 / 2 3 4 / 4
0 1 2 / 1 2 3 / 2 3 4
0 1 2 / 2 3 / 4
0 / 2 / 4
a = 0: b = 0, 1, 2 gives 0 1 2; a = 1: b = 1, 2 gives 2 3; a = 2: b = 2 gives 4.
What does the Fibonacci program print for 8 terms?
a, b = 0, 1 for i in range(n): print(a, end=" ") a, b = b, a + b
0 1 1 2 3 5 8
1 1 2 3 5 8 13 21
0 1 2 3 5 8 13 21
0 1 1 2 3 5 8 13
Each term is the sum of the previous two, starting from 0 and 1: 0 1 1 2 3 5 8 13.
Which line has an error?
if n > 0 print(n)
n must be in quotes
The colon is missing after 'if n > 0'
There is no error
print must be in capitals
Every header (if, for, while, else) must end with a colon; otherwise a SyntaxError occurs.