Practice
Solution
Step 1: Understand the requirement for families of related objects
The problem states the need to create related vehicles (e.g., electric car and electric bike) that belong to a family and must be consistent.Step 2: Match pattern to requirement
Abstract Factory is designed to create families of related objects, ensuring that the created objects are compatible and consistent.Final Answer:
Option B -> Option BQuick Check:
Abstract Factory creates related object families, Factory creates single objects [OK]
- Confusing Factory with Abstract Factory
- Using Builder for simple object creation
Solution
Step 1: Compile-time binding
The compiler knows the reference type but defers binding for overridden methods to runtime.Step 2: Runtime dispatch
At runtime, the actual object's class is identified, and the method pointer is retrieved from the vtable.Step 3: Method execution
The overridden method in the subclass is executed, enabling dynamic polymorphism.Final Answer:
Option C -> Option CQuick Check:
Overriding uses dynamic dispatch via vtable lookup at runtime, not compile-time binding.
- Assuming compile-time binding for overridden methods
- Confusing overloading resolution with overriding
- Believing method signature matching happens at runtime
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 cyclic references problem
Naive recursion without tracking copied objects causes infinite recursion on cycles.Step 2: Evaluate solutions
Memoization tracks already copied objects, preventing infinite loops and ensuring correct deep copy. Shallow copy shares references, breaking independence. JSON serialization cannot handle cycles and will fail.Final Answer:
Option A -> Option AQuick Check:
Memoization prevents infinite recursion in cyclic graphs [OK]
- Ignoring cycles causes infinite recursion or stack overflow
Solution
Step 1: Understand requirement
Need to optionally skip multiple steps dynamically while preserving fixed sequence and reuse.Step 2: Evaluate options
Adding multiple hook methods in base class allows subclasses to override selectively without breaking skeleton.Step 3: Reject other options
Overriding entire template method duplicates code and breaks pattern; flags break encapsulation; removing base class loses reuse.Final Answer:
Option B -> Option BQuick Check:
Multiple hooks preserve flexibility and structure [OK]
- Overriding template method to skip steps
- Using flags breaking encapsulation
