Practice
Profile component, should Profile be an abstract class or an interface, considering it has both state (e.g., name, headline) and behavior (e.g., updateStatus())? Why?Solution
Step 1: Analyze state and behavior requirements
TheProfilehas shared state fields like name and headline, which need storage and default behavior likeupdateStatus().Step 2: Choose between abstract class and interface
Abstract classes can hold state and provide default method implementations, while interfaces cannot hold instance state (prior to Java 8 default methods, but still no fields).Final Answer:
Option A -> Option AQuick Check:
Abstract class supports shared state and behavior [check]
getInstance() method returning a single instance. Which pattern is applied and why is this the CORRECT use case?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 with thread safety.Step 2: Eliminate other creational patterns
Factory Method creates objects but doesn't enforce single instance; Builder constructs complex objects but not singleton; Prototype clones instances, not restricts to one.Final Answer:
Option D -> Option DQuick Check:
Private constructor + synchronized static getter -> Singleton pattern [check]
addTrack() is not synchronized. Two threads concurrently check if the queue has space and then add tracks. Which exact sequence causes a race condition leading to queue overflow?Solution
Step 1: Identify the check-then-act pattern in addTrack()
Both threads check queue size before adding, but without synchronization, the state can change between check and add.Step 2: Trace the race condition sequence
Thread 1 checks size -> space available, context switch before adding. Thread 2 checks size -> space available, both add tracks, causing overflow.Final Answer:
Option A -> Option AQuick Check:
Unsynchronized check-then-act -> double add -> overflow [check]
sendNotification() method checks if a channel is available before sending. Without synchronizing this method, what exact sequence of thread interleaving causes duplicate notifications to be sent?Solution
Step 1: Identify the check-then-act race condition
Without synchronization, both threads can see the channel as available before either marks it busy.Step 2: Trace thread interleaving
Thread 1 checks availability -> true, context switch; Thread 2 checks availability -> true; both proceed to send, causing duplicate notifications.Final Answer:
Option D -> Option DQuick Check:
Unsynchronized check-then-act allows concurrent sends [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]
