Practice
FareCalculator class has a private constructor and a static synchronized getInstance() method. Which design pattern is applied here and why is this the correct use case?Solution
Step 1: Identify the pattern from constructor and method signatures
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method ensures a single shared instance, matching Singleton pattern characteristics.Step 2: Why Singleton fits FareCalculator
FareCalculator needs consistent fare rules and shared state (e.g., pricing configs). Singleton avoids multiple inconsistent instances and ensures thread-safe access.Final Answer:
Option C -> Option CQuick Check:
Private constructor + synchronized getInstance() -> Singleton pattern [check]
makeMove() method, which exact sequence of thread interleaving causes two players to move simultaneously, violating turn order?Solution
Step 1: Identify the check-then-act race condition
Both threads read the turn state before either updates it, causing both to believe it's their turn.Step 2: Trace thread interleaving
Thread 1 checks turn -> true, context switch before move; Thread 2 checks turn -> true, both move simultaneously, violating turn order.Final Answer:
Option B -> Option BQuick Check:
Check-then-act gap allows double move without synchronization [check]
WeekendFeeStrategy to the billing module without modifying existing classes. Which SOLID principle does this design enforce, and what is the minimum change required?Solution
Step 1: Identify SOLID principle for extensibility
Open/Closed Principle (OCP) states software entities should be open for extension but closed for modification.Step 2: Minimum change to add WeekendFeeStrategy
Create a new class implementing FeeStrategy and inject it via dependency injection (DIP) into Billing or related classes, avoiding modification of existing code.Final Answer:
Option A -> Option AQuick Check:
Inject new strategy class -> respects OCP and DIP [check]
WeekendFeeStrategy to calculate notification fees differently on weekends. According to SOLID principles, what is the minimum change required in the current design that uses a FeeStrategy interface?Solution
Step 1: Identify applicable SOLID principle
The Open-Closed Principle (OCP) requires classes to be open for extension but closed for modification.Step 2: Evaluate options against OCP
Create a new WeekendFeeStrategy class implementing FeeStrategy and inject it without modifying existing classes. adds a new class implementing FeeStrategy without modifying existing code, adhering to OCP. Others modify existing classes or interfaces, violating OCP.Final Answer:
Option A -> Option AQuick Check:
New strategy class extends behavior without modifying existing code [check]
WeekendFeeStrategy for fare calculation. Given the current design uses a FeeStrategy interface, what is the minimum change required to comply with SOLID principles?Solution
Step 1: Identify applicable SOLID principle
The Open-Closed Principle (OCP) requires classes to be open for extension but closed for modification.Step 2: Evaluate options against OCP
Create a new WeekendFeeStrategy class implementing FeeStrategy and inject it without modifying existing classes. adds a new FeeStrategy implementation without modifying existing classes, adhering to OCP. Options A and C modify existing classes, violating OCP. Create a WeekendFareCalculator subclass extending FareCalculator and override calculateFare(). creates subclass but may cause class explosion.Final Answer:
Option B -> Option BQuick Check:
New FeeStrategy implementation -> OCP compliant [check]
