Home Subjects Java Programming Multithreading

Java Programming
Multithreading

Java OOP, collections, multithreading

100 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 51–60 of 100
Topics in Java Programming
Q.51 Medium Multithreading
In a scenario where Thread A holds Lock1 and waits for Lock2, while Thread B holds Lock2 and waits for Lock1, what is this called?
A Race condition
B Deadlock
C Starvation
D Livelock
Correct Answer:  B. Deadlock
EXPLANATION

This is a classic deadlock scenario involving circular wait. Race condition involves competing for resources. Starvation is when a thread never gets the resource. Livelock is when threads keep changing states without progress.

Take Test
Q.52 Easy Multithreading
Which collection class is thread-safe without explicit synchronization?
A ArrayList
B HashMap
C ConcurrentHashMap
D TreeMap
Correct Answer:  C. ConcurrentHashMap
EXPLANATION

ConcurrentHashMap uses segment-based locking (in older versions) or fine-grained locking to provide thread-safety without synchronizing the entire map. Other options require external synchronization or Collections.synchronizedMap().

Take Test
Q.53 Medium Multithreading
What happens when a thread acquires a lock on an object and then calls wait()?
A The thread continues holding the lock and waits
B The thread releases the lock and enters the waiting pool
C The thread is terminated
D An IllegalMonitorStateException is thrown immediately
Correct Answer:  B. The thread releases the lock and enters the waiting pool
EXPLANATION

When wait() is called, the thread releases the lock it holds on the object and enters the waiting pool. Another thread can then acquire the lock. The thread re-acquires the lock when notified.

Take Test
Q.54 Medium Multithreading
Which of the following best describes CyclicBarrier?
A It allows N threads to wait for each other at a barrier point
B It decrements a counter until it reaches zero
C It prevents threads from accessing a shared resource
D It executes a task periodically in a separate thread
Correct Answer:  A. It allows N threads to wait for each other at a barrier point
EXPLANATION

CyclicBarrier is a synchronizer that allows a fixed number of threads to wait for each other at a barrier point. Once all threads reach the barrier, they proceed together. It's reusable (cyclic) unlike CountDownLatch.

Take Test
Q.55 Medium Multithreading
What is the difference between notify() and notifyAll() in Java?
A notify() wakes one waiting thread; notifyAll() wakes all waiting threads
B They are identical and can be used interchangeably
C notify() is for synchronized methods; notifyAll() is for synchronized blocks
D notifyAll() is deprecated in Java 21
Correct Answer:  A. notify() wakes one waiting thread; notifyAll() wakes all waiting threads
EXPLANATION

notify() randomly selects one waiting thread to wake up, while notifyAll() wakes all waiting threads. notifyAll() is generally safer to avoid missed notifications when multiple threads are waiting on the same condition.

Take Test
Q.56 Medium Multithreading
Which interface would you use to submit multiple tasks and wait for all of them to complete?
A Runnable
B Callable with ExecutorService.invokeAll()
C Thread
D Semaphore
Correct Answer:  B. Callable with ExecutorService.invokeAll()
EXPLANATION

Callable interface returns a result via Future, and ExecutorService.invokeAll() waits for all submitted tasks to complete. This is the standard pattern for parallel task execution with result collection.

Take Test
Q.57 Medium Multithreading
What will be the output of the following code?

Object lock = new Object();
synchronized(lock) {
synchronized(lock) {
System.out.println("Nested");
}
}
A Will compile and print 'Nested' successfully
B Will cause a deadlock
C Will throw an IllegalMonitorStateException
D Will throw a CompileTimeException
Correct Answer:  A. Will compile and print 'Nested' successfully
EXPLANATION

Java supports reentrant locks on synchronized blocks. The same thread can acquire the same lock multiple times. The lock is released only when the outermost synchronized block exits.

Take Test
Q.58 Medium Multithreading
In Java 21 (latest), which is a modern approach to create thread-safe operations?
A Using synchronized keyword exclusively
B Using Virtual Threads (Project Loom) with structured concurrency
C Avoiding multithreading altogether
D Using only volatile variables
Correct Answer:  B. Using Virtual Threads (Project Loom) with structured concurrency
EXPLANATION

Java 21 introduced Virtual Threads as a lightweight threading model under Project Loom, allowing millions of concurrent tasks. This is more efficient than platform threads for high-concurrency scenarios.

Take Test
Q.59 Easy Multithreading
Which of the following will cause a deadlock situation?
A Two threads waiting for locks held by each other in a circular manner
B A thread acquiring the same lock twice
C A thread calling Thread.sleep() while holding a lock
D Using ExecutorService with a fixed thread pool
Correct Answer:  A. Two threads waiting for locks held by each other in a circular manner
EXPLANATION

Deadlock occurs when two or more threads are waiting indefinitely for locks held by each other. Option B creates a reentrant situation (allowed in ReentrantLock). Option C is problematic but not necessarily deadlock. Option D is safe if used properly.

Take Test
Q.60 Easy Multithreading
What is the primary purpose of the volatile keyword in Java multithreading?
A To prevent thread creation
B To ensure visibility of variable changes across threads without synchronization
C To make a variable immutable
D To automatically lock the variable
Correct Answer:  B. To ensure visibility of variable changes across threads without synchronization
EXPLANATION

The volatile keyword ensures that any read of a volatile variable will read the most recent write by any thread. It provides visibility guarantees but not atomicity, making it lighter than synchronization for simple flag checks.

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