Python Programming - Inheritance

Exercise : Inheritance - General Questions
  • Inheritance - General Questions
86.
In Python, what is the purpose of the isinstance() function?
To check if an object is an instance of a class
To check if a class is a subclass of another class
To check if two classes have the same attributes
To check if an object has a specific attribute
Answer: Option
Explanation:
The isinstance() function is used to check if an object is an instance of a particular class or a tuple of classes.

87.
Consider the following code:
class Vehicle:
    def __init__(self, brand):
        self.brand = brand

class Car(Vehicle):
    def __init__(self, brand, model):
        super().__init__(brand)
        self.model = model
What is the primary purpose of using super().__init__(brand) in the Car class constructor?
To call the constructor of the Car class
To create a new instance of the Vehicle class
To initialize attributes of the Vehicle class in the Car class
To access superclass attributes directly
Answer: Option
Explanation:
super().__init__(brand) is used to call the constructor of the superclass Vehicle and initialize the brand attribute.

88.
What is the purpose of the __len__() method in Python classes?
To define class attributes
To create a new instance of the class
To customize the behavior when the len() function is called on an instance
To access superclass attributes directly
Answer: Option
Explanation:
The __len__() method is used to customize the behavior when the len() function is called on an instance of the class.

89.
How does Python handle multiple inheritance conflicts for attribute resolution?
By automatically resolving conflicts using the first defined attribute
By raising an error and requiring explicit resolution using super()
By using the C3 linearization algorithm to determine attribute resolution order
Multiple inheritance conflicts are not allowed in Python
Answer: Option
Explanation:
Python automatically resolves attribute conflicts in multiple inheritance by using the attribute defined in the first class.

90.
What is the purpose of the __slots__ attribute in Python classes?
To define class attributes
To create a new instance of the class
To restrict the set of attributes that can be assigned to instances
To access superclass attributes directly
Answer: Option
Explanation:
The __slots__ attribute is used to restrict the set of attributes that can be assigned to instances, providing memory optimization.