11. How is a decorator applied to a class method in Python? A. By using the @decorator syntax above the class B. By using the @decorator syntax above the method C. By applying the decorator within the method D. By using the apply_decorator() function Answer & Solution Discuss in Board Save for Later Answer & Solution Answer: Option B Solution: A decorator is applied to a class method using the @decorator syntax above the method definition.
12. What is the purpose of the property decorator in Python? A. To add properties to classes B. To remove properties from classes C. To define a getter method for a class attribute D. To define a setter method for a class attribute Answer & Solution Discuss in Board Save for Later Answer & Solution Answer: Option C Solution: The property decorator is used to define a getter method for a class attribute.
13. 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 Answer & Solution Discuss in Board Save for Later Answer & Solution Answer: Option B Solution: The @staticmethod decorator allows a method to be called without creating an instance of the class.
14. 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 Answer & Solution Discuss in Board Save for Later Answer & Solution Answer: Option C Solution: The @classmethod decorator can access class attributes, while @staticmethod cannot.
15. What is the purpose of the @property decorator in Python? A. To define a setter method for a class attribute B. To define a getter method for a class attribute C. To add properties to classes D. To define methods for instance attributes Answer & Solution Discuss in Board Save for Later Answer & Solution Answer: Option B Solution: The @property decorator is used to define a getter method for a class attribute, allowing access as a property.
16. 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 Answer & Solution Discuss in Board Save for Later Answer & Solution Answer: Option A Solution: The @classmethod decorator is used to define a method that operates on the class and not just instances.
17. 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 Answer & Solution Discuss in Board Save for Later Answer & Solution Answer: Option B Solution: The @classmethod decorator is applied above the method definition using the @classmethod syntax.
18. 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 Answer & Solution Discuss in Board Save for Later Answer & Solution Answer: Option C Solution: The @abstractmethod decorator indicates that a method must be overridden in subclasses.
19. 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 Answer & Solution Discuss in Board Save for Later Answer & Solution Answer: Option D Solution: You create a custom decorator by defining a function and using the @decorator_name syntax.
20. 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 Answer & Solution Discuss in Board Save for Later Answer & Solution Answer: Option A Solution: The functools.partial function is used to create a new function with some arguments pre-filled.