Exercise: Inheritance

Questions for: Inheritance

What is the diamond problem in the context of multiple inheritance?
A:
A naming conflict in class attributes
B:
Difficulty in determining method resolution order
C:
Ambiguity caused by a class inheriting from two classes with a common ancestor
D:
Inability to use private attributes
Answer: C
The diamond problem occurs in multiple inheritance when a class inherits from two classes that have a common ancestor, leading to ambiguity in method resolution order.
What is the purpose of the __init__ method in Python classes?
A:
To define class attributes
B:
To create a new instance of the class
C:
To initialize class attributes and perform setup operations
D:
To access superclass attributes
Answer: C
The __init__ method is a special method in Python classes used for initializing the attributes of an object and performing setup operations during object creation.
What is the purpose of the issubclass() function?
A:
To check if an object is an instance of a specific class
B:
To create a new instance of a class
C:
To check if a class is a subclass of another class
D:
To access class attributes directly
Answer: C
The issubclass() function is used to check if a given class is a subclass of another class.
How does Python support encapsulation in the context of inheritance?
A:
By making all attributes public
B:
By using private attributes and methods
C:
By restricting inheritance
D:
By removing the need for constructors
Answer: B
Python supports encapsulation by allowing the use of private attributes and methods, denoted by a double underscore (e.g., __private_attribute), which are only accessible within the class.
In Python, what is the order in which classes are searched when resolving a method or attribute?
A:
Bottom-up: From subclass to superclass
B:
Top-down: From superclass to subclass
C:
Random: No specific order
D:
Alphabetical: Based on class names
Answer: B
In Python, the method resolution order (MRO) follows a top-down approach, searching for methods or attributes first in the current class, then in its superclass, and so on.
Ad Slot (Above Pagination)
Quiz