Home Subjects Java Programming Multithreading

Java Programming
Multithreading

Java OOP, collections, multithreading

50 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 31–40 of 50
Topics in Java Programming
Q.31 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.

Test
Q.32 Medium Multithreading
In a multithreaded application, what does the term 'context switching' refer to?
A Creating new threads dynamically
B Switching between different objects in memory
C The OS switching CPU execution from one thread to another
D Changing the priority of a running thread
Correct Answer:  C. The OS switching CPU execution from one thread to another
EXPLANATION

Context switching is the OS mechanism of switching CPU execution between different threads, saving and restoring thread state.

Test
Q.33 Medium Multithreading
What is the main advantage of using ExecutorService over directly creating threads?
A Lower memory consumption
B Thread reuse and better resource management
C Faster execution speed
D No need for synchronization
Correct Answer:  B. Thread reuse and better resource management
EXPLANATION

ExecutorService manages a pool of threads, reusing them for multiple tasks, which reduces overhead and improves resource utilization.

Test
Q.34 Medium Multithreading
In the context of synchronized methods, what is acquired and released automatically?
A Semaphore
B Monitor lock (intrinsic lock)
C ReadWriteLock
D Condition
Correct Answer:  B. Monitor lock (intrinsic lock)
EXPLANATION

Synchronized methods automatically acquire and release the monitor lock (intrinsic lock) associated with the object, ensuring thread safety.

Test
Q.35 Medium Multithreading
Which method is used to check the current state of a thread in Java?
A getStatus()
B getState()
C checkState()
D isAlive()
Correct Answer:  B. getState()
EXPLANATION

The getState() method returns the Thread.State enum indicating whether a thread is NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, or TERMINATED.

Test
Q.36 Medium Multithreading
What happens if you call interrupt() on a thread that is not in a waiting or sleeping state?
A The thread is immediately terminated
B An InterruptedException is thrown
C The interrupt status flag is set and the thread continues execution
D The method throws a RuntimeException
Correct Answer:  C. The interrupt status flag is set and the thread continues execution
EXPLANATION

interrupt() sets the interrupt status flag. If the thread is not waiting or sleeping, it continues execution but the status is marked as interrupted.

Test
Q.37 Medium Multithreading
Which of the following is a thread-safe collection in Java that can be used without explicit synchronization?
A ArrayList
B HashMap
C ConcurrentHashMap
D TreeMap
Correct Answer:  C. ConcurrentHashMap
EXPLANATION

ConcurrentHashMap is designed for concurrent access and is thread-safe. ArrayList, HashMap, and TreeMap require external synchronization for thread safety.

Test
Q.38 Medium Multithreading
Consider a scenario where Thread A acquires Lock1 and tries to acquire Lock2, while Thread B acquires Lock2 and tries to acquire Lock1. What situation arises?
A Thread starvation
B Deadlock
C Livelock
D Race condition
Correct Answer:  B. Deadlock
EXPLANATION

When threads wait indefinitely for each other to release resources, it's a deadlock. This scenario is a classic deadlock example.

Test
Q.39 Medium Multithreading
What will be the output of the following code snippet?
java
class Test extends Thread {
public void run() {
System.out.print("T");
}
}
public class Main {
public static void main(String[] args) {
Test t = new Test();
t.run();
t.start();
}
}
A TT
B T
C Compilation error
D Runtime exception
Correct Answer:  A. TT
EXPLANATION

First t.run() executes directly (prints T), then t.start() creates a new thread and calls run() again (prints T). So output is TT.

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

Test
IGET
IGET AI
Online · Exam prep assistant
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