Python Programming - Inheritance

Exercise : Inheritance - General Questions
  • Inheritance - General Questions
96.
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.

97.
Consider the following code:
class Book:
    def __init__(self, title, author):
        self.title = title
        self.author = author

class EBook(Book):
    def __init__(self, title, author, format_type):
        super().__init__(title, author)
        self.format_type = format_type
What is the primary advantage of using super().__init__(title, author) in the EBook class constructor?
To call the constructor of the EBook class
To create a new instance of the Book class
To initialize attributes of the Book class in the EBook class
To access superclass attributes directly
Answer: Option
Explanation:
super().__init__(title, author) is used to call the constructor of the superclass Book and initialize the title and author attributes.

98.
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.

99.
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.

100.
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.