Practice
Job class has a private constructor and a static synchronized getInstance() method. Which design pattern is applied here and why is this the correct use case?Solution
Step 1: Identify the pattern from constructor and method
A private constructor prevents external instantiation, and a static synchronizedgetInstance()method controls instance creation, which matches Singleton pattern characteristics.Step 2: Reason why Singleton fits Job Scheduler
Singleton ensures a single Job instance manages scheduling globally, avoiding conflicts or duplicate scheduling logic.Final Answer:
Option A -> Option AQuick Check:
Private constructor + static synchronized getInstance() -> Singleton [check]
NotificationChannel be an abstract class or an interface? Consider that each channel has state like connection status and common methods like connect() and send(). Why?Solution
Step 1: Analyze state and behavior requirements
NotificationChannel needs to hold connection status (state) and provide common method implementations.Step 2: Choose between interface and abstract class
Interfaces cannot hold instance state; abstract classes can hold state and provide default behavior, making abstract class the correct choice.Final Answer:
Option A -> Option AQuick Check:
Abstract class supports shared state and default methods [check]
createFile() method checks if a filename is available and then adds the file to the directory without synchronization. Which exact thread interleaving causes two threads to create files with the same name (double creation)?Solution
Step 1: Trace the check-then-act sequence without synchronization
Thread 1 calls isNameAvailable() -> true, but before adding the file, context switches to Thread 2.Step 2: Thread 2 also calls isNameAvailable() -> true and proceeds to add the file, then Thread 1 resumes and adds its file too.
This causes double creation due to the race condition on the availability check and add operation.Final Answer:
Option A -> Option AQuick Check:
Check-then-act gap without sync -> double creation [check]
placeBid() method checks if the auction is still open before accepting a bid. Without synchronizing placeBid(), which exact thread interleaving causes two threads to accept bids after the auction closes, resulting in inconsistent state?Solution
Step 1: Identify check-then-act race
Without synchronization, Thread 1 reads auction open as true but is preempted before recording the bid.Step 2: Thread 2 reads auction open as true and proceeds to record bid as well, both bypassing the closed state check.
Final Answer:
Option D -> Option DQuick Check:
Unsynchronized check-then-act -> double acceptance after close [check]
ConnectionManager, the method addConnection(User a, User b) checks if users are already connected before adding. Without synchronizing this method, what exact thread interleaving causes a double connection to be created?Solution
Step 1: Identify the check-then-act race condition
Without synchronization, both threads can see no existing connection and proceed to add, causing duplicates.Step 2: Trace exact thread interleaving
Thread 1 callsisConnected(a,b)and gets false, then context switches before adding. Thread 2 also callsisConnected(a,b)and gets false, then both add connections concurrently.Final Answer:
Option A -> Option AQuick Check:
Check-then-act gap causes double connection addition [check]
