Decorators Questions and Answers
Practice ModeShowing 10 of 25 questions
Q1
What is the primary purpose of a decorator in Python?
Answer: Option B
Explanation: Decorators allow you to modify or extend the behavior of functions without permanently modifying the function itself.
Q2
Which symbol is used to denote a decorator in Python?
Answer: Option B
Explanation: The @ symbol is used immediately before the function definition to apply a decorator.
Q3
What is the correct syntax for a basic decorator?
Answer: Option D
Explanation: A decorator function takes a function as argument and returns a wrapper function.
Q4
Which module provides built-in decorators like @staticmethod and @classmethod?
Answer: Option B
Explanation: The functools module provides decorators like @staticmethod and @classmethod for method decoration.
Q5
What will be the output of a decorated function if the decorator does not return anything?
Answer: Option B
Explanation: If a decorator does not return a function, the original function becomes None.
Q6
Can a function have multiple decorators?
Answer: Option B
Explanation: Yes, multiple decorators can be stacked and are applied from bottom to top.
Q7
What is a common use case for decorators?
Answer: Option B
Explanation: Decorators are commonly used for logging, timing, authentication, and other cross-cutting concerns.
Q8
Which decorator is used to cache function results?
Answer: Option B
Explanation: @lru_cache from functools module is used to cache function results for optimization.
Q9
What does @staticmethod decorator do?
Answer: Option A
Explanation: @staticmethod creates a static method that does not receive the class or instance as first argument.
Q10
How does @classmethod differ from @staticmethod?
Answer: Option A
Explanation: @classmethod receives the class as first argument, while @staticmethod does not.