Decorators Questions and Answers

Practice Mode
Showing 10 of 25 questions
Q11
Can decorators take arguments?
  • A No, never
  • B Yes, with additional function layer
  • C Only string arguments
  • D Only numeric 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?
  • A Improves performance
  • B Preserves function metadata
  • C Handles exceptions
  • D Manages memory
Answer: Option B
Explanation: functools.wraps preserves the original function metadata like name and docstring.
Q13
Which of these is a valid decorator application?
  • A @decorator def my_func(): pass
  • B def my_func(): @decorator pass
  • C def @decorator my_func(): pass
  • D decorator @ def my_func(): pass
Answer: Option A
Explanation: Decorators are applied using @ symbol before function definition.
Q14
What happens if a decorator returns a non-callable object?
  • A Function works normally
  • B TypeError when calling function
  • C Function returns None
  • D SyntaxError
Answer: Option B
Explanation: If decorator returns non-callable, calling the decorated function will raise TypeError.
Q15
Can decorators be applied to class methods?
  • A No, only functions
  • B Yes, both functions and methods
  • C Only instance methods
  • D Only static methods
Answer: Option B
Explanation: Yes, decorators can be applied to both functions and class methods.
Q16
What is a decorator factory?
  • A A class that creates decorators
  • B A function that returns a decorator
  • C A special import statement
  • D A built-in decorator
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?
  • A @overload
  • B @singledispatch
  • C @multimethod
  • D @dispatch
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?
  • A Makes method private
  • B Allows method access like attribute
  • C Prevents attribute modification
  • D Makes method static
Answer: Option B
Explanation: @property allows defining methods that can be accessed like attributes.
Q19
Can decorators access function arguments?
  • A No, never
  • B Yes, through *args and **kwargs
  • C Only positional arguments
  • D Only keyword 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?
  • A Top to bottom
  • B Bottom to top
  • C Random order
  • D Alphabetical order
Answer: Option B
Explanation: Multiple decorators execute from bottom to top when applied, but wrapper execution is top to bottom.
Questions and Answers for Competitive Exams Various Entrance Test