Practice
Solution
Step 1: Understand naive search
MRO does not simply search the first parent class fully before moving to the next; it uses a more sophisticated approach.Step 2: Recognize MRO linearization
MRO uses a specific linearization (like C3 linearization) that merges parent classes and their ancestors in a consistent order.Step 3: Eliminate incorrect options
MRO always calls the method from the last parent class listed in the inheritance is incorrect because the last parent is not always chosen; order and ancestors matter. MRO randomly picks the method from any parent class that defines it is incorrect because MRO is deterministic, not random.Final Answer:
Option A -> Option AQuick Check:
MRO merges inheritance hierarchies to find the correct method in a predictable order.
- Assuming simple left-to-right search suffices
- Believing last parent always overrides
- Thinking method choice is random
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
class Singleton {
private static Singleton instance;
public static Singleton getInstance() {
if (instance == null) {
synchronized(Singleton.class) {
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}
What is the subtle bug that can cause thread-safety issues?Solution
Step 1: Analyze double-checked locking correctness
Without volatile, the instance reference may be visible before full construction due to instruction reordering.Step 2: Check other options
Synchronized block size is minimal and correct; first null check is necessary for performance; constructor privacy is unrelated to this snippet.Final Answer:
Option B -> Option BQuick Check:
Missing volatile causes subtle thread-safety bugs [OK]
- Ignoring volatile keyword necessity
- Thinking synchronized block size is the bug
- Assuming constructor privacy is the main issue here
Solution
Step 1: Understand reuse implications
Reusing the same command instance for multiple executions causes undo/redo to affect the wrong state because the command's internal state may change.Step 2: Identify correct approach
Creating a new command instance per execution ensures each undo/redo corresponds to the exact executed command instance and its parameters.Step 3: Confirm undo/redo stack integrity
This approach preserves correct ordering and state consistency during undo/redo operations.Final Answer:
Option C -> Option CQuick Check:
New instances per execution prevent state corruption on undo/redo [OK]
- Reusing command instances causing incorrect undos
- Clearing stacks unnecessarily losing history
Solution
Step 1: Singleton Board pattern
Incorrect: Singleton would cause shared state across games, leading to conflicts.Step 2: Independent GameController and Player per game
Correct: Isolates state per game, preventing interference.Step 3: Sharing Dice instance
Incorrect: Shared Dice can cause race conditions or inconsistent rolls.Step 4: Global player positions
Incorrect: Global state breaks isolation and causes bugs.Final Answer:
Option A -> Option AQuick Check:
Isolating state per game instance is key for concurrency safety.
- Using singleton for shared components without considering concurrency
- Sharing mutable objects like Dice across threads
- Centralizing state globally ignoring isolation
