Practice
Plugin be an abstract class or an interface if plugins must maintain internal state and provide default lifecycle methods? Why?Solution
Step 1: Analyze state and default lifecycle method needs
Interfaces cannot hold instance state; abstract classes can hold fields and provide default method bodies.Step 2: Determine which construct fits stateful plugins with default behavior
Abstract class fits because it supports shared state and default lifecycle implementations, easing plugin development.Final Answer:
Option C -> Option CQuick Check:
Abstract class supports state and default methods [check]
User class has a private constructor and a static synchronized getInstance() method to manage user sessions. 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 controls instance creation, which matches Singleton pattern characteristics.Step 2: Why Singleton fits user session management
Singleton ensures a single global instance manages user sessions, avoiding multiple conflicting session managers.Final Answer:
Option A -> Option AQuick Check:
Private constructor + static synchronized getInstance() -> Singleton [check]
notifyObservers() is not synchronized, which exact sequence of events can cause observers to receive inconsistent or duplicate updates?Solution
Step 1: Identify race condition in notifyObservers()
Without synchronization, Thread 1 reads observer list size as 3, then context switches.Step 2: Thread 2 modifies observer list by removing one observer, reducing size to 2.
Thread 1 resumes and tries to notify 3 observers, causing out-of-date or duplicate notifications.Final Answer:
Option A -> Option AQuick Check:
Unsynchronized notifyObservers() -> stale observer list read -> inconsistent notifications [check]
parkVehicle() is NOT synchronized, which exact thread interleaving causes double-booking of the same spot?Solution
Step 1: Identify check-then-act race condition
Without synchronization, Thread 1 reads spot as available but before marking it occupied, context switches to Thread 2 which also reads available.Step 2: Both threads proceed to park, causing double-booking
Because the state update is not atomic, both threads believe the spot is free and park simultaneously.Final Answer:
Option B -> Option BQuick Check:
Check-then-act gap -> double-booking due to unsynchronized parkVehicle() [check]
ElectricCar jobs requiring ChargingSpot resources in the Job Scheduler, what is the minimum change to the current design to maintain extensibility without modifying existing classes?Solution
Step 1: Identify extensibility requirements
ElectricCar and ChargingSpot are new types requiring minimal impact on existing Vehicle and Spot classes.Step 2: Apply extensibility without modifying existing classes
Add new enum values for types, create ElectricCar subclass, and update SpotCompatibility map externally to maintain OCP and avoid class explosion.Final Answer:
Option B -> Option BQuick Check:
New enums + subclass + external compatibility update -> minimal existing code change [check]
