AlgoMaster Logo

Functions - Quiz

Last Updated: January 3, 2026

Functions Exercises

31 quizzes

1
Code Completion

Complete the code so the sum of all values in prices is displayed to the user.

python
1
print(sum(prices) 2)

Click an option to fill the blank:

2
Code Completion

Complete the code so the function full_name is passed as a callback to sorted for custom sorting.

python
1
sorted(users, key=)

Click an option to fill the blank:

3
Code Completion

Complete the code so the lambda doubles every element in data using map.

python
1
result = list(map(, data))

Click an option to fill the blank:

4
Multiple Choice

Which statement best describes a Python function?

5
Multiple Choice

What happens if a Python function has no return statement?

6
Multiple Choice

Given def area(length, width): return length * width, what are length and width called?

7
Multiple Choice

Which call correctly uses keyword arguments for def connect(host, port, secure=False)?

8
Multiple Choice

What is the main benefit of using *args in a function definition?

9
Multiple Choice

Which statement about **kwargs is true?

10
Multiple Choice

What is a key limitation of lambda functions compared to def-defined functions?

11
Multiple Choice

In Python's LEGB rule, which scope is searched first when looking up a variable name?

12
Multiple Choice

Which keyword allows a nested function to reassign a variable from its enclosing (non-global) scope?

13
Multiple Choice

What is the main purpose of a decorator in Python?

14
Multiple Choice

Which functools tool is most suitable for caching expensive function results?

15
Sequencing

Order the steps to create and use a recursive function that sums a list of numbers.

Drag and drop to reorder, or use the arrows.

16
Sequencing

Order the steps to build a closure that counts how many times it has been called.

Drag and drop to reorder, or use the arrows.

17
Output Prediction

What is the output of this code using default arguments?

1def greet(user='Guest'):
2    return f'Hello, {user}!'
3
4message = greet()
5print(message)
18
Output Prediction

What is the output of this code involving *args?

1def multiply_all(*values):
2    result = 1
3    for number in values:
4        result *= number
5    return result
6
7print(multiply_all(2, 3, 4))
19
Output Prediction

What is the output of this recursion example?

1def countdown(n):
2    if n == 0:
3        return 'Go!'
4    return countdown(n - 1)
5
6print(countdown(3))
20
Output Prediction

What is the output of this closure example?

1def make_adder(x):
2    def adder(y):
3        return x + y
4    return adder
5
6add_ten = make_adder(10)
7print(add_ten(3))
21
Output Prediction

What will this decorator example print?

1def trace(func):
2    def wrapper(*args, **kwargs):
3        result = func(*args, **kwargs)
4        return result
5    return wrapper
6
7@trace
8def square(value):
9    return value * value
10
11print(square(4))
22
Bug Spotting

This function should use a global counter to track how many times it was called. Find the bug.

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

python
1
count_calls = 0
2
 
3
def log_call():
4
    count_calls += 1
5
    return count_calls
23
Bug Spotting

This code tries to build a list of callbacks that remember their index. Find the bug.

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

python
1
def make_callbacks():
2
    callbacks = []
3
    for index in range(3):
4
        def cb():
5
            return index
6
        callbacks.append(cb)
7
    return callbacks
24
Matching

Match each recursion term with its role.

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

25
Matching

Match each functools tool with its primary use.

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 build a decorator that times how long a function takes and prints the duration.

python
1
import time
2
3
def timed(action):
4
def wrapper(*values, **options):
5
start = ()
6
result = action(*values, **options)
7
end = time.time()
8
print('Elapsed:', - start)
9
return result
10
return wrapper

Click an option to fill blank 1:

27
Fill in the Blanks

Complete the code so the factory function returns a closure that applies a percentage discount.

python
1
def make_discount(rate):
2
def apply(price):
3
return price * (1 - )
4
return

Click an option to fill blank 1:

28
Fill in the Blanks

Complete the code so this recursive function computes the nth Fibonacci number with caching.

python
1
from functools import lru_cache
2
3
@lru_cache(maxsize=None)
4
def fib(position):
5
if position < 2:
6
return position
7
return fib(position - 1) +

Click an option to fill blank 1:

29
Fill in the Blanks

Complete the code so the function describe_user accepts any keyword details and shows how many were provided.

python
1
def describe_user(username, **details):
2
info = f'User: {username}'
3
count = len()
4
return info + f' (fields: {count})'

Click an option to fill blank 1:

30
Hotspot Selection

Click the line that uses a global variable inside the function.

Click on the line to select.

python
1
total_requests = 0
2
 
3
def record_request():
4
    global total_requests
5
    total_requests += 1
6
    return total_requests
31
Hotspot Selection

Click the line where a nested function accesses a variable from its enclosing scope.

Click on the line to select.

python
1
def outer_task():
2
    task_name = 'backup'
3
    def inner_task():
4
        return f'Running {task_name}'
5
    return inner_task()