Decorators Questions and Answers

Practice Mode
Showing 10 of 25 questions
Q21
Which decorator is used to mark abstract methods?
  • A @abstract
  • B @abstractmethod
  • C @virtual
  • D @must_implement
Answer: Option B
Explanation: @abstractmethod from abc module is used to define abstract methods in abstract base classes.
Q22
Can decorators modify function return values?
  • A No, only arguments
  • B Yes, both arguments and return values
  • C No, neither
  • D Only return values, not arguments
Answer: Option B
Explanation: Yes, decorators can modify both function arguments and return values.
Q23
What is the main advantage of using decorators?
  • A Faster execution
  • B Code reuse and separation of concerns
  • C Smaller code size
  • D Automatic documentation
Answer: Option B
Explanation: Decorators promote code reuse and separation of concerns by modularizing cross-cutting functionality.
Q24
Which decorator would you use to time function execution?
  • A @timeit
  • B @timer
  • C @timing
  • D Custom decorator using time module
Answer: Option D
Explanation: A custom timing decorator can be created using time module to measure function execution time.
Q25
Can decorators be chained with inheritance?
  • A No, inheritance breaks decorators
  • B Yes, they work together
  • C Only with multiple inheritance
  • D Only with single inheritance
Answer: Option B
Explanation: Yes, decorators work with inheritance - decorated methods in parent classes affect child classes.
Questions and Answers for Competitive Exams Various Entrance Test