OCP Java Concurrency 3 — Questions and Answers
Question 1: Which `java.util.concurrent.atomic` class provides an atomic reference to an object that can be updated using compare-and-swap?
- AtomicReference (Correct answer)
- AtomicStampedReference
- AtomicMarkableReference
- AtomicReferenceFieldUpdater
Correct answer: AtomicReference
`AtomicReference<V>` wraps an object reference and provides atomic CAS operations via `compareAndSet()`.
Question 2: What is a happens-before relationship in the Java Memory Model?
- A guarantee that results of action A are visible to action B if A happens-before B (Correct answer)
- A constraint that prevents two threads from running simultaneously
- A compile-time ordering of statements within a single thread
- A JVM optimization that reorders memory accesses for performance
Correct answer: A guarantee that results of action A are visible to action B if A happens-before B
The happens-before relationship guarantees that memory writes by one action are visible to another action that follows it in the happens-before order.
Question 3: What exception is thrown when a thread waiting on a `Future.get()` is interrupted?
- InterruptedException (Correct answer)
- ExecutionException
- CancellationException
- TimeoutException
Correct answer: InterruptedException
`Future.get()` throws `InterruptedException` if the current thread is interrupted while waiting for the computation.
Question 4: Which method of `Semaphore` acquires a permit, blocking until one is available or the thread is interrupted?
- acquire() (Correct answer)
- tryAcquire()
- release()
- drainPermits()
Correct answer: acquire()
`acquire()` blocks indefinitely until a permit is available or the calling thread is interrupted.
Question 5: In a `ForkJoinPool`, what does a worker thread do when its own deque is empty?
- Steals tasks from the tail of another worker's deque (Correct answer)
- Blocks until the main thread submits new work
- Terminates and is replaced by a new thread
- Requests tasks from a central shared queue
Correct answer: Steals tasks from the tail of another worker's deque
Work-stealing allows idle workers to steal tasks from the tail (opposite end) of busy workers' deques to maximize CPU utilization.
Question 6: Which class is the preferred replacement for `Vector` in concurrent Java programs?
- CopyOnWriteArrayList (Correct answer)
- Collections.synchronizedList()
- ArrayList
- LinkedList
Correct answer: CopyOnWriteArrayList
`CopyOnWriteArrayList` is thread-safe for iteration without locking by creating a new copy of the array on each mutative operation.
Question 7: What is the result of calling `Thread.interrupted()` on a thread whose interrupt flag is set?
- Returns true and clears the interrupt flag (Correct answer)
- Returns true without clearing the interrupt flag
- Returns false and clears the interrupt flag
- Throws InterruptedException
Correct answer: Returns true and clears the interrupt flag
`Thread.interrupted()` is a static method that returns the current thread's interrupt status and then clears it.
Which `java.util.concurrent.atomic` class provides an atomic reference to an object that can be updated using compare-and-swap?