1Z0-819 Exception Handling & Concurrency 4 — Questions and Answers
Question 1: What is a deadlock in concurrent programming?
- A thread that runs indefinitely without yielding
- A thread waiting for I/O that never completes
- Two or more threads each waiting for a lock held by the other (Correct answer)
- A condition where CPU scheduling starves a low-priority thread
Correct answer: Two or more threads each waiting for a lock held by the other
Deadlock is a circular wait condition where thread A holds lock X and wants Y, while thread B holds Y and wants X, causing both to block forever.
Question 2: Which method allows a thread to pause execution and give other threads a chance to run?
- Thread.sleep(0)
- Thread.yield() (Correct answer)
- Thread.wait()
- Thread.pause()
Correct answer: Thread.yield()
Thread.yield() hints to the scheduler that the current thread is willing to relinquish CPU time, though the scheduler may ignore this hint.
Question 3: What does the try-with-resources statement require of its resources?
- They must implement Serializable
- They must implement AutoCloseable (Correct answer)
- They must be declared final
- They must implement Closeable and be non-null
Correct answer: They must implement AutoCloseable
Resources in try-with-resources must implement AutoCloseable (or its subinterface Closeable), ensuring close() is called automatically.
Question 4: What is the difference between ConcurrentHashMap and Collections.synchronizedMap()?
- They are functionally identical
- ConcurrentHashMap uses segment-level locking for higher throughput; synchronizedMap locks the entire map per operation (Correct answer)
- ConcurrentHashMap does not allow null keys; synchronizedMap allows them
- Both A and C
Correct answer: ConcurrentHashMap uses segment-level locking for higher throughput; synchronizedMap locks the entire map per operation
ConcurrentHashMap uses internal striped locking for much better concurrent throughput, while synchronizedMap serializes all access with a single lock.
Question 5: Which checked exception does Thread.sleep() declare?
- IOException
- ExecutionException
- InterruptedException (Correct answer)
- ThreadException
Correct answer: InterruptedException
Thread.sleep(millis) declares throws InterruptedException so callers must handle the case where another thread interrupts the sleeping thread.
Question 6: What does the synchronized keyword guarantee when applied to an instance method?
- Only one thread can ever call any method on the class at once
- The method is atomic at the CPU instruction level
- Only one thread holds the intrinsic lock of 'this' while executing the method (Correct answer)
- The method cannot throw exceptions
Correct answer: Only one thread holds the intrinsic lock of 'this' while executing the method
synchronized on an instance method acquires the intrinsic monitor lock on 'this', preventing other synchronized methods on the same object from running concurrently.
Question 7: Which Future method retrieves the result of a Callable, blocking if necessary, and can throw ExecutionException?
- Future.run()
- Future.get() (Correct answer)
- Future.call()
- Future.result()
Correct answer: Future.get()
Future.get() blocks until the computation completes and wraps any exception thrown by the task in an ExecutionException.
What is a deadlock in concurrent programming?