Decorators Questions with Answers

5 practice questions on Decorators (Python) with answers and step-by-step solutions. Try 5 samples below, then practise all 5 free in the AptiNest app.

Sample questions

Question 1Easy

In Python, what is a decorator?

  1. A callable that takes a function and returns a replacement
  2. A special kind of class attribute shared by instances
  3. A keyword that makes a function private to its module
  4. A comment that documents what a function does

Answer: A. A callable that takes a function and returns a replacement

Explanation

@deco above def f is shorthand for f = deco(f). The decorator receives the function and returns what will be bound to the name.

Step-by-step solution

  1. @deco placed above def f is the same as f = deco(f).
  2. So a decorator is a callable that takes a function and returns a new object to use in its place.
  3. That matches option A, the correct answer.

Why the other options are wrong

  • B. A class attribute shared by instances is not a decorator.
  • C. Python has no keyword that makes a function private to its module.
  • D. A comment is ignored by Python, but a decorator runs code.

Tip: @deco is shorthand for f = deco(f).

Question 2Medium

What is the output?

def deco(f):
    def wrapper(*args):
        return f(*args)
    return wrapper

@deco
def greet():
    pass

print(greet.__name__)
  1. wrapper
  2. greet
  3. f
  4. deco

Answer: A. wrapper

Explanation

Without functools.wraps, the decorated name refers to wrapper and its metadata. Adding @functools.wraps(f) to wrapper copies __name__, __doc__ and other metadata.

Step-by-step solution

  1. The decorator returns wrapper, so the name greet now refers to wrapper.
  2. Wrapper keeps its own name, which is 'wrapper', because nothing copied the original name.
  3. So print shows wrapper, the correct answer.

Why the other options are wrong

  • B. Greet is the original name, but the decorated function now reports the wrapper's name.
  • C. F is only a local name inside deco, not the name of the function.
  • D. Deco is the decorator's name, not the name of the function.

Tip: Use @functools.wraps(f) on wrapper to keep the original name and docstring.

Question 3Medium

What is the output?

def repeat(n):
    def deco(f):
        def wrapper(*args):
            return [f(*args) for _ in range(n)]
        return wrapper
    return deco

@repeat(3)
def sq(x):
    return x * x

print(sq(2))
  1. [2, 2, 2]
  2. 12
  3. [4, 4, 4]
  4. 4

Answer: C. [4, 4, 4]

Explanation

repeat(3) returns the actual decorator, and the wrapper calls sq three times, collecting the results in a list.

Step-by-step solution

  1. repeat(3) returns deco; deco wraps sq.
  2. sq(2) calls wrapper, which runs sq 3 times.
  3. Each result is 4: [4, 4, 4].

Why the other options are wrong

  • A. f computes x*x, not x.
  • B. 3x4 not a sum; the result is a list.
  • D. The wrapper returns a list.

Question 4Medium

What is the output?

def a(f): return lambda: "a(" + f() + ")"
def b(f): return lambda: "b(" + f() + ")"

@a
@b
def g():
    return "g"

print(g())
  1. a(b(g))
  2. b(a(g))
  3. a(g)
  4. b(g)

Answer: A. a(b(g))

Explanation

Stacked decorators apply bottom-up: g = a(b(g)). Calling the result runs a's wrapper outermost.

Step-by-step solution

  1. Decorators apply from the bottom up: first b wraps g, then a wraps that result.
  2. So g = a(b(g)).
  3. Calling g() runs a's wrapper first, so the output is a(b(g)), the correct answer.

Why the other options are wrong

  • B. b(a(g)) is wrong; b is applied first, so b sits inside a.
  • C. a(g) is wrong; b is applied too, so it appears in the result.
  • D. b(g) is wrong; a is applied as well.

Tip: Stacked decorators apply from the bottom up, and the top one's wrapper runs first when the function is called.

Question 5Hard

What is the output?

def register(f):
    print("reg", f.__name__, end="; ")
    return f

@register
def task():
    print("task")

print("start")
  1. reg task; task; start
  2. start; reg task
  3. reg task; start
  4. start

Answer: C. reg task; start

Explanation

A decorator runs once, when the def statement is executed, not when the function is called. task() is never called here.

Step-by-step solution

  1. The decorator register runs when the def statement runs, before print('start').
  2. It prints 'reg task; ' and returns the function unchanged.
  3. Task is never called, so the output is 'reg task; start', the correct answer.

Why the other options are wrong

  • A. Task is printed only when task() is called, and it is not called here.
  • B. Start is printed after reg task, because the def runs first.
  • D. Reg task is printed first, by the decorator when the def runs.

Tip: A decorator runs once, when the def statement runs, not when the function is called.

Practise all 5 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Python topics · All topics