Answer key · 12 questions

Python functools Exercises With Answers

All 12 questions for Python functools in one place. Try the practice page first, then use this page to check the exact answer and the reason.

Quick answer

This answer key contains 4 multiple-choice, 4 fill-in-the-blank, and 4 output-prediction questions for Python functools, with the exact answer and a short explanation for every question.

The answers are shown in full, so use this page after making an attempt. The practice page is the better first pass.

01 · Multiple choice

What does lru_cache do?

  1. Sorts the arguments
  2. Caches results by argument valuescorrect
  3. Runs the function in parallel
  4. Deletes old functions

Answer: Caches results by argument values

Why: Repeated calls with the same arguments can reuse a stored result.

02 · Multiple choice

What does partial return?

  1. The result of calling the function
  2. A class
  3. A new callable with some arguments fixedcorrect
  4. A list of arguments

Answer: A new callable with some arguments fixed

Why: partial stores arguments and waits for the remaining ones.

03 · Multiple choice

What does reduce do?

  1. Sorts an iterable
  2. Creates a dictionary
  3. Folds an iterable into one valuecorrect
  4. Removes duplicates

Answer: Folds an iterable into one value

Why: reduce applies a two-argument function cumulatively.

04 · Multiple choice

What does this code print?

from functools import partial
def power(base, exponent):
    return base ** exponent
square = partial(power, exponent=2)
print(square(5))
  1. 10
  2. 5
  3. 25correct
  4. 7

Answer: 25

Why: partial fixes exponent to 2, so the call becomes power(5, 2).

05 · Fill in the blank

Complete the cache decorator import.

from functools import ____

Answer: lru_cache

Why: lru_cache is the memoization decorator in functools.

06 · Fill in the blank

Complete the function that fixes an argument.

from functools import partial
def power(base, exponent):
    return base ** exponent
square = ____(power, exponent=2)

Answer: partial

Why: partial creates a specialized callable.

07 · Fill in the blank

Complete the folding function.

from functools import reduce
print(____(lambda a, b: a + b, [1, 2, 3], 0))

Answer: reduce

Why: reduce combines values from left to right.

08 · Fill in the blank

Complete the cache size argument.

from functools import lru_cache
@lru_cache(____=None)
def square(n):
    return n * n

Answer: maxsize

Why: maxsize=None lets the cache grow without a fixed limit.

09 · Predict output

What does the program output?

from functools import lru_cache
@lru_cache(maxsize=None)
def f(n):
    return n * 2
print(f(2) + f(2))

Answer: 8

Why: Both calls return four, so the sum is eight; the second call reuses the cached result.

10 · Predict output

What appears on the screen?

from functools import partial
def add(a, b):
    return a + b
add5 = partial(add, 5)
print(add5(3))

Answer: 8

Why: partial fixes the first argument to five, so the result is eight.

11 · Predict output

What is printed?

from functools import reduce
print(reduce(lambda a, b: a * b, [2, 3, 4]))

Answer: 24

Why: The multiplication folds to twenty-four.

12 · Predict output

What is the output?

from functools import lru_cache
@lru_cache(maxsize=None)
def f(n):
    return n + 1
f(1)
f(1)
print(f.cache_info().hits)

Answer: 1

Why: The second call is a cache hit, so hits is one.

Try the questions first

Open the practice page for Python functools and answer the same 12 questions with instant feedback.

Practice this lesson