Decorators Questions and Answers

Practice Mode
Showing 10 of 25 questions
Q1
What is the primary purpose of a decorator in Python?
  • A To add new methods to a class
  • B To modify function behavior without changing its code
  • C To create new variables
  • D To handle exceptions
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?
  • A #
  • B @
  • C $
  • D &
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?
  • A def decorator(func): return func
  • B def decorator(func): return wrapper
  • C @decorator def function(): pass
  • D Both 2 and 3
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?
  • A decorator
  • B functools
  • C builtins
  • D inspect
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?
  • A The original function output
  • B None
  • C Error
  • D The decorator function output
Answer: Option B
Explanation: If a decorator does not return a function, the original function becomes None.
Q6
Can a function have multiple decorators?
  • A Yes, applied in order from top to bottom
  • B Yes, applied in order from bottom to top
  • C No, only one decorator per function
  • D Only if they are different types
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?
  • A Memory management
  • B Logging and timing functions
  • C Variable declaration
  • D Loop optimization
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?
  • A @cache
  • B @lru_cache
  • C @memoize
  • D @remember
Answer: Option B
Explanation: @lru_cache from functools module is used to cache function results for optimization.
Q9
What does @staticmethod decorator do?
  • A Makes a method accessible without class instance
  • B Prevents method from being overridden
  • C Makes method private
  • D Forces method to be called from class only
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?
  • A classmethod takes class reference, staticmethod takes nothing
  • B No difference
  • C classmethod is faster
  • D staticmethod can access class variables
Answer: Option A
Explanation: @classmethod receives the class as first argument, while @staticmethod does not.
Questions and Answers for Competitive Exams Various Entrance Test