Decorators Questions and Answers
Python Programming - Decorators Questions with Answers help understand how functions can modify other functions’ behavior. These Python Decorators Questions are common in Infosys, Wipro, and Cognizant exams to test advanced Python concepts
Decorators
Showing 10 of
25 questions
1. What is the primary purpose of a decorator in Python?
- To add new methods to a class
- To modify function behavior without changing its code
- To create new variables
- To handle exceptions
3. What is the correct syntax for a basic decorator?
- def decorator(func): return func
- def decorator(func): return wrapper
- @decorator def function(): pass
- Both 2 and 3
4. Which module provides built-in decorators like @staticmethod and @classmethod?
- decorator
- functools
- builtins
- inspect
5. What will be the output of a decorated function if the decorator does not return anything?
- The original function output
- None
- Error
- The decorator function output
6. Can a function have multiple decorators?
- Yes, applied in order from top to bottom
- Yes, applied in order from bottom to top
- No, only one decorator per function
- Only if they are different types
7. What is a common use case for decorators?
- Memory management
- Logging and timing functions
- Variable declaration
- Loop optimization
8. Which decorator is used to cache function results?
- @cache
- @lru_cache
- @memoize
- @remember
9. What does @staticmethod decorator do?
- Makes a method accessible without class instance
- Prevents method from being overridden
- Makes method private
- Forces method to be called from class only
10. How does @classmethod differ from @staticmethod?
- classmethod takes class reference, staticmethod takes nothing
- No difference
- classmethod is faster
- staticmethod can access class variables