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 decoratorB:
By defining all methods as private
C:
By declaring the class as
sealedD:
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.Discuss About this Question.
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.Discuss About this Question.
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.Discuss About this Question.
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.
Discuss About this Question.
Ad Slot (Above Pagination)
Discuss About this Question.