Practice
Vehicle abstraction in Java that requires both state (like speed) and behavior (like accelerate()), should Vehicle be an abstract class or an interface? Why?Solution
Step 1: Analyze state and behavior requirements
Vehicle needs to hold state (speed) and behavior (accelerate()), which requires fields and method implementations.Step 2: Choose correct OOP construct
Abstract classes can hold state and provide default behavior, while interfaces cannot hold instance state (fields) and only allow default methods without state.Final Answer:
Option D -> Option DQuick Check:
Abstract class supports state and behavior, interface does not hold state [check]
Command interface in Java, should it be an abstract class or an interface, and why? Consider that commands may need to store state for undo functionality.Solution
Step 1: Analyze state requirements for undo
Commands often need to store state (e.g., previous values) to support undo, which requires fields.Step 2: Compare interface vs abstract class in Java
Interfaces cannot hold instance fields, so abstract classes are better to share common state and default undo logic.Final Answer:
Option A -> Option AQuick Check:
Abstract class supports shared state and behavior for undo [check]
ElevatorController class uses a private constructor and a static synchronized getInstance() method to provide a single global access point. Which design pattern is applied here and why is it the correct choice?Solution
Step 1: Identify Singleton usage
The private constructor and static synchronizedgetInstance()method restrict instantiation to one object, ensuring a single ElevatorController.Step 2: Why Singleton fits ElevatorController
ElevatorController coordinates multiple elevators and floor queues centrally, so multiple instances would cause inconsistent state and scheduling conflicts.Final Answer:
Option B -> Option BQuick Check:
Singleton ensures one global ElevatorController instance [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]
ElectricPlayer who requires ChargingSpot in the Snake and Ladder game, what is the minimum change to the current design to maintain extensibility without modifying existing classes?Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and new subclass preserves existing class code unchanged.Step 2: Update compatibility map for new player and spot types
SpotCompatibility map extension allows new rules without changing existing logic.Final Answer:
Option A -> Option AQuick Check:
New enums + subclass + compatibility map extension -> minimal existing code change [check]
