OCP Java Concurrency 2 — Questions and Answers
Question 1: Which method of the `CountDownLatch` class blocks the calling thread until the count reaches zero?
- await() (Correct answer)
- countDown()
- reset()
- getCount()
Correct answer: await()
`await()` blocks the current thread until the latch's count reaches zero, while `countDown()` decrements the count.
Question 2: What happens when a thread calls `CyclicBarrier.await()` and it is the last thread to arrive at the barrier?
- It triggers the optional barrier action and releases all waiting threads (Correct answer)
- It blocks until an external thread calls reset()
- It throws a BrokenBarrierException
- It silently exits without releasing others
Correct answer: It triggers the optional barrier action and releases all waiting threads
The last thread to call `await()` optionally runs the barrier action, then all waiting threads are released.
Question 3: Which `ExecutorService` method submits a `Callable` and returns a `Future` representing the pending result?
- submit(Callable) (Correct answer)
- execute(Runnable)
- invokeAll(Collection)
- schedule(Callable, long, TimeUnit)
Correct answer: submit(Callable)
`submit(Callable)` accepts a `Callable` and returns a `Future<T>` that can be used to retrieve the result.
Question 4: What is the purpose of the `volatile` keyword in Java concurrency?
- Ensures visibility of variable changes across threads without atomicity guarantees (Correct answer)
- Makes a variable immutable
- Prevents two threads from accessing a variable simultaneously
- Provides atomic compound operations on a variable
Correct answer: Ensures visibility of variable changes across threads without atomicity guarantees
`volatile` guarantees that reads/writes go directly to main memory, ensuring visibility, but does not provide atomicity for compound operations.
Question 5: Which class in `java.util.concurrent` provides a resizable-array implementation of a blocking queue?
- ArrayBlockingQueue
- LinkedBlockingQueue (Correct answer)
- SynchronousQueue
- PriorityBlockingQueue
Correct answer: LinkedBlockingQueue
`LinkedBlockingQueue` is backed by linked nodes and can optionally be bounded, while `ArrayBlockingQueue` uses a fixed-size array.
Question 6: What does `Future.cancel(true)` do to a running task?
- Attempts to interrupt the thread executing the task (Correct answer)
- Immediately terminates the thread
- Waits for the task to complete then marks it cancelled
- Throws an exception in the task's thread
Correct answer: Attempts to interrupt the thread executing the task
Passing `true` to `cancel()` sends an interrupt signal to the thread running the task, but the task must check for interruption to stop.
Question 7: Which `ReentrantLock` feature allows a thread to acquire the lock only if it is not held by another thread at the time of invocation?
- tryLock() (Correct answer)
- lockInterruptibly()
- lock()
- newCondition()
Correct answer: tryLock()
`tryLock()` returns `true` immediately if the lock is available or `false` if another thread holds it, without blocking.
Which method of the `CountDownLatch` class blocks the calling thread until the count reaches zero?