Which method in Collections can create a thread-safe Map from a non-thread-safe Map?
Answer: A
Collections.synchronizedMap() wraps a Map with synchronized access. However, iteration still requires external synchronization.
Q.62Hard
What is the main advantage of EnumSet over HashSet when working with Enum constants?
Answer: B
EnumSet is specialized for Enum constants and uses bit vectors internally, providing O(1) operations with minimal memory overhead.
Q.63Hard
Consider this code: List<String> list = new CopyOnWriteArrayList<>(); list.add("A"); list.add("B"); Iterator<String> itr = list.iterator(); list.add("C"); System.out.println(itr.next());
Answer: B
CopyOnWriteArrayList creates a snapshot for iterators, so modifications after iterator creation don't affect the iteration. Iterator sees only original elements: A and B.
Q.64Hard
In Java 2024, if you need a collection that automatically removes entries based on garbage collection patterns, which would you choose?
Answer: A
WeakHashMap uses weak references for keys. When a key is no longer referenced elsewhere, it becomes eligible for garbage collection and is automatically removed from the map.
Q.65Hard
What happens when wait() is called from a thread that doesn't hold the lock?
Answer: B
wait() must be called from within a synchronized context. Calling it without holding the lock throws IllegalMonitorStateException.
Advertisement
Q.66Hard
How does the volatile keyword help in multithreading?
Answer: B
The volatile keyword ensures that changes to a variable are immediately visible to all threads, providing memory visibility without the overhead of full synchronization.
Q.67Hard
What will happen if you call sleep() inside a synchronized block?
Answer: B
When sleep() is called inside a synchronized block, the thread retains the lock on the object, preventing other threads from entering the synchronized section.
Q.68Hard
Which interface allows multiple threads to access a resource with a permit system?
Answer: B
Semaphore is a synchronization utility that uses a counter to control access to a shared resource. Threads can acquire and release permits.
Q.69Hard
Consider code where Thread A holds Lock1 and waits for Lock2, while Thread B holds Lock2 and waits for Lock1. What is this scenario called?
Answer: C
This is a classic circular wait scenario resulting in deadlock, where neither thread can proceed because each is waiting for a resource held by the other.
Q.70Hard
Consider the following code. What will be the likely behavior?
java
synchronized void method1() { method2(); }
synchronized void method2() { }
Answer: C
Java's intrinsic locks are reentrant, meaning the same thread can acquire the same lock multiple times. The thread can call method2() from method1() without deadlock.
Q.71Hard
Which class is used to synchronize the execution of multiple threads at a specific point?
Answer: B
CyclicBarrier allows multiple threads to wait for each other at a specific point. CountDownLatch is for one-time synchronization; CyclicBarrier is reusable.
Q.72Hard
What is the difference between CountDownLatch and CyclicBarrier?
Answer: B
CountDownLatch is used for one-time synchronization where threads wait for a countdown to reach zero. CyclicBarrier can be reused after threads are released.
Q.73Hard
What will happen if a thread tries to acquire a lock it already holds while inside a synchronized method?
Answer: C
Java's monitors are reentrant. A thread can acquire the same lock multiple times. An internal counter tracks lock acquisitions and releases.
Q.74Hard
In a high-concurrency scenario with 2024-25 exam patterns, which Java construct provides the best lock-free approach for thread safety?
Answer: C
Atomic variables use Compare-And-Swap (CAS) operations for lock-free thread safety, providing better performance in high-concurrency scenarios compared to traditional locking.
Q.75Hard
What is the primary advantage of using ReentrantLock over synchronized?
Answer: B
ReentrantLock provides more control with tryLock() for non-blocking attempts, Condition objects for complex wait/notify patterns, and fairness policy. It's more flexible than synchronized.
Q.76Hard
In a high-traffic web application using Java 21 Virtual Threads, what is the main performance benefit?
Answer: B
Virtual Threads are lightweight (millions can run) with minimal memory overhead compared to platform threads (thousands). They automatically handle I/O blocking without thread creation, making them ideal for high-concurrency scenarios.
Q.77Hard
Which pattern should be used to safely publish data from one thread to another?
Answer: B
Safe publication requires using visibility mechanisms: synchronized (mutual exclusion), volatile (visibility), or thread-safe collections (both). Direct assignment without synchronization causes visibility issues in the memory model.
Q.78Hard
What is the purpose of ForkJoinPool in Java?
Answer: B
ForkJoinPool is designed for divide-and-conquer tasks where large problems are split (fork) into smaller subtasks and results are combined (join). It's optimized for recursive parallel algorithms.
Q.79Hard
If a thread is blocked waiting for I/O, what happens when interrupt() is called on it?
Answer: B
Calling interrupt() on a blocked thread sets the interrupt flag. If the blocking operation supports interruption (like sleep(), join(), wait()), it throws InterruptedException. Non-interruptible I/O blocks require other mechanisms.
Q.80Hard
Consider a scenario: Thread A is waiting in wait() inside a synchronized block. Thread B calls notify(). What is the state of Thread A?
Answer: A
After notify(), the waiting thread transitions to runnable state, but it must still acquire the lock before resuming execution within the synchronized block.