Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 611–620 of 958
Topics in Java Programming
Q.611 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.612 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.613 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.614 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.615 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.616 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.617 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.618 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
Q.619 Easy Multithreading
Which of the following statements about Java threads is correct?
A A thread can be started multiple times using the start() method
B Once a thread completes execution, it can be restarted by calling start() again
C A thread can only be in one state at a time
D The run() method should be called directly to start a thread
Correct Answer:  C. A thread can only be in one state at a time
EXPLANATION

A thread can only exist in one state at a time (NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, or TERMINATED). Calling start() multiple times throws IllegalThreadStateException. The start() method should be called, not run() directly.

Take Test
Q.620 Hard Multithreading
In a high-concurrency scenario with 2024-25 exam patterns, which Java construct provides the best lock-free approach for thread safety?
A synchronized blocks
B ReentrantReadWriteLock
C AtomicInteger and similar atomic variables
D synchronized collections
Correct Answer:  C. AtomicInteger and similar atomic variables
EXPLANATION

Atomic variables use Compare-And-Swap (CAS) operations for lock-free thread safety, providing better performance in high-concurrency scenarios compared to traditional locking.

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