Practice
Solution
Step 1: Understand LSP's core requirement
LSP requires that subclasses can replace their superclasses without altering desirable properties of the program, especially behavior.Step 2: Analyze each option carefully
When a subclass extends a superclass but changes the expected behavior of inherited methods. describes a subclass changing expected behavior, which violates LSP. When a subclass adds new methods without overriding any superclass methods. is safe as adding methods doesn't break substitutability. When a subclass narrows the input parameter types of an overridden method. narrows input types (contravariance violation), which breaks substitutability but is less direct than changing behavior. When a subclass uses composition instead of inheritance. is unrelated to LSP since composition is an alternative to inheritance.Final Answer:
Option D -> Option DQuick Check:
Only changing inherited method behavior breaks LSP directly.
- Thinking adding methods breaks LSP
- Confusing covariance and contravariance in parameters
- Assuming composition relates directly to LSP
Solution
Step 1: Understand Singleton purpose
Singleton restricts instantiation to one object, providing a global point of access.Step 2: Identify trade-offs
While simplifying access, it limits flexibility and scalability, especially if multiple parking lots or levels need independent management.Step 3: Address misconceptions
Singleton does not inherently improve concurrency or persistence, nor does it remove synchronization needs.Final Answer:
Option B -> Option BQuick Check:
Singleton simplifies access but restricts scalability [OK]
- Believing Singleton improves concurrency or persistence
- Assuming Singleton removes synchronization requirements
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: Identify overridden methods
Tea overrides prepare_recipe, which breaks the template method pattern by duplicating and changing the algorithm flow.Step 2: Understand impact
Overriding the template method in subclass bypasses the base class skeleton, causing inconsistent behavior and code duplication.Final Answer:
Option A -> Option AQuick Check:
Template method must not be overridden by subclasses [OK]
- Thinking overriding abstract methods is bug
- Ignoring hook method usage
Solution
Step 1: Understand reuse implications
Reusing leaves means the same component can appear multiple times, risking infinite loops.Step 2: Identify solution to prevent infinite loops
Tracking visited components prevents revisiting the same node repeatedly during iteration.Final Answer:
Option C -> Option CQuick Check:
Visited set avoids infinite loops with shared components [OK]
- Assuming no changes needed
- Changing push order does not fix reuse loops
