What does the volatile keyword guarantee in multithreading?
Answer: B
volatile ensures that changes to a variable are immediately visible to all threads and prevents the JVM from reordering instructions. However, it doesn't make operations atomic.
Q.382Medium
In Java 21, which new feature was introduced for concurrent programming?
Answer: A
Java 21 introduced Virtual Threads as part of Project Loom, which are lightweight threads that make it easier to write scalable concurrent applications.
Q.383Medium
What is the output of this code snippet?
ExecutorService es = Executors.newFixedThreadPool(2);
es.execute(() -> System.out.println("Task 1"));
es.shutdown();
Answer: A
shutdown() gracefully shuts down the executor service by rejecting new tasks but allowing submitted tasks to complete. Task 1 is already submitted before shutdown(), so it will execute.
Q.384Medium
What exception does CyclicBarrier throw when a thread is interrupted while waiting?
Answer: A
When a thread waiting at a CyclicBarrier is interrupted, it throws InterruptedException. This breaks the barrier and causes other waiting threads to receive BrokenBarrierException.
Q.385Hard
Consider a scenario with 3 threads updating a shared counter. Which synchronization mechanism is MOST efficient?
Answer: B
AtomicInteger uses lock-free CAS operations which are more efficient than synchronized blocks or locks for simple counter operations with multiple threads.
Advertisement
Q.386Hard
What is the behavior of ReentrantReadWriteLock when multiple threads perform read operations?
Answer: B
ReentrantReadWriteLock allows multiple threads to acquire the read lock simultaneously, but only one thread can hold the write lock. This improves concurrency for read-heavy workloads.
Q.387Hard
Which scenario can lead to livelock in multithreading?
Answer: A
Livelock occurs when threads are not blocked but continuously change state in response to each other (like two people trying to pass each other), preventing progress. This differs from deadlock where threads are blocked.
Q.388Hard
In a ForkJoinPool, what is the primary advantage over ExecutorService for recursive tasks?
Answer: B
ForkJoinPool uses a work-stealing algorithm where idle threads can 'steal' tasks from busy threads' queues, providing better load balancing for divide-and-conquer problems.
Q.389Hard
What is the output of the following code?
Thread t = new Thread(() -> { throw new RuntimeException("Error"); });
t.setUncaughtExceptionHandler((thread, ex) -> System.out.println("Caught"));
t.start();
Answer: A
The UncaughtExceptionHandler is invoked when an exception is thrown in a thread and not caught. It will print 'Caught' before the thread terminates.
Q.390Hard
Which statement about StampedLock is TRUE?
Answer: B
StampedLock provides optimistic reads that don't require acquiring a lock. If the data changes during an optimistic read, validation fails and a pessimistic lock can be acquired.
Q.391Hard
In the context of Java 21 Virtual Threads, what is a major limitation of traditional threading that Virtual Threads solve?
Answer: B
Virtual Threads are lightweight and can be created in large numbers (millions) with minimal memory footprint, unlike platform threads which are heavy OS-level constructs. This solves scalability issues in high-concurrency applications.
Q.392Easy
Which of the following methods will cause a thread to release all locks it holds while waiting?
Answer: A
The wait() method causes a thread to release all acquired locks and enter a waiting state until notify() or notifyAll() is called. sleep(), yield(), and join() do not release locks.
Q.393Easy
A developer needs to ensure that exactly 5 threads complete their tasks before proceeding to the next phase. Which synchronization utility is most appropriate?
Answer: B
CountDownLatch is designed for one-time barrier scenarios where threads wait for a countdown to reach zero. With an initial count of 5, it perfectly suits this use case. Semaphore is reusable, Phaser handles multiple phases, and Mutex is not a Java class.
Q.394Medium
In a producer-consumer scenario using BlockingQueue, what happens when a consumer thread calls take() on an empty queue?
Answer: B
BlockingQueue's take() method blocks the calling thread until an element becomes available. This is safer and more efficient than busy-waiting. NoSuchElementException is thrown by non-blocking operations like remove().
Q.395Medium
A multi-threaded application experiences poor performance despite having adequate CPU cores. Code inspection reveals frequent calls to synchronized blocks on shared objects. Which modern Java feature (2024-25) could optimize this without major refactoring?
Answer: A
Virtual Threads (Project Loom, stable in Java 21+) allow massive parallelism with lightweight threads, reducing contention overhead. They enable better scaling than increasing pool size. volatile doesn't help with synchronized block contention, and static conversion is inappropriate.
Q.396Hard
Consider a ThreadLocal variable initialized in a thread pool executor with 10 threads. If the same thread is reused from the pool for a different task, what is the state of its ThreadLocal variable?
Answer: B
ThreadLocal values persist across task executions in the same thread. ThreadPools reuse threads, so previous ThreadLocal values remain unless explicitly removed. This can cause data leakage. Developers must call remove() to clean up.
Q.397Hard
A high-frequency trading system uses AtomicInteger for concurrent counter updates. However, performance degrades as more threads access the same counter. What is the primary cause and best solution?
Answer: A
AtomicInteger suffers from false sharing when multiple threads frequently update adjacent memory locations on the same CPU cache line. LongAdder uses striped updates across multiple cells, reducing contention. This is a 2024-25 performance optimization best practice for high-concurrency scenarios.
Q.398Easy
Which of the following is a checked exception in Java?
Answer: B
IOException is a checked exception that must be caught or declared in the method signature. NullPointerException, ArrayIndexOutOfBoundsException, and ArithmeticException are unchecked exceptions.
Q.399Medium
What is the output of the following code?
int x = 10;
try {
x = x / 0;
} catch(ArithmeticException e) {
x = x + 5;
} finally {
x = x * 2;
}
System.out.println(x);
Answer: B
ArithmeticException is caught, x becomes 10+5=15. Finally block executes, x becomes 15*2=30. Finally block always executes regardless of exception.
Q.400Easy
Which exception is thrown when a method receives an illegal or inappropriate argument?
Answer: B
IllegalArgumentException is thrown to indicate that a method has been passed an illegal or inappropriate argument. It is an unchecked exception.