ComputerDecorators In Depth MCQs
Practice Decorators In Depth MCQs for competitive exams.
Decorators In Depth MCQs
Practice questions from this topic.
What is the purpose of the @dataclass decorator in Python?
- A. To create data visualization
- B. To add metadata to classes
- C. To create a class for data manipulation
- D. To simplify the creation of classes for storing data
Correct Answer: D
How does the @contextmanager decorator facilitate context management?
- A. By creating context managers automatically
- B. By adding new context to existing managers
- C. By wrapping functions with context management behavior
- D. By removing context from code
Correct Answer: C
What is the purpose of the @lru_cache decorator in Python?
- A. To limit the number of decorator applications
- B. To cache the last used decorator
- C. To cache function results with a limited size
- D. To cache only class methods
Correct Answer: C
What is a memoization decorator used for?
- A. To add logging to functions
- B. To modify function behavior
- C. To cache function results
- D. To validate function inputs
Correct Answer: C
How can you remove a decorator from a function in Python?
- A. By deleting the decorator's code
- B. By using the @remove_decorator syntax
- C. By using the @decorator syntax again
- D. By calling the remove_decorator() function
Correct Answer: C
What is the purpose of the functools.partial function in decorators?
- A. To create a partial function
- B. To replace built-in functions
- C. To preserve the original function's metadata
- D. To modify the decorator's behavior
Correct Answer: A
How do you create a custom decorator in Python?
- A. By modifying the built-in decorators
- B. By using the @custom_decorator syntax
- C. By importing decorators from external libraries
- D. By defining a function and using the @ symbol
Correct Answer: D
What is the purpose of the @abstractmethod decorator in Python?
- A. To create abstract classes
- B. To prevent method overriding
- C. To indicate that a method must be overridden in subclasses
- D. To define a method as static
Correct Answer: C
How is the @classmethod decorator applied to a method in a class?
- A. Using the @classmethod syntax above the class
- B. Using the @classmethod syntax above the method
- C. Using the @classmethod decorator within the method
- D. By using the apply_classmethod() function
Correct Answer: B
What is the purpose of the @classmethod decorator in Python?
- A. To define a class method
- B. To define an instance method
- C. To define a static method
- D. To define a constructor method
Correct Answer: A
How does the @classmethod decorator differ from @staticmethod?
- A. @classmethod can't access class attributes
- B. @staticmethod can't access instance attributes
- C. @classmethod can access class attributes
- D. @staticmethod can access instance attributes
Correct Answer: C
What is the role of the @staticmethod decorator in Python?
- A. To define static variables
- B. To make a method callable without an instance
- C. To add static typing to variables
- D. To prevent method overriding
Correct Answer: B