Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

476 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 311–320 of 476
Topics in Java Programming
Q.311 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.312 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.313 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.

Take Test
Q.314 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.

Take Test
Q.315 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.

Take Test
Q.316 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.

Take Test
Q.317 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.

Take Test
Q.318 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.

Take Test
Q.319 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.

Take Test
Q.320 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.

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