Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

476 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 301–310 of 476
Topics in Java Programming
Q.301 Medium Multithreading
What does the volatile keyword guarantee in Java multithreading?
A Visibility of changes across threads, but not atomicity
B Both visibility and atomicity
C Prevents thread creation
D Ensures mutual exclusion
Correct Answer:  A. Visibility of changes across threads, but not atomicity
EXPLANATION

volatile ensures that all threads see the most recent value of a variable (visibility) but does not provide atomicity for compound operations.

Take Test
Q.302 Medium Multithreading
In Java 21 Virtual Threads, what is the key advantage over platform threads?
A Virtual threads are lighter weight and millions can be created without OS limitations
B Virtual threads are faster than platform threads
C Virtual threads do not require synchronization
D Virtual threads cannot cause deadlocks
Correct Answer:  A. Virtual threads are lighter weight and millions can be created without OS limitations
EXPLANATION

Virtual threads (Project Loom) are extremely lightweight and allow creating millions of threads efficiently, unlike platform threads which are limited by OS resources.

Take Test
Q.303 Medium Multithreading
What happens if an exception is thrown inside a synchronized block?
A The lock is automatically released when exiting the block
B The lock is permanently held
C The exception propagates, but lock is not released
D The thread terminates immediately
Correct Answer:  A. The lock is automatically released when exiting the block
EXPLANATION

Java guarantees that the lock is released when exiting a synchronized block, whether normally or via an exception. This is why synchronized is considered safer than manual lock management.

Take Test
Q.304 Medium Multithreading
Which method is used to forcefully stop a thread in modern Java?
A Thread.stop() method
B Using a shared volatile boolean flag and checking it regularly
C Thread.interrupt() followed by checking isInterrupted()
D Both B and C are acceptable modern approaches
Correct Answer:  D. Both B and C are acceptable modern approaches
EXPLANATION

Thread.stop() is deprecated and unsafe. Modern approaches use either a volatile flag or Thread.interrupt() with proper handling. Both are valid and recommended depending on the scenario.

Take Test
Q.305 Medium Multithreading
What is the output of this code?

ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(() -> System.out.println("Task 1"));
executor.submit(() -> System.out.println("Task 2"));
executor.shutdown();
A Task 1 and Task 2 printed in any order
B Task 1 printed, then Task 2 printed
C No output
D Compilation error
Correct Answer:  B. Task 1 printed, then Task 2 printed
EXPLANATION

newSingleThreadExecutor() creates an executor with exactly one thread. Tasks are queued and executed sequentially in the order submitted. Task 1 will always execute before Task 2.

Take Test
Q.306 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.307 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.308 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.309 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.310 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
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