Java Programming — Multithreading
Java OOP, collections, multithreading
28 Questions 10 Topics Take Test
Advertisement
Showing 1–10 of 28 questions in Multithreading
Q.1 Hard Multithreading
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?
A False sharing due to cache line contention; use LongAdder instead
B Integer overflow; switch to AtomicLong
C Insufficient synchronization; add synchronized keyword
D Thread starvation; increase thread priority
Correct Answer:  A. False sharing due to cache line contention; use LongAdder instead
EXPLANATION

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.

Take Test
Q.2 Hard Multithreading
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?
A It is automatically reset to its initial value
B It retains the value from the previous task execution
C It becomes null
D It throws ThreadLocalException
Correct Answer:  B. It retains the value from the previous task execution
EXPLANATION

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.

Take Test
Q.3 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.

Take Test
Q.4 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.

Take Test
Q.5 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.

Take Test
Advertisement
Q.6 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.

Take Test
Q.7 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.

Take Test
Q.8 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.

Take Test
Q.9 Hard Multithreading
Consider a scenario with 3 threads updating a shared counter. Which synchronization mechanism is MOST efficient?
A synchronized block on counter
B AtomicInteger
C ReentrantLock
D Semaphore with permits=1
Correct Answer:  B. AtomicInteger
EXPLANATION

AtomicInteger uses lock-free CAS operations which are more efficient than synchronized blocks or locks for simple counter operations with multiple threads.

Take Test
Q.10 Hard Multithreading
In a producer-consumer problem, what is the ideal synchronization mechanism?
A BlockingQueue with synchronized wait/notify pattern
B Random sleep delays
C No synchronization
D Thread.yield() in loops
Correct Answer:  A. BlockingQueue with synchronized wait/notify pattern
EXPLANATION

BlockingQueue (like LinkedBlockingQueue) is purpose-built for producer-consumer patterns, handling synchronization and blocking elegantly.

Take 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