Practice
OrderManager class has a private constructor and a static synchronized getInstance() method. Which design pattern is applied here and why is it the correct choice?Solution
Step 1: Identify the pattern from constructor and method
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method ensures a single shared instance, which matches the Singleton pattern.Step 2: Why Singleton fits OrderManager
OrderManager must coordinate all orders centrally to avoid conflicting states; multiple instances would cause inconsistent order tracking.Final Answer:
Option C -> Option CQuick Check:
Private constructor + synchronized static getInstance() -> Singleton [check]
Player abstraction for the Tic-Tac-Toe game, should Player be an abstract class or an interface, and why?Solution
Step 1: Analyze state and behavior requirements
Players have common state (e.g., name, symbol, score) and some shared behavior (e.g., basic validation). Abstract classes allow state fields and shared method implementations.Step 2: Why abstract class is better than interface here
Interfaces define behavior contracts but cannot hold instance state. Although Java 8+ interfaces support default methods, they cannot hold mutable state. Thus, abstract class suits better for shared state and behavior.Final Answer:
Option C -> Option CQuick Check:
Shared state + behavior -> abstract class, not interface [check]
WeekendFeeStrategy to calculate parking fees differently on weekends. Given the current design uses a FeeStrategy interface and ParkingLot depends on it, what is the minimum change to comply with SOLID principles?Solution
Step 1: Identify SOLID principle involved
The Open-Closed Principle (OCP) requires thatParkingLotshould be open for extension but closed for modification.Step 2: Evaluate options against OCP
ModifyParkingLotto detect weekends and apply extra fees internally. modifiesParkingLot, violating OCP. Create a newWeekendFeeStrategyimplementingFeeStrategyand inject it without changingParkingLot. adds a new strategy class and injects it, adhering to OCP. Add a boolean flagisWeekendtoFeeStrategyand update all implementations. adds a flag to the interface, forcing all implementations to change, violating OCP. Combine all fee calculations into a singleComplexFeeStrategyclass. merges logic, increasing complexity and violating Single Responsibility Principle.Final Answer:
Option A -> Option AQuick Check:
New strategy class extends behavior without modifying existing code [check]
WeekendFeeStrategy without modifying existing classes, which SOLID principle is being applied and what is the minimum required change?Solution
Step 1: Identify SOLID principle
Open/Closed Principle (OCP) states software entities should be open for extension but closed for modification.Step 2: Apply OCP by adding WeekendFeeStrategy implementing FeeStrategy interface without modifying existing classes.
Final Answer:
Option A -> Option AQuick Check:
New strategy class + injection -> OCP compliance [check]
addProductToCart() method is not synchronized, which exact sequence of events can cause the cart to contain duplicate entries for the same product?Solution
Step 1: Identify check-then-act race condition
Both threads check if product exists and see false before either adds it, due to lack of synchronization.Step 2: Both threads add product concurrently causing duplicates
Because the add operation is not atomic, both add the product, resulting in duplicates.Final Answer:
Option A -> Option AQuick Check:
Check-then-act gap without synchronization -> duplicate entries [check]
