1. What are decorators in Python? A. Design patterns B. Functions that add functionality to other functions C. Programming languages D. Built-in Python modules Answer & Solution Discuss in Board Save for Later Answer & Solution Answer: Option B Solution: Decorators are functions that enhance or modify the behavior of other functions.
2. How is a decorator defined in Python? A. Using the @decorator syntax B. By calling the decorator() function C. Using the def keyword D. Using the modify() keyword Answer & Solution Discuss in Board Save for Later Answer & Solution Answer: Option A Solution: Decorators are defined using the @decorator_name syntax above a function definition.
3. What is the primary purpose of using decorators? A. To reduce code complexity B. To replace existing functions C. To add functionalities to functions D. To remove functionalities from functions Answer & Solution Discuss in Board Save for Later Answer & Solution Answer: Option C Solution: Decorators are used to add or modify functionalities of functions without changing their code.
4. How can you pass arguments to a decorator in Python? A. By modifying the decorator's code B. By using the @decorator syntax C. By defining arguments within the function D. By using the args keyword Answer & Solution Discuss in Board Save for Later Answer & Solution Answer: Option B Solution: Arguments can be passed to decorators using the @decorator(arg) syntax.
5. What is a common use case of decorators? A. Replacing built-in functions B. Adding logging or authentication to functions C. Removing error handling from functions D. Changing the function's name Answer & Solution Discuss in Board Save for Later Answer & Solution Answer: Option B Solution: Decorators are often used to add features like logging, authentication, or authorization to functions.
6. How does the order of applying multiple decorators matter? A. It doesn't matter B. It depends on the number of decorators C. It affects the order of execution D. It changes the function's return value Answer & Solution Discuss in Board Save for Later Answer & Solution Answer: Option C Solution: The order of applying decorators can affect the order in which their functionalities are executed.
7. What is a higher-order function in the context of decorators? A. A function that returns a function B. A function that takes a function as an argument C. A function that only contains decorators D. A function with multiple return statements Answer & Solution Discuss in Board Save for Later Answer & Solution Answer: Option B Solution: Higher-order functions accept functions as arguments and/or return functions as results.
8. How do you create a decorator that takes arguments? A. Using the @decorator syntax B. By using the @decorator(arg) syntax C. By using the @decorator() syntax D. By using the decorator(arg) function Answer & Solution Discuss in Board Save for Later Answer & Solution Answer: Option B Solution: A decorator that takes arguments is applied using the @decorator(arg) syntax.
9. What is the purpose of the functools.wraps() function in decorators? A. To create a new function B. To modify the decorator's behavior C. To preserve the original function's metadata D. To remove a decorator Answer & Solution Discuss in Board Save for Later Answer & Solution Answer: Option C Solution: functools.wraps() preserves the original function's metadata when using decorators.
10. What is the difference between a class-based decorator and a function-based decorator? A. Class-based decorators use fewer resources B. Class-based decorators can't accept arguments C. Function-based decorators are more flexible D. Function-based decorators are slower Answer & Solution Discuss in Board Save for Later Answer & Solution Answer: Option C Solution: Function-based decorators are generally more flexible and can accept arguments more easily than class-based decorators.