Practice
Deck class has a private constructor and a static synchronized getInstance() method returning the single deck instance. Which design pattern is applied here and why is this 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, matching Singleton pattern characteristics.Step 2: Why Singleton fits this scenario
In a card game, having a single deck instance avoids inconsistencies and duplication, ensuring all players draw from the same deck state.Final Answer:
Option B -> Option BQuick Check:
Private constructor + static synchronized 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]
getInstance() method returning the single instance. Which design pattern is applied and why is this the CORRECT use case?Solution
Step 1: Identify the pattern by constructor and method
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method controls access to a single instance, matching Singleton pattern characteristics.Step 2: Eliminate other patterns
Factory creates multiple instances, Builder constructs complex objects with multiple steps, and Prototype clones objects; none restrict to a single instance.Final Answer:
Option D -> Option DQuick Check:
Private constructor + synchronized static getter -> Singleton pattern [check]
Scorecard class has a recordBall() method that checks if the ball is valid and then updates the score. Without synchronizing recordBall(), what exact sequence of Thread 1 and Thread 2 causes double-counting of runs?Solution
Step 1: Identify check-then-act race condition
Both threads check ball validity before updating score; without synchronization, both see valid state.Step 2: Trace interleaving causing double-count
Thread 1 checks valid -> context switch; Thread 2 checks valid -> both proceed to update score independently causing double-count.Final Answer:
Option B -> Option BQuick Check:
Check-then-act gap without sync -> double update [check]
Solution
Step 1: Identify extensibility requirements
Adding ElectricCar and ChargingSpot requires new types without modifying existing classes to respect OCP.Step 2: Evaluate options
Add ELECTRIC_CAR to VehicleType enum and CHARGING to SpotType enum, create ElectricCar subclass, update SpotCompatibility map adds new enum values and subclasses without changing existing classes, enabling extension. Options B, C, and D modify existing classes or interfaces, violating OCP.Final Answer:
Option C -> Option CQuick Check:
New enums and subclass -> no existing class modified [check]
