Inheritance, super() & the MRO — Concept Check
Questions on Inheritance, super() & the MRO. Subclasses extend or override parent behaviour; super() delegates up the Method Resolution Order — a C3-linearized chain that makes even multiple inheritance deterministic.
Questions in this quiz
8 questions on Inheritance, super() & the MRO — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.In a Python class hierarchy where a subclass does not explicitly call super().__init__(), what is the consequence?
- AThe subclass will inherit methods but not attributes from the parent class.
- BThe parent class's __init__() is not executed, leaving parent attributes uninitialized.
- CThe subclass will not be able to access any methods from the parent class.
- DThe subclass will automatically call the parent class's __init__().
2.A developer creates a class D(B, C) using multiple inheritance. What is the order of method resolution?
- AB, C, D, base
- BD, B, C, base
- CC, B, D, base
- DD, C, B, base
3.What happens if you forget to call super() in a class with multiple inheritance?
- AThe program will raise an error immediately.
- BThe MRO is disrupted, and subsequent super() calls may fail.
- COnly the first parent class's methods are called.
- DThe class will still function correctly as long as methods are defined.
4.In the code snippet, what does the method User.to_json() return?
- AAn empty JSON object.
- BA JSON string of only the User class attributes.
- CA JSON string of the User instance's methods.
- DA JSON string of the User instance's attributes.
5.A developer believes that in Python, super() always calls the immediate parent class. What is the flaw in this belief?
- Asuper() only works with single inheritance.
- Bsuper() calls the first method in the class definition.
- Csuper() follows the MRO, which may not be the immediate parent.
- Dsuper() can only be used in __init__ methods.
6.In a class hierarchy, why is it important to call super().__init__() in a subclass?
- ATo automatically inherit all parent methods.
- BTo ensure parent class attributes are initialized.
- CTo guarantee the subclass has no attributes.
- DTo prevent syntax errors during class definition.
7.When using multiple inheritance in Python, what does C3 linearization ensure?
- AA deterministic order for method resolution.
- BAttributes are inherited before methods.
- CMethods are resolved in the order they are defined.
- DThe first parent class is always prioritized.
8.In the given code, what is the role of the super() call in TimestampMixin?
- AIt ensures the MRO is followed, allowing Model.__init__ to execute.
- BIt directly calls the User class constructor.
- CIt prevents the Model class from being called.
- DIt initializes the JSONMixin attributes.