AlgoMaster Logo

Control Flow - Quiz

Last Updated: January 3, 2026

Control Flow Exercises

31 quizzes

1
Code Completion

Complete the expression to check if access_level is not equal to 0 before printing a message

python
1
print(access_level 0)

Click an option to fill the blank:

2
Code Completion

Complete the expression to iterate over numbers 0 through 4 when building a list of squares

python
1
squares = [n * n for n in (5)]

Click an option to fill the blank:

3
Code Completion

Complete the expression to choose 'adult' when age is 18 or more, otherwise 'minor'

python
1
status = 'adult' if age >= 18 'minor'

Click an option to fill the blank:

4
Multiple Choice

You want to run a block of code only when a condition is false. Which structure is most suitable?

5
Multiple Choice

Which statement best describes an elif clause?

6
Multiple Choice

When is a ternary (conditional) expression most appropriate?

7
Multiple Choice

What does this loop do? for name in users: print(name)

8
Multiple Choice

Which while loop condition is most likely to create an infinite loop if count is not changed inside the loop?

9
Multiple Choice

In a for loop, what does the continue statement do?

10
Multiple Choice

When will the else block of a for loop execute?

11
Multiple Choice

What is a common use case for nested loops?

12
Multiple Choice

In match-case, what does the underscore (_) pattern represent?

13
Multiple Choice

You want to validate input until it is correct. Which loop is typically more appropriate?

14
Multiple Choice

What happens when break is executed inside a while loop that has an else clause?

15
Sequencing

Order the steps to use a while loop that counts down from 3 and then prints 'Go!'.

Drag and drop to reorder, or use the arrows.

16
Sequencing

Order the steps to use match-case to handle different HTTP status codes.

Drag and drop to reorder, or use the arrows.

17
Output Prediction

What is the output of this code? status = 'member' message = 'Welcome' if status == 'member' else 'Please sign up' print(message)

1status = 'member'
2message = 'Welcome' if status == 'member' else 'Please sign up'
3print(message)
18
Output Prediction

What is the output of this code? level = 5 if level > 10: result = 'high' else: result = 'low' print(result)

1level = 5
2if level > 10:
3    result = 'high'
4else:
5    result = 'low'
6print(result)
19
Output Prediction

What is the output of this code? items = [1, 3, 5] for number in items: if number % 2 == 0: print('even') break else: print('no even numbers')

1items = [1, 3, 5]
2for number in items:
3    if number % 2 == 0:
4        print('even')
5        break
6else:
7    print('no even numbers')
20
Output Prediction

What is the output of this code? matrix = [[1, 2], [3, 4]] product = 1 for row in matrix: for value in row: product *= value print(product)

1matrix = [[1, 2], [3, 4]]
2product = 1
3for row in matrix:
4    for value in row:
5        product *= value
6print(product)
21
Output Prediction

What is the output of this code? value = 'hello' match value: case 'hi': msg = 'short greeting' case 'hello': msg = 'longer greeting' case _: msg = 'unknown' print(msg)

1value = 'hello'
2match value:
3    case 'hi':
4        msg = 'short greeting'
5    case 'hello':
6        msg = 'longer greeting'
7    case _:
8        msg = 'unknown'
9print(msg)
22
Bug Spotting

Find the bug in this code that searches for a username using a for-else loop.

Click on the line(s) that contain the bug.

python
1
def find_user(target, users):
2
    for user in users:
3
        if user == target:
4
            return True
5
        break
6
    else:
7
        return False
23
Bug Spotting

Find the bug in this code that should count down using a while loop.

Click on the line(s) that contain the bug.

python
1
def countdown(start):
2
    value = start
3
    while value >= 0:
4
        print(value)
5
        value + 1
6
 
24
Matching

Match each keyword with its effect on loop execution.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

25
Matching

Match each control-flow construct with a typical use case.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

26
Fill in the Blanks

Complete the code to use a for-else loop that reports when a discount code is not found.

python
1
def has_discount(search_code, all_codes):
2
for discount in all_codes:
3
if discount == search_code:
4
return
5
else:
6
return

Click an option to fill blank 1:

27
Fill in the Blanks

Complete the code to build a multiplication table using nested loops.

python
1
def multiplication_table(limit):
2
for row in :
3
for column in range(1, limit + 1):
4
print(row * column, end=' ')
5

Click an option to fill blank 1:

28
Fill in the Blanks

Complete the code so that a while loop keeps asking until a non-empty name is provided.

python
1
def ask_name(get_input):
2
customer_name = ''
3
while :
4
customer_name = get_input()
5
return

Click an option to fill blank 1:

29
Fill in the Blanks

Complete the code to choose a label with match-case based on a priority number.

python
1
def label_priority(priority):
2
match priority:
3
case 1:
4
return 'high'
5
case 2 | 3:
6
return
7
case _:
8
return

Click an option to fill blank 1:

30
Hotspot Selection

Click the line that prevents this while loop from ever stopping.

Click on the line to select.

python
1
counter = 3
2
while counter > 0:
3
    print(counter)
4
    # missing update to counter here
31
Hotspot Selection

Click the line where the loop should skip processing negative scores.

Click on the line to select.

python
1
scores = [10, -3, 7]
2
for score in scores:
3
    if score < 0:
4
        continue
5
    print('Valid:', score)