Decorators Questions and Answers
Practice ModeShowing 10 of 25 questions
Q11
Can decorators take arguments?
Answer: Option B
Explanation: Yes, decorators can take arguments by using an extra layer of function wrapping.
Q12
What is the purpose of functools.wraps in decorators?
Answer: Option B
Explanation: functools.wraps preserves the original function metadata like name and docstring.
Q13
Which of these is a valid decorator application?
Answer: Option A
Explanation: Decorators are applied using @ symbol before function definition.
Q14
What happens if a decorator returns a non-callable object?
Answer: Option B
Explanation: If decorator returns non-callable, calling the decorated function will raise TypeError.
Q15
Can decorators be applied to class methods?
Answer: Option B
Explanation: Yes, decorators can be applied to both functions and class methods.
Q16
What is a decorator factory?
Answer: Option B
Explanation: A decorator factory is a function that returns a decorator, allowing parameterized decorators.
Q17
Which decorator is used for method overloading?
Answer: Option B
Explanation: Python does not have built-in method overloading, but @singledispatch can be used for similar functionality.
Q18
What does @property decorator do?
Answer: Option B
Explanation: @property allows defining methods that can be accessed like attributes.
Q19
Can decorators access function arguments?
Answer: Option B
Explanation: Yes, wrapper functions in decorators can access and modify function arguments.
Q20
What is the execution order of multiple decorators?
Answer: Option B
Explanation: Multiple decorators execute from bottom to top when applied, but wrapper execution is top to bottom.