Playing as a guest? to earn XP and track your progress.
๐ Flow of Control Quiz
๐ Flow of Control
The order in which statements of a program are executed is called:
Flow of control
Debugging
Indentation
Data flow
Flow of control decides which statement runs next.
What are the three basic kinds of flow of control?
Input, process and output
if, for and while only
Sequence, selection and iteration
Syntax, runtime and logic
Statements run one after another (sequence), choose a path (selection) or repeat (iteration).
Statements running one after another from top to bottom is called:
Sequence
Recursion
Selection
Iteration
Sequence is the default flow โ line by line.
Choosing which block to run based on a condition is called:
Sequence
Repetition
Selection
Iteration
if statements are used for selection (decision making).
Repeating a set of statements is called:
Selection
Indentation
Iteration
Sequence
Loops (for and while) are used for iteration.
A line that starts a block (such as an if or a for) must end with:
A comma ,
A semicolon ;
A colon :
A full stop .
if, elif, else, for and while lines end with a colon.
Which statement does nothing and is used as a placeholder for an empty block?
break
pass
None
continue
pass is the empty statement in Python.
What is the output of:
x = 5
if x > 10:
print("Big", end=" ")
print("Done")Big Done
Error
Done
Big
x > 10 is False, so the if block is skipped; Done is outside the if and always prints.
What is the output of:
x = 4
if x % 2 == 0:
print("Even")
else:
print("Odd")Even Odd
Odd
Error
Even
4 % 2 is 0, so the if block runs.
In an ifโelifโelse ladder, how many blocks run?
Only the else block
Only the first block whose condition is True (or else)
All blocks with a True condition
All blocks
Python checks conditions top to bottom and runs only the first True block.
What is the output of:
m = 75
if m >= 90:
print("A")
elif m >= 60:
print("B")
elif m >= 40:
print("C")
else:
print("D")B C
B
A
C
75 >= 90 is False, 75 >= 60 is True, so B prints and the rest are skipped.
Which keyword is short for 'else if' in Python?
ifelse
elif
else if
elseif
Python uses elif.
Can an if statement be written without an else part?
No, else is compulsory
Only with elif
Only inside a loop
Yes, else is optional
A simple if has no else.
An if statement written inside another if statement is called a:
Loop
Double if
Nested if
Ladder if
A nested if is used when one decision depends on another.
What is the output of:
a = 5
if a > 0:
if a > 10:
print("X")
else:
print("Y")X Y
X
Y
Nothing
a > 0 is True; inside it, a > 10 is False, so the inner else prints Y.
What error is given by:
if 5 > 2
print("Yes")SyntaxError
TypeError
No error
NameError
The colon after the condition is missing.
What is the output of:
print(list(range(5)))[0, 1, 2, 3, 4]
[1, 2, 3, 4, 5]
[5]
[0, 1, 2, 3, 4, 5]
range(5) starts at 0 and stops before 5.
What is the output of:
print(list(range(2, 6)))[2, 6]
[3, 4, 5, 6]
[2, 3, 4, 5, 6]
[2, 3, 4, 5]
range(start, stop) includes start and excludes stop.
What is the output of:
print(list(range(1, 10, 3)))[1, 4, 7, 10]
[3, 6, 9]
[1, 4, 7]
[1, 3, 9]
Start at 1 and jump by 3, stopping before 10.
What is the output of:
print(list(range(10, 0, -2)))[]
[10, 8, 6, 4, 2, 0]
[8, 6, 4, 2, 0]
[10, 8, 6, 4, 2]
A negative step counts down; 0 is the stop value and is not included.
What is the output of:
print(list(range(5, 1)))[5, 4, 3, 2]
[]
Error
[1, 2, 3, 4, 5]
With the default step +1, you can't go from 5 up to 1, so the range is empty.
In range(start, stop, step), which value is NEVER included?
stop
0
start
step
The sequence always stops before the stop value.
What is the output of:
for i in range(3):
print(i, end=" ")3
0 1 2
1 2 3
0 1 2 3
range(3) gives 0, 1, 2.
What is the output of:
for ch in "CAT":
print(ch, end="-")C-A-T
Error
C-A-T-
CAT-
A for loop can go through each character of a string.
How many times does this loop run?
for i in range(1, 11):
print(i)1
9
10
11
It runs for i = 1 to 10.
What is the output of:
s = 0
for i in range(1, 5):
s = s + i
print(s)5
10
4
15
1 + 2 + 3 + 4 = 10.
What is the output of:
f = 1
for i in range(1, 5):
f = f * i
print(f)120
24
10
0
1 ร 2 ร 3 ร 4 = 24.
When finding a factorial, why do we start with fact = 1 and not fact = 0?
It makes no difference
Anything multiplied by 0 stays 0
Python does not allow 0
1 is faster
Product accumulators start at 1; sum accumulators start at 0.
Which loop is best when the number of repetitions is known in advance?
if statement
pass statement
for loop
while loop
A for loop with range() is used when you know how many times to repeat.
Which loop repeats as long as a condition is True?
for loop
while loop
elif
ifโelse
A while loop keeps running while its condition is True.
What is the output of:
i = 1
while i <= 3:
print(i, end=" ")
i += 11 2 3 4
0 1 2 3
1 2 3
1
The loop runs while i is 1, 2 and 3.
What happens in this code?
i = 1
while i <= 5:
print(i)It runs forever (infinite loop)
SyntaxError
It prints nothing
It prints 1 to 5
i is never updated, so i <= 5 is always True.
The three steps needed for a correct while loop are:
Initialise, test the condition, update the variable
Start, break, continue
Declare, compile, run
Import, print, stop
Give the variable a starting value, test it in while, and change it inside the loop.
What is the output of:
n = 10
while n > 0:
n = n - 3
print(n)-3
-2
1
0
n goes 10 โ 7 โ 4 โ 1 โ โ2; then n > 0 is False.
What is the output of:
n = 123
s = 0
while n > 0:
s = s + n % 10
n = n // 10
print(s)6
321
123
3
n % 10 takes the last digit and n // 10 removes it: 3 + 2 + 1 = 6.
Which expression gives the LAST digit of a number n?
n // 10
n % 10
n * 10
n / 10
The remainder after dividing by 10 is the last digit.
Which statement immediately exits the loop?
exit loop
break
pass
continue
break stops the loop completely.
Which statement skips the rest of the current round and goes to the next round?
continue
pass
return
break
continue jumps to the next iteration.
What is the output of:
for i in range(1, 6):
if i == 3:
break
print(i, end=" ")1 2 3
3
1 2
1 2 4 5
When i becomes 3, the loop stops.
What is the output of:
for i in range(1, 6):
if i == 3:
continue
print(i, end=" ")1 2 3 4 5
1 2
3
1 2 4 5
continue skips only the round where i is 3.
What is the output of:
for i in range(1, 10):
if i % 4 == 0:
break
print(i)4
9
8
10
The loop breaks when i is 4, and i keeps that value.
A loop inside another loop is called a:
Jump loop
Infinite loop
Nested loop
Double loop
In a nested loop, the inner loop runs completely for each round of the outer loop.
How many times will "Hi" be printed?
for i in range(3):
for j in range(4):
print("Hi")3
12
7
4
Total runs = outer ร inner = 3 ร 4 = 12.
What is the output of:
for i in range(1, 4):
print("*" * i, end=" ")"*" * i repeats the star i times.
What is the output of:
for i in range(1, 3):
for j in range(1, 3):
print(i * j, end=" ")1 2 3 4
1 1 2 2
1 2 2 4
2 4
i=1: 1, 2; i=2: 2, 4.
Which code prints the multiplication table of 5 (5 to 50)?
for i in range(1, 11): print(5 + i)
for i in range(5): print(5 * i)
for i in range(1, 11): print(5 * i)
for i in range(1, 10): print(5 * i)
range(1, 11) gives 1 to 10, so 5 ร 1 to 5 ร 10 are printed.
What is the output of:
x = 0
if x:
print("Yes")
else:
print("No")0
Error
Yes
No
0 is treated as False in a condition.
What is the output of:
for i in range(3):
pass
print(i)2
0
Error
3
pass does nothing; after the loop, i holds its last value, 2.
What is the output of:
count = 0
for ch in "banana":
if ch == "a":
count += 1
print(count)2
1
3
6
"banana" has three a's.
What is the output of:
for i in range(5, 0, -1):
print(i, end="")5
12345
543210
54321
The loop counts down from 5 to 1.
What is the output of:
n = 9
is_prime = True
for i in range(2, n):
if n % i == 0:
is_prime = False
break
print(is_prime, i)True 3
True 8
False 9
False 3
9 % 3 == 0, so is_prime becomes False and the loop breaks with i = 3.
A for loop vs a while loop โ which statement is TRUE?
while can only count up to 10
for is used when the number of rounds is known; while when it depends on a condition
for cannot use range()
Both need break to stop
Use for with a known count and while when you repeat until a condition changes.
What error does this code give?
x = 8
if x > 5:
print(x)No error
NameError
IndentationError
SyntaxError: missing colon
The statement inside the if block must be indented.
What is the output of:
total = 0
for i in range(2, 11, 2):
total += i
print(total)20
25
30
55
2 + 4 + 6 + 8 + 10 = 30.
What is the output of:
i = 5
while i > 5:
print(i)
print("End")5
5 End
End
Infinite loop
The condition is False at the start, so the loop body never runs.
Which value of age makes this print "Teen"?
if age >= 13 and age <= 19:
print("Teen")25
15
12
20
Both conditions must be True; only 15 is between 13 and 19.