Exercise: Inheritance

Questions for: Inheritance

What is the purpose of the @staticmethod decorator?
A:
To define a static method
B:
To create a new instance of a class
C:
To access class attributes directly
D:
To override a method in the superclass
Answer: A
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.
What is the role of the __bases__ attribute in a Python class?
A:
To access the superclass attributes
B:
To check if an object is an instance of a specific class
C:
To determine the base classes of a class
D:
To create a new instance of the class
Answer: C
The __bases__ attribute is a tuple containing the base classes of a class, allowing you to inspect the inheritance hierarchy.
What is the difference between composition and inheritance?
A:
Composition allows a class to inherit from multiple classes
B:
Inheritance allows a class to contain objects of other classes
C:
Composition promotes code reusability through the creation of new classes
D:
Inheritance promotes code reusability by allowing a class to inherit attributes and methods
Answer: B
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.
What is the purpose of the @classmethod decorator?
A:
To define a class method
B:
To create a new instance of a class
C:
To access class attributes directly
D:
To override a method in the superclass
Answer: A
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.
In Python, what happens when a subclass defines a method with the same name as a method in its superclass?
A:
The superclass method is automatically overridden
B:
The subclass method is ignored
C:
An error occurs, and the program crashes
D:
The subclass method overrides the superclass method
Answer: D
In Python, if a subclass defines a method with the same name as a method in its superclass, the subclass method will override the superclass method.
Ad Slot (Above Pagination)
Quiz