Practice
Solution
Step 1: Understand Proxy's role
Proxy controls access and may delay creation of the real service (lazy initialization).Step 2: Trace the call flow
Client calls Proxy -> Proxy checks access -> Proxy creates real service if not already created -> Proxy forwards call -> Real service executes.Step 3: Eliminate incorrect options
B is incorrect because Proxy usually adds control logic before forwarding. C is incorrect as Proxy typically does not modify parameters (that's Decorator or Adapter). D describes Facade, not Proxy.Final Answer:
Option A -> Option A
- Assuming Proxy always forwards calls immediately without checks
- Confusing Proxy with Facade's simplification role
- Thinking Proxy modifies request parameters
Solution
Step 1: Understand the role of Dice
The Dice only generates a random number; it does not manage state transitions.Step 2: Consider Player class responsibilities
Player holds position but should not decide how to update it considering snakes or ladders.Step 3: Analyze Board class role
Board knows snakes and ladders but does not manage player state transitions directly.Step 4: Role of GameController
GameController coordinates dice roll, queries Board for snakes/ladders, and updates Player position accordingly.Final Answer:
Option D -> Option DQuick Check:
GameController centralizes state transitions, ensuring separation of concerns.
- Thinking Dice manages player position
- Assuming Player updates position without Board's input
- Believing Board directly changes player state
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: Identify responsibilities
The class handles both validation and persistence, two distinct reasons to change.Step 2: Trace change impact
Changing validation rules requires modifying validation code inside the class.Step 3: Side effects
Because persistence logic shares the class, changes risk affecting persistence unintentionally, increasing maintenance risk.Step 4: SRP violation
This coupling violates SRP, as one reason to change (validation) affects unrelated functionality (persistence).Final Answer:
Option B -> Option BQuick Check:
One reason to change should not force changes in unrelated code -> SRP violation.
- Assuming changes affect only related methods without side effects.
- Believing tight coupling is harmless if code is in one class.
- Thinking validation and persistence are always linked.
import copy
class Profile:
def __init__(self, name, scores):
self.name = name
self.scores = scores
def __deepcopy__(self, memo):
new_name = self.name # Bug here
new_scores = copy.deepcopy(self.scores, memo)
return Profile(new_name, new_scores)
original = Profile('Alice', [10, 20])
copy_obj = copy.deepcopy(original)
copy_obj.scores.append(30)
print(original.scores)
Solution
Step 1: Examine __deepcopy__ method
The line new_name = self.name copies the reference to the name string instead of deep copying it.Step 2: Understand impact
Strings are immutable in Python, so shallow copy is usually safe, but if name were a mutable object, this would cause shared references and bugs. Proper deep copy should be used for consistency.Final Answer:
Option A -> Option AQuick Check:
Only new_name assignment lacks deepcopy -> subtle bug [OK]
- Forgetting to deepcopy all nested fields, assuming immutables are safe
