1Z0-819 Exception Handling & Concurrency 3 — Questions and Answers
Question 1: Which method of ExecutorService blocks until all submitted tasks complete or a timeout occurs?
- shutdown()
- shutdownNow()
- awaitTermination(long, TimeUnit) (Correct answer)
- invokeAll()
Correct answer: awaitTermination(long, TimeUnit)
awaitTermination() blocks the calling thread until the executor terminates or the specified timeout elapses, returning a boolean result.
Question 2: What kind of exception is java.io.IOException?
- Unchecked exception
- Error
- Checked exception (Correct answer)
- Runtime exception
Correct answer: Checked exception
IOException extends Exception (not RuntimeException), making it a checked exception that must be declared or handled.
Question 3: What does Callable differ from Runnable in terms of return value and exceptions?
- Callable returns void and cannot throw checked exceptions
- Callable returns a typed value and can throw checked exceptions (Correct answer)
- Callable returns a Future and cannot throw exceptions
- Callable and Runnable are identical interfaces
Correct answer: Callable returns a typed value and can throw checked exceptions
Callable<V> defines call() which returns V and declares throws Exception, unlike Runnable's run() which returns void and cannot throw checked exceptions.
Question 4: Which exception is thrown by BlockingQueue.take() when the current thread is interrupted?
- RuntimeException
- IllegalStateException
- InterruptedException (Correct answer)
- NoSuchElementException
Correct answer: InterruptedException
BlockingQueue.take() blocks until an element is available and throws InterruptedException if the thread is interrupted while waiting.
Question 5: In a multi-catch block 'catch (IOException | SQLException e)', what is the type of 'e'?
- Object
- Exception
- The common supertype of IOException and SQLException
- A union type that can only be rethrown (Correct answer)
Correct answer: A union type that can only be rethrown
In a multi-catch, the variable is implicitly final and typed as the union; it can only be rethrown as-is, not reassigned.
Question 6: What is a race condition?
- When two threads run at exactly the same CPU clock speed
- When program output depends on unpredictable relative timing of threads (Correct answer)
- When a thread acquires locks in wrong order causing deadlock
- When a thread loops faster than the JVM scheduler can handle
Correct answer: When program output depends on unpredictable relative timing of threads
A race condition occurs when correctness depends on the relative execution order of concurrent threads, which is non-deterministic.
Question 7: Which atomic class provides a compareAndSet(expected, update) method for lock-free integer updates?
- AtomicInteger (Correct answer)
- SynchronizedInteger
- VolatileInteger
- ConcurrentInteger
Correct answer: AtomicInteger
AtomicInteger in java.util.concurrent.atomic provides compareAndSet() (CAS) for lock-free, thread-safe integer manipulation.
Which method of ExecutorService blocks until all submitted tasks complete or a timeout occurs?