Part 1: Basics and Syntax
Q 1. What is a decorator in Python?
A. A way to delete functions
B. A function that takes another function and extends its behavior without explicitly modifying it
C. A tool used to compile Python code into C++
D. A class method used for garbage collection
Show Answer
Answer:-B. A function that takes another function and extends its behavior without explicitly modifying itQ 2. Which symbol is used to apply a decorator to a function?
A. &
B. $
C. @
D. #
Show Answer
Answer:-C. @Q 3. What is the equivalent of the following code?
Python
@my_decorator
def my_func():
pass
A. my_func = my_decorator(my_func)
B. my_decorator = my_func(my_decorator)
C. my_func = my_decorator()
D. my_func()
Show Answer
Answer:-A. my_func = my_decorator(my_func)Q 4. Decorators are an application of which programming concept?
A. Inheritance
B. Encapsulation
C. Higher-order functions
D. Polymorphism
Show Answer
Answer:-C. Higher-order functionsQ 5. Can a single function have multiple decorators applied to it?
A. Yes
B. No
Show Answer
Answer:-A. YesPart 2: Inner Workings & Closures
Q 6. When multiple decorators are used, in what order are they executed?
A. Bottom-to-top (closest to the function first)
B. Top-to-bottom
C. Randomly
D. Alphabetically by name
Show Answer
Answer:-A. Bottom-to-top (closest to the function first)Q 7. What is the purpose of functools.wraps?
A. To make the code run faster
B. To preserve the metadata (like __name__ and __doc__) of the original function
C. To encrypt the function code
D. To convert the function into a class
Show Answer
Answer:-B. To preserve the metadata (like __name__ and __doc__) of the original functionQ 8. What does a decorator typically return?
A. The input function itself
B. A wrapper function or a modified function object
C. An integer
D. None
Show Answer
Answer:-B. A wrapper function or a modified function objectQ 9. What will the following code output?
Python
def dec(f):
def wrapper():
return f().upper()
return wrapper
@dec
def greet():
return "hello"
print(greet())
A. hello
B. HELLO
C. <function greet at …>
D. Error
Show Answer
Answer:-B. HELLOQ 10. What is a “Closure” in the context of decorators?
A. Closing a file after writing
B. An inner function that remembers the variables in its outer (enclosing) scope
C. The end of a Python script
D. A method to private-protect a class
Show Answer
Answer:-B. An inner function that remembers the variables in its outer (enclosing) scopePart 3: Arguments and Advanced Use
Q 11. How do you pass arguments to the decorated function inside the wrapper?
A. Using *args and **kwargs
B. You cannot pass arguments to decorated functions
C. By defining them as global variables
D. By using the pass keyword
Show Answer
Answer:-A. Using *args and **kwargsQ 12. What is a “Decorator with Arguments”?
A. A decorator that only works on math functions
B. A function that returns a decorator
C. A decorator that requires the user to type more code
D. A decorator that cannot be reused
Show Answer
Answer:-B. A function that returns a decoratorQ 13. Which of the following is the correct structure for a decorator that accepts its own arguments?
A. Two levels of nesting (Wrapper inside Decorator)
B. One level (just a Function)
C. Three levels (Wrapper inside Decorator inside Factory)
D. Decorators cannot take arguments
Show Answer
Answer:-C. Three levels (Wrapper inside Decorator inside Factory)Q 14. Can a class be used as a decorator?
A. Yes, by implementing the __call__ method
B. No, decorators must be functions
C. Yes, but only if it inherits from object
D. Yes, but only in Python 2.x
Show Answer
Answer:-A. Yes, by implementing the __call__ methodQ 15. What happens to the original function after it is decorated?
A. It is deleted from memory
B. It is replaced by the wrapper function returned by the decorator
C. It remains unchanged and the decorator is ignored
D. It is renamed to original_func
Show Answer
Answer:-B. It is replaced by the wrapper function returned by the decoratorPart 4: Practical Scenarios
Q 16. Which of the following is a common use case for decorators?
A. Logging and timing function execution
B. Access control and authentication
C. Rate limiting
D. All of the above
Show Answer
Answer:-D. All of the aboveQ 17. What is a “Class Decorator”?
A. A decorator applied to an entire class instead of a single method
B. A method inside a class
C. A decorator that only works on the __init__ method
D. A way to delete a class
Show Answer
Answer:-A. A decorator applied to an entire class instead of a single methodQ 18. In Python’s built-in library, @staticmethod and @classmethod are examples of:
A. Keywords
B. Built-in decorators
C. Variables
D. Modules
Show Answer
Answer:-B. Built-in decoratorsQ 19. What is the output of this code?
Python
def outer(func):
return lambda: "Decorated"
@outer
def simple():
return "Simple"
print(simple())
A. Simple
B. Decorated
C. None
D. Error
Show Answer
Answer:-B. DecoratedQ 20. If you forget to return the wrapper function inside your decorator, what will happen when you call the decorated function?
A. It works normally
B. TypeError: 'NoneType' object is not callable
C. The code will crash during compilation
D. It will print “Return Missing”


Leave a Reply