Practice
print(house) after constructing a basic house with the Director?Solution
Step 1: Trace Director.construct_basic_house()
The Director calls build_walls() and build_roof() on the builder, adding "Walls" and "Roof" to the house parts.Step 2: Check the final house parts list
The house parts list contains ["Walls", "Roof"]. The pool is not added in this method.Final Answer:
Option A -> Option AQuick Check:
Only walls and roof are added in basic house construction [OK]
- Assuming pool is added by default
- Mixing order of parts
import threading
class Observer:
def __init__(self, name):
self.name = name
def update(self, event_type, message):
print(f"{self.name} received {event_type}: {message}")
class Subject:
def __init__(self):
self.lock = threading.Lock()
self.observers = {}
def subscribe(self, observer, event_type):
with self.lock:
if event_type not in self.observers:
self.observers[event_type] = set()
self.observers[event_type].add(observer)
def unsubscribe(self, observer, event_type):
with self.lock:
if event_type in self.observers and observer in self.observers[event_type]:
self.observers[event_type].remove(observer)
if not self.observers[event_type]:
del self.observers[event_type]
def notify(self, event_type, message):
with self.lock:
observers_snapshot = list(self.observers.get(event_type, []))
for observer in observers_snapshot:
observer.update(event_type, message)
subject = Subject()
obs1 = Observer('Obs1')
obs2 = Observer('Obs2')
subject.subscribe(obs1, 'eventA')
subject.subscribe(obs2, 'eventB')
subject.notify('eventA', 'Hello A')
subject.notify('eventB', 'Hello B')
subject.unsubscribe(obs1, 'eventA')
subject.notify('eventA', 'Hello again A')
What is printed?Solution
Step 1: Trace subscriptions and notifications
Obs1 subscribes to 'eventA', Obs2 to 'eventB'. First notify('eventA') calls Obs1.update, printing "Obs1 received eventA: Hello A". Then notify('eventB') calls Obs2.update, printing "Obs2 received eventB: Hello B".Step 2: Trace unsubscribe and final notification
Obs1 unsubscribes from 'eventA'. The final notify('eventA') finds no observers, so no output for Obs1. Therefore, the last notification does not print anything.Final Answer:
Option D -> Option DQuick Check:
Unsubscribed observers do not receive notifications [OK]
- Assuming unsubscribed observers still get notified
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.
Solution
Step 1: Understand the Diamond Problem
Diamond Problem arises when a class inherits from two classes that share a common ancestor, causing ambiguity.Step 2: Evaluate Multiple inheritance always leads to ambiguous method calls that cannot be resolved.
Multiple inheritance can cause ambiguity, but languages use MRO to resolve it, so it is not always unresolved.Step 3: Evaluate Multiple inheritance eliminates the need for Method Resolution Order (MRO).
MRO is essential in multiple inheritance to resolve method calls, so multiple inheritance does not eliminate MRO.Step 4: Evaluate Multiple inheritance reduces code reuse compared to single inheritance.
Multiple inheritance generally increases code reuse by combining features from multiple classes.Step 5: Correct trade-off
Using multiple inheritance can increase complexity and make the class hierarchy harder to understand and maintain. correctly identifies that multiple inheritance increases complexity and can make hierarchies harder to maintain.Final Answer:
Option D -> Option DQuick Check:
Complexity and maintainability are key trade-offs in multiple inheritance.
- Believing multiple inheritance always causes irresolvable ambiguity
- Thinking MRO is unnecessary with multiple inheritance
- Assuming multiple inheritance reduces code reuse
Solution
Step 1: Analyze statement A
OCP does not forbid all modifications; it encourages minimizing changes to stable, tested code but allows modifications when necessary.Step 2: Validate other statements
Statements B, C, and D correctly describe OCP's goals and mechanisms.Step 3: Why A is incorrect
Absolute prohibition of modification is impractical; OCP is about minimizing and isolating changes.Final Answer:
Option A -> Option AQuick Check:
OCP is about minimizing, not forbidding, modifications.
- Interpreting OCP as no code changes ever allowed
- Ignoring the role of abstraction in OCP
- Underestimating OCP's impact on bug reduction
