1Z0-819 Exception Handling & Concurrency 5 — Questions and Answers
Question 1: Which statement about the Error class hierarchy is correct?
- Error extends RuntimeException
- Error extends Exception
- Error extends Throwable directly (Correct answer)
- Error extends Exception and is always unchecked
Correct answer: Error extends Throwable directly
Error extends Throwable directly (not Exception), and errors like OutOfMemoryError and StackOverflowError represent serious JVM-level problems.
Question 2: What does CountDownLatch.await() do when the count reaches zero?
- It throws an exception to signal completion
- It resets the count and continues blocking
- It unblocks all waiting threads (Correct answer)
- It cancels all registered tasks
Correct answer: It unblocks all waiting threads
When countDown() reduces the latch's count to zero, all threads blocked on await() are released simultaneously.
Question 3: Which exception is thrown when you call Future.get() on a cancelled task?
- InterruptedException
- ExecutionException
- CancellationException (Correct answer)
- IllegalStateException
Correct answer: CancellationException
Calling get() on a Future that was cancelled via cancel(true) causes CancellationException to be thrown.
Question 4: What is the effect of calling Thread.interrupt() on a thread that is currently sleeping?
- The thread is immediately killed
- The thread continues sleeping and the interrupt flag is set
- The thread's sleep is interrupted and InterruptedException is thrown (Correct answer)
- The interrupt is queued until the sleep finishes
Correct answer: The thread's sleep is interrupted and InterruptedException is thrown
Interrupting a sleeping thread causes the sleep to terminate early by throwing InterruptedException and clearing the thread's interrupt status.
Question 5: Which method of Semaphore acquires a permit, blocking until one is available?
- Semaphore.lock()
- Semaphore.acquire() (Correct answer)
- Semaphore.take()
- Semaphore.wait()
Correct answer: Semaphore.acquire()
Semaphore.acquire() decrements the permit count, blocking if no permits are available until another thread calls release().
Question 6: What is the behavior of a FixedThreadPool created with Executors.newFixedThreadPool(3) when a 4th task is submitted while all 3 threads are busy?
- It throws RejectedExecutionException
- It creates a temporary 4th thread to handle the task
- The task is queued in an unbounded LinkedBlockingQueue (Correct answer)
- The calling thread executes the task inline
Correct answer: The task is queued in an unbounded LinkedBlockingQueue
newFixedThreadPool uses an unbounded LinkedBlockingQueue, so excess tasks wait in the queue rather than being rejected or creating new threads.
Question 7: Which exception type should you catch to handle both IOException and RuntimeException in a single catch block without multi-catch syntax?
- Throwable
- Error
- Exception (Correct answer)
- You cannot — they share no common subtype below Throwable
Correct answer: Exception
Both IOException and RuntimeException extend Exception, so catching Exception covers both, though it also catches other exception subtypes.
Which statement about the Error class hierarchy is correct?