Exercise: Python

Questions for: Inheritance

What is the purpose of the __annotations__ attribute in Python classes?
A:
To define class attributes
B:
To create a new instance of the class
C:
To store type annotations for class attributes and methods
D:
To access superclass attributes directly
Answer: C
The __annotations__ attribute is used to store type annotations for class attributes and methods, providing information about the expected data types.
How can you achieve method overriding in Python using the @abstractmethod decorator?
A:
By directly using the @abstractmethod decorator on the overridden method
B:
By using the @override decorator
C:
By using the @implements decorator
D:
Method overriding is not possible using @abstractmethod
Answer: A
Method overriding in Python can be achieved by using the @abstractmethod decorator on the method in the superclass and implementing it in the subclass.
In Python, what is the purpose of the __call__() method in a metaclass?
A:
To create a new instance of the metaclass
B:
To define class attributes for the metaclass
C:
To customize the behavior when an instance of the metaclass is called
D:
To access superclass attributes directly
Answer: C
The __call__() method in a metaclass is used to customize the behavior when an instance of the metaclass is called, allowing customization of class creation.
What is the purpose of the __missing__() method in Python classes?
A:
To define class attributes
B:
To create a new instance of the class
C:
To customize the behavior when a key is not found in a class used as a dictionary
D:
To access superclass attributes directly
Answer: C
The __missing__() method is used to customize the behavior when a key is not found in a class used as a dictionary.
What is the purpose of the __subclasshook__() method in Python classes?
A:
To define class attributes
B:
To create a new instance of the class
C:
To customize the behavior when checking for subclass relationships
D:
To access superclass attributes directly
Answer: C
The __subclasshook__() method is used to customize the behavior when checking for subclass relationships, providing a way to influence the issubclass() function.
Ad Slot (Above Pagination)
Quiz