LearnFrenzy
Sign in Take a free test

Decorators · Exercise 1 of 4

Decorators Decorators MCQ

  • 15 questions
  • Page 1 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Fifteen multiple-choice questions on Python decorators: the @ syntax, wrappers, functools.wraps, when decorators run, arguments, stacking, closures and built-in decorators. Pick an option to check your answer instantly.

Question 1 Easy

@deco written above def f(): … is equivalent to which statement?

A deco = f(deco)
B f = deco(f)
C f = deco
D deco(f) is called on every call without rebinding f
Question page & discussion
Question 2 Easy

Which description fits a decorator?

A A comment that documents a function
B A keyword that makes a function faster
C A class that every function inherits from
D A callable that takes a function and returns a (usually wrapped) function
Question page & discussion
Question 3 Medium

Why should a wrapper be written as def wrapper(*args, **kwargs)?

A To make the wrapper faster
B Because decorators cannot take named parameters
C To prevent the function from being called
D So it can accept and pass on any arguments, whatever the decorated function's signature
Question page & discussion
Question 4 Medium

What problem does @functools.wraps(func) solve?

A It makes the decorator run only once
B It catches exceptions inside the wrapper
C It allows decorators to take arguments
D Without it, the decorated function reports the wrapper's __name__ and loses its docstring
Question page & discussion
Question 5 Medium

When does the code in a decorator's body (outside the wrapper) run?

A Every time the decorated function is called
B Only when the program ends
C Once, when the decorated function is defined
D Never
Question page & discussion
Question 6 Hard

How many nested function levels does a decorator used as @retry(times=3) typically need?

A One
B Three: one for the arguments, one for the function, one for the wrapper
C Two: the decorator and the wrapper
D It is impossible to pass arguments to decorators
Question page & discussion
Question 7 Medium

What is @a / @b / def f(): … equivalent to?

A f = a(b(f))
B f = b(a(f))
C f = a(f); f = b(f) in parallel
D f = (a + b)(f)
Question page & discussion
Question 8 Medium

A decorator defines a wrapper but has no return wrapper. What happens when the decorated function is called?

A The original function runs
B The wrapper runs anyway
C TypeError: 'NoneType' object is not callable
D SyntaxError
Question page & discussion
Question 9 Medium

How does the wrapper still know which function to call after the decorator has returned?

A func is a global variable
B Python passes func again on every call
C It is a closure: it remembers func from the enclosing decorator's scope
D It looks func up by name in the module
Question page & discussion
Question 10 Medium

In a class used as a decorator, which method runs when the decorated function is called?

A __init__
B __call__
C __new__
D __enter__
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links