Python Programming - Inheritance

Exercise : Inheritance - General Questions
  • Inheritance - General Questions
71.
What is the purpose of the __init_subclass__() method?
To define class attributes
To create a new instance of the class
To customize the initialization of subclasses
To access superclass attributes directly
Answer: Option
Explanation:
The __init_subclass__() method is used to customize the initialization of subclasses in Python.

72.
Consider the following code:
class Shape:
    def __init__(self, color):
        self.color = color

class Circle(Shape):
    def __init__(self, radius, color):
        super().__init__(color)
        self.radius = radius
What is the purpose of using super().__init__(color) in the Circle class?
To call the constructor of the Shape class
To create a new instance of the Circle class
To access superclass attributes directly
To define a new constructor for the Circle class
Answer: Option
Explanation:
super().__init__(color) is used to call the constructor of the superclass Shape and initialize the color attribute.

73.
In Python, what is the purpose of the @classmethod 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 takes the class as its first argument
Answer: Option
Explanation:
The @classmethod decorator is used to define a method that takes the class as its first argument, conventionally named cls.

74.
How can you achieve method overriding in Python using the @property decorator?
By directly using the @property decorator on the overridden method
By using the @override decorator
By using the @implements decorator
Method overriding is not possible using @property
Answer: Option
Explanation:
Method overriding in Python can be achieved by using the @property decorator on the method in the superclass and implementing it in the subclass.

75.
What is the purpose of the __call__() method in a Python class?
To create a new instance of the class
To define class attributes for the class
To customize the behavior when an instance of the class is called
To access superclass attributes directly
Answer: Option
Explanation:
The __call__() method in a class is used to customize the behavior when an instance of the class is called, allowing instances to be callable.