Exercise: Inheritance

Questions for: Inheritance

What is the purpose of the __len__() method in Python classes?
A:
To define class attributes
B:
To create a new instance of the class
C:
To customize the behavior of the len() function for instances of the class
D:
To access superclass attributes directly
Answer: C
The __len__() method is used to customize the behavior of the len() function for instances of a class.
How can you prevent a class in Python from being inherited by other classes?
A:
By using the @final decorator
B:
By defining all methods as private
C:
By declaring the class as sealed
D:
By not providing a superclass for the class
Answer: A
In Python, a class can be prevented from being inherited by using the @final decorator.
What is the purpose of the __eq__() method in Python classes?
A:
To define class attributes
B:
To create a new instance of the class
C:
To customize the equality comparison between instances
D:
To access superclass attributes directly
Answer: C
The __eq__() method is used to customize the equality comparison between instances of a class.
In Python, what is the purpose of the __call__() method in a class?
A:
To call a method of the superclass
B:
To create a new instance of the class
C:
To make an instance of the class callable
D:
To access class attributes directly
Answer: C
The __call__() method is called when an instance of a class is used as a function, allowing instances to be callable.
How can you achieve method overriding in Python without using the super() function?
A:
By using the @override decorator
B:
By explicitly calling the superclass method
C:
Method overriding is not possible without super()
D:
By redefining the method in the subclass with the same name
Answer: D
Method overriding in Python can be achieved by redefining a method in the subclass with the same name as in the superclass.
Ad Slot (Above Pagination)
Quiz