Practice
1. In the Online Stock Brokerage system, the
TradeManager class has a private constructor and a static synchronized getInstance() method. Which design pattern is applied here and why is this the correct choice?easy
Solution
Step 1: Identify the pattern from constructor and method
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method ensures a single instance is created and accessed thread-safely.Step 2: Why Singleton fits this scenario
TradeManager must coordinate all trades globally to maintain consistent state; multiple instances would cause conflicting trade states.Final Answer:
Option D -> Option DQuick Check:
Private constructor + synchronized getInstance() -> Singleton pattern [check]
Hint: Private constructor + static synchronized getInstance() -> Singleton [check]
Common Mistakes:
2. In a UML class diagram, should
Vehicle be modeled as an abstract class or an interface if it has state like speed and behavior like accelerate()? Why?easy
Solution
Step 1: Analyze state and behavior requirements
Vehicle has state (speed) and behavior (accelerate()), so it needs a construct that supports instance variables and method implementations.Step 2: Evaluate abstract class vs interface
Abstract classes can hold state and provide default behavior; interfaces cannot hold instance state (only constants) and default methods can't maintain state.Final Answer:
Option B -> Option BQuick Check:
State + behavior with shared code -> abstract class [check]
Hint: Stateful base class -> abstract class, not interface [check]
Common Mistakes:
3. You want to add a
WeekendBonusRule that gives players extra moves on weekends without modifying existing classes. Which SOLID principle guides the minimal change, and what is the correct approach?medium
Solution
Step 1: Identify SOLID principle for extension without modification
Open/Closed Principle (OCP) states software entities should be open for extension but closed for modification.Step 2: Evaluate approaches
Subclassing Player to WeekendPlayer allows extension without modifying Player; injecting new MoveRule class also respects OCP but requires interface design.Final Answer:
Option C -> Option CQuick Check:
Subclassing extends behavior without modifying base class [check]
Hint: Extend via subclass or interface, don't modify existing classes [check]
Common Mistakes:
4. In the Blackjack game, if the
placeBet() method is not synchronized, which exact thread interleaving causes a player to double-bet?medium
Solution
Step 1: Identify check-then-act race condition
Without synchronization, Thread 1 reads balance sufficient and is preempted before updating; Thread 2 also reads balance sufficient and proceeds.Step 2: Both threads place bets reducing balance twice, causing double-bet
This classic check-then-act gap leads to inconsistent state and double deduction.Final Answer:
Option C -> Option CQuick Check:
Unsynchronized check-then-act -> double bet race condition [check]
Hint: Check balance -> context switch -> both bet -> double deduction [check]
Common Mistakes:
5. You want to add a
WeekendFeeStrategy to calculate weekend parking fees without modifying existing classes. Which SOLID principle guides the minimum change, and what is the correct approach?medium
Solution
Step 1: Identify the SOLID principle for extensibility
Open/Closed Principle (OCP) requires adding new functionality without modifying existing code.Step 2: Evaluate approaches
Creating a new FeeStrategy implementation and injecting it respects OCP and DIP; modifying ParkingLot or existing strategies violates OCP or mixes responsibilities.Final Answer:
Option B -> Option BQuick Check:
New FeeStrategy class + injection -> OCP and DIP respected [check]
Hint: New strategy class + injection -> Open/Closed Principle [check]
Common Mistakes:
