Practice
Solution
Step 1: Understand inheritance types
Single inheritance involves one parent and one child class, so it cannot inherit from two unrelated classes.Step 2: Multiple inheritance definition
Multiple inheritance allows a class to inherit from more than one parent class, fitting the scenario.Step 3: Multilevel and hierarchical inheritance
Multilevel is a chain of inheritance, hierarchical is one parent with multiple children, neither fits inheriting from two unrelated classes.Final Answer:
Option B -> Option BQuick Check:
Multiple inheritance is the only type that supports inheriting from multiple unrelated classes.
- Confusing multilevel with multiple inheritance
- Assuming hierarchical inheritance allows multiple parents
- Thinking single inheritance can have multiple parents
Solution
Step 1: Review Adapter intent
Adapter converts incompatible interfaces to make them compatible.Step 2: Check each statement
A is correct: Adapter changes interface. B is correct: Adapter can use inheritance or composition. C is correct: Adapter can add behavior without modifying original object. D is incorrect: Simplifying a complex subsystem is Facade's role, not Adapter's.Final Answer:
Option D -> Option D
- Confusing Adapter with Facade's simplification role
- Thinking Adapter only uses inheritance
- Assuming Adapter cannot add new behavior
Solution
Step 1: Understand iterator stack usage
The iterator stack holds nodes along the current traversal path, not all nodes simultaneously.Step 2: Relate stack size to tree height
Maximum stack size corresponds to the height h of the tree, as children are pushed and popped during traversal.Final Answer:
Option D -> Option DQuick Check:
Stack size bounded by tree height h, not total nodes n [OK]
- Assuming stack holds all nodes at once
- Confusing height with log n for unbalanced trees
Solution
Step 1: Analyze each statement
A class should have only one reason to change, which means it should have only one responsibility. correctly states the core SRP definition.Step 2: Evaluate SRP means a class should only have one method to ensure simplicity.
SRP is about reasons to change, not the number of methods; a class can have many methods if they serve one responsibility.Step 3: Confirm options A, B, and D
Options A, B, and D are true: SRP improves cohesion, reduces coupling, and prevents fragile code.Final Answer:
Option A -> Option AQuick Check:
SRP ≠ one method per class; it's about one reason to change.
- Confusing responsibility with method count.
- Assuming fewer methods always means better design.
- Ignoring cohesion and coupling effects.
Solution
Step 1: Understand super() in multiple inheritance
super() does not simply call the immediate parent but follows the MRO linearization.Step 2: MRO linearization
MRO creates a linear order of classes to avoid ambiguity and duplication, so super() calls the next method in this order.Step 3: Eliminate incorrect options
super() calls the method from the immediate parent class only, ignoring the diamond structure is incorrect because super() is not limited to immediate parent. super() calls all parent methods with the same name in parallel, combining their effects is wrong because super() does not call methods in parallel. super() always calls the method from the base class at the top of the diamond first is incorrect because super() does not always start at the base class.Final Answer:
Option A -> Option AQuick Check:
super() respects MRO linearization, ensuring consistent method calls in diamond inheritance.
- Thinking super() calls only immediate parent
- Believing super() calls all parents simultaneously
- Assuming super() always calls base class first
