Python Programming - Inheritance

Exercise : Inheritance - General Questions
  • Inheritance - General Questions
16.
What is the purpose of the @classmethod decorator?
To define a class method
To create a new instance of a class
To access class attributes directly
To override a method in the superclass
Answer: Option
Explanation:
The @classmethod decorator is used to define a class method, which is a method that is bound to the class and not the instance of the class.

17.
What is the difference between composition and inheritance?
Composition allows a class to inherit from multiple classes
Inheritance allows a class to contain objects of other classes
Composition promotes code reusability through the creation of new classes
Inheritance promotes code reusability by allowing a class to inherit attributes and methods
Answer: Option
Explanation:
Inheritance is the mechanism by which one class can inherit attributes and methods from another class, while composition involves containing objects of other classes within a class.

18.
What is the role of the __bases__ attribute in a Python class?
To access the superclass attributes
To check if an object is an instance of a specific class
To determine the base classes of a class
To create a new instance of the class
Answer: Option
Explanation:
The __bases__ attribute is a tuple containing the base classes of a class, allowing you to inspect the inheritance hierarchy.

19.
What is the purpose of the @staticmethod decorator?
To define a static method
To create a new instance of a class
To access class attributes directly
To override a method in the superclass
Answer: Option
Explanation:
The @staticmethod decorator is used to define a static method in a class, which is a method that belongs to the class rather than an instance.

20.
What is the purpose of the __mro__ attribute?
To access superclass attributes
To check if an object is an instance of a specific class
To determine the method resolution order of a class
To create a new instance of the class
Answer: Option
Explanation:
The __mro__ attribute is a tuple containing the method resolution order of a class, showing the order in which base classes are searched when resolving methods.