Python Programming - Inheritance

Exercise : Inheritance - General Questions
  • Inheritance - General Questions
76.
In Python, what is the purpose of the __repr__() method in a class?
To define class attributes
To create a new instance of the class
To represent the object as a string for debugging
To access superclass attributes directly
Answer: Option
Explanation:
The __repr__() method is used to provide a string representation of the object, often for debugging purposes.

77.
Consider the following code:
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

class Employee(Person):
    def __init__(self, name, age, employee_id):
        super().__init__(name, age)
        self.employee_id = employee_id
What is the primary advantage of using super().__init__(name, age) in the Employee class constructor?
To call the constructor of the Employee class
To create a new instance of the Person class
To initialize attributes of the Person class in the Employee class
To access superclass attributes directly
Answer: Option
Explanation:
super().__init__(name, age) is used to call the constructor of the superclass Person and initialize the name and age attributes.

78.
What is the purpose of the @staticmethod decorator?
To create a new instance of the class
To define a method that can be accessed like an attribute
To access class attributes directly
To define a method that does not access instance-specific attributes
Answer: Option
Explanation:
The @staticmethod decorator is used to define a method that does not access instance-specific attributes, making it independent of instance state.

79.
How does Python handle method resolution in the presence of diamond inheritance conflicts?
By automatically resolving conflicts using the first defined method
By raising an error and requiring explicit resolution using super()
By using the C3 linearization algorithm to determine method resolution order
Diamond inheritance conflicts are not allowed in Python
Answer: Option
Explanation:
Python uses the C3 linearization algorithm to determine the method resolution order in the presence of diamond inheritance conflicts.

80.
What is the purpose of the __del__() method in Python classes?
To define class attributes
To create a new instance of the class
To customize the behavior when an instance is deleted
To access superclass attributes directly
Answer: Option
Explanation:
The __del__() method is used to customize the behavior when an instance is deleted, allowing cleanup operations.