Java Programming — Multithreading
Java OOP, collections, multithreading
100 Questions 10 Topics Take Test
Advertisement
Showing 81–90 of 100 questions in Multithreading
Q.81 Hard Multithreading
Consider code where Thread A holds Lock1 and waits for Lock2, while Thread B holds Lock2 and waits for Lock1. What is this scenario called?
A Race condition
B Starvation
C Deadlock
D Livelock
Correct Answer:  C. Deadlock
EXPLANATION

This is a classic circular wait scenario resulting in deadlock, where neither thread can proceed because each is waiting for a resource held by the other.

Take Test
Q.82 Medium Multithreading
What is thread pooling and which class is commonly used for it?
A Creating individual threads, Thread class
B Reusing a fixed set of threads, ExecutorService
C Synchronizing threads, Synchronized class
D Managing thread priority, ThreadGroup
Correct Answer:  B. Reusing a fixed set of threads, ExecutorService
EXPLANATION

Thread pooling reuses a fixed set of threads instead of creating new ones for each task. ExecutorService provides convenient methods for thread pool management.

Take Test
Q.83 Hard Multithreading
Which interface allows multiple threads to access a resource with a permit system?
A Runnable
B Semaphore
C Lock
D Executor
Correct Answer:  B. Semaphore
EXPLANATION

Semaphore is a synchronization utility that uses a counter to control access to a shared resource. Threads can acquire and release permits.

Take Test
Q.84 Hard Multithreading
What will happen if you call sleep() inside a synchronized block?
A The lock is released during sleep
B The lock is retained during sleep, blocking other threads
C An exception is thrown
D The synchronized block is skipped
Correct Answer:  B. The lock is retained during sleep, blocking other threads
EXPLANATION

When sleep() is called inside a synchronized block, the thread retains the lock on the object, preventing other threads from entering the synchronized section.

Take Test
Q.85 Hard Multithreading
How does the volatile keyword help in multithreading?
A It prevents race conditions completely
B It ensures visibility of variable changes across threads without full synchronization
C It increases thread speed
D It prevents deadlocks
Correct Answer:  B. It ensures visibility of variable changes across threads without full synchronization
EXPLANATION

The volatile keyword ensures that changes to a variable are immediately visible to all threads, providing memory visibility without the overhead of full synchronization.

Take Test
Q.86 Medium Multithreading
What is a deadlock in multithreading?
A When a thread terminates unexpectedly
B When two or more threads are blocked indefinitely, waiting for each other
C When a thread has the lowest priority
D When a thread accesses a null pointer
Correct Answer:  B. When two or more threads are blocked indefinitely, waiting for each other
EXPLANATION

A deadlock occurs when two or more threads are blocked forever, waiting for each other to release resources they need.

Take Test
Q.87 Easy Multithreading
Which of the following is a valid way to create a thread in Java?
A Extend Thread class and override run() method
B Implement Runnable interface and override run() method
C Both A and B are valid
D Neither A nor B is valid
Correct Answer:  C. Both A and B are valid
EXPLANATION

Both extending Thread class and implementing Runnable interface are valid approaches to create threads in Java.

Take Test
Q.88 Medium Multithreading
What is a daemon thread in Java?
A A thread that runs in the background and doesn't prevent JVM shutdown
B A thread that has the highest priority
C A thread that cannot be interrupted
D A thread that runs system services
Correct Answer:  A. A thread that runs in the background and doesn't prevent JVM shutdown
EXPLANATION

A daemon thread runs in the background and doesn't prevent the JVM from exiting. When all non-daemon threads finish, the JVM exits even if daemon threads are running.

Take Test
Q.89 Hard Multithreading
What happens when wait() is called from a thread that doesn't hold the lock?
A The thread waits indefinitely
B IllegalMonitorStateException is thrown
C The program exits
D The thread continues normally
Correct Answer:  B. IllegalMonitorStateException is thrown
EXPLANATION

wait() must be called from within a synchronized context. Calling it without holding the lock throws IllegalMonitorStateException.

Take Test
Q.90 Medium Multithreading
Which class provides thread-safe operations on shared variables in Java?
A Thread
B Runnable
C AtomicInteger
D Synchronizer
Correct Answer:  C. AtomicInteger
EXPLANATION

The AtomicInteger class (and other Atomic classes) provides thread-safe operations without explicit synchronization using low-level atomic operations.

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