Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 561–570 of 958
Topics in Java Programming
Q.561 Medium Multithreading
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?
A Virtual Threads with appropriate lock strategies
B Increasing thread pool size
C Using volatile keyword everywhere
D Converting all methods to static
Correct Answer:  A. Virtual Threads with appropriate lock strategies
EXPLANATION

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.

Test
Q.562 Medium Multithreading
In a producer-consumer scenario using BlockingQueue, what happens when a consumer thread calls take() on an empty queue?
A It throws NoSuchElementException immediately
B It blocks until a producer adds an element
C It returns null
D It spins in a busy-wait loop
Correct Answer:  B. It blocks until a producer adds an element
EXPLANATION

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().

Test
Q.563 Easy Multithreading
A developer needs to ensure that exactly 5 threads complete their tasks before proceeding to the next phase. Which synchronization utility is most appropriate?
A Semaphore
B CountDownLatch
C Phaser
D Mutex
Correct Answer:  B. CountDownLatch
EXPLANATION

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.

Test
Q.564 Easy Multithreading
Which of the following methods will cause a thread to release all locks it holds while waiting?
A wait()
B sleep()
C yield()
D join()
Correct Answer:  A. wait()
EXPLANATION

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.

Test
Q.565 Hard Multithreading
In the context of Java 21 Virtual Threads, what is a major limitation of traditional threading that Virtual Threads solve?
A Virtual Threads eliminate the need for synchronization
B Virtual Threads allow creating millions of lightweight threads with minimal memory overhead
C Virtual Threads make garbage collection unnecessary
D Virtual Threads automatically detect deadlocks
Correct Answer:  B. Virtual Threads allow creating millions of lightweight threads with minimal memory overhead
EXPLANATION

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.

Test
Q.566 Hard Multithreading
Which statement about StampedLock is TRUE?
A It always uses pessimistic locking
B It provides optimistic read locks without acquiring write lock
C It's a replacement for ReentrantLock in all scenarios
D It guarantees fairness like Fair ReentrantLock
Correct Answer:  B. It provides optimistic read locks without acquiring write lock
EXPLANATION

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.

Test
Q.567 Hard Multithreading
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();
A Caught
B RuntimeException is thrown to main thread
C No output, exception is silently ignored
D Compilation error
Correct Answer:  A. Caught
EXPLANATION

The UncaughtExceptionHandler is invoked when an exception is thrown in a thread and not caught. It will print 'Caught' before the thread terminates.

Test
Q.568 Hard Multithreading
In a ForkJoinPool, what is the primary advantage over ExecutorService for recursive tasks?
A Better exception handling
B Work-stealing algorithm for better load balancing
C Lower memory footprint
D Automatic task prioritization
Correct Answer:  B. Work-stealing algorithm for better load balancing
EXPLANATION

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.

Test
Q.569 Hard Multithreading
Which scenario can lead to livelock in multithreading?
A Two threads continuously change state in response to each other but never make progress
B One thread blocks another thread indefinitely
C Multiple threads access the same resource without synchronization
D A thread is waiting for a resource held by another waiting thread
Correct Answer:  A. Two threads continuously change state in response to each other but never make progress
EXPLANATION

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.

Test
Q.570 Hard Multithreading
What is the behavior of ReentrantReadWriteLock when multiple threads perform read operations?
A Only one thread can read at a time
B Multiple threads can read simultaneously, but writing locks them out
C Threads must alternate between read and write operations
D All threads must wait for exclusive lock
Correct Answer:  B. Multiple threads can read simultaneously, but writing locks them out
EXPLANATION

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.

Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips