Practice
Player abstraction for the Snake and Ladder game, should Player be an abstract class or an interface, and why?Solution
Step 1: Analyze state and behavior requirements
ThePlayerhas common state such as current position on the board and methods like move().Step 2: Choose abstraction type
An abstract class allows sharing state and default method implementations, which interfaces (pre-Java 8) cannot provide.Final Answer:
Option B -> Option BQuick Check:
Abstract class holds shared state and behavior [check]
clone() method that returns a copy of itself. Which design pattern is applied here and why is this the correct use case?Solution
Step 1: Identify the pattern from constructor and clone()
The private constructor prevents direct instantiation, and the presence of aclone()method indicates object copying rather than new creation.Step 2: Match pattern intent to scenario
The Prototype pattern is used to clone objects when construction is expensive, which fits the scenario. Singleton restricts to one instance, Factory creates new instances, and Builder separates construction steps.Final Answer:
Option D -> Option DQuick Check:
Private constructor + clone() -> Prototype pattern [check]
RoomBookingManager class has a bookRoom() method that checks if a room is available and then marks it booked. Without synchronizing bookRoom(), which exact sequence of events between Thread 1 and Thread 2 causes a double-booking race condition?Solution
Step 1: Identify check-then-act gap
Without synchronization, Thread 1 reads availability as true but is preempted before booking; Thread 2 also reads true and proceeds.Step 2: Both threads book simultaneously causing double-booking
Because the availability check and booking are not atomic, both threads can book the same room concurrently.Final Answer:
Option D -> Option DQuick Check:
Unsynchronized check-then-act -> double-booking race [check]
reserveSeat() method is not synchronized, which exact sequence of thread interleaving causes double-booking of the same seat?Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads seat as available but is preempted before booking; Thread 2 also reads available and proceeds.Step 2: Both threads book the same seat concurrently
Because no lock or atomic operation protects the check-and-book sequence, double-booking occurs.Final Answer:
Option D -> Option DQuick Check:
Check-then-act gap without synchronization -> double-booking [check]
ElectricCar and ChargingSpot in the existing parking system using the Iterator pattern. What is the minimum change to the current design to support this without modifying existing classes?Solution
Step 1: Identify extensibility requirements
Adding ElectricCar and ChargingSpot should not require modifying existing classes to respect OCP.Step 2: Evaluate options
Add ELECTRIC_CAR to VehicleType enum, CHARGING to SpotType enum, create ElectricCar subclass, and update SpotCompatibility map; existing classes remain unchanged. adds new enums and classes without modifying existing ones, preserving OCP. Modify Vehicle and Spot classes to include electric-specific fields and logic, then update iterators accordingly. modifies existing classes, violating OCP. Create a new ElectricVehicle interface extending Vehicle and a ChargingSpot class, then modify existing iterators to handle them. adds interfaces but requires modifying iterators, violating OCP. Add a flag isElectric to Vehicle class and modify SpotCompatibility to check this flag during iteration. modifies Vehicle class, violating OCP.Final Answer:
Option A -> Option AQuick Check:
Adding new enums and subclasses without changing existing classes respects OCP [check]
