1Z0-819 Exception Handling & Concurrency 2 — Questions and Answers
Question 1: What happens when both the try block and the finally block throw exceptions?
- The try block's exception propagates
- The finally block's exception propagates and the try block's exception is lost (Correct answer)
- Both exceptions propagate as a chain
- A CompoundException wraps both
Correct answer: The finally block's exception propagates and the try block's exception is lost
The exception thrown in the finally block replaces the one from the try block, causing the original exception to be silently lost.
Question 2: Which interface must a class implement to be used as the target of a Thread?
- Callable
- Runnable (Correct answer)
- Executable
- Task
Correct answer: Runnable
The Runnable functional interface (with its run() method) is the standard contract for thread task targets.
Question 3: What does ReentrantLock.tryLock() return if the lock is already held by another thread?
- It blocks until the lock is available
- It throws IllegalMonitorStateException
- It returns false immediately (Correct answer)
- It returns null
Correct answer: It returns false immediately
tryLock() is a non-blocking acquire attempt that returns false immediately if the lock cannot be obtained.
Question 4: Which exception is thrown when a thread is interrupted while waiting in Object.wait()?
- ThreadInterruptedException
- InterruptedException (Correct answer)
- IllegalThreadStateException
- ConcurrentModificationException
Correct answer: InterruptedException
Object.wait() declares throws InterruptedException, which is thrown when the waiting thread's interrupt status is set.
Question 5: What is the output of: try { throw new RuntimeException(); } catch (Exception e) { System.out.print("A"); } finally { System.out.print("B"); }
- A
- B
- AB (Correct answer)
- BA
Correct answer: AB
The catch block prints 'A', then the finally block always executes and prints 'B', giving 'AB'.
Question 6: Which class provides a thread-safe, resizable array implementation in Java?
- ArrayList
- Vector
- CopyOnWriteArrayList (Correct answer)
- LinkedList
Correct answer: CopyOnWriteArrayList
CopyOnWriteArrayList is the modern thread-safe list that creates a fresh copy of the backing array on every mutation.
Question 7: What is the purpose of the volatile keyword on a variable?
- It makes the variable immutable after first assignment
- It ensures atomic compound operations like i++
- It guarantees visibility of writes across threads without caching (Correct answer)
- It prevents the variable from being garbage collected
Correct answer: It guarantees visibility of writes across threads without caching
volatile ensures all threads see the most recent write by bypassing CPU caches, but does not make compound operations atomic.
What happens when both the try block and the finally block throw exceptions?