Home Subjects Java Programming Multithreading

Java Programming
Multithreading

Java OOP, collections, multithreading

100 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 61–70 of 100
Topics in Java Programming
Q.61 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.62 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
Q.63 Easy Multithreading
Which of the following best describes the purpose of the Runnable interface in Java?
A It automatically creates multiple threads
B It provides a way to define code that can be executed by a thread
C It manages thread lifecycle
D It provides synchronization mechanisms
Correct Answer:  B. It provides a way to define code that can be executed by a thread
EXPLANATION

The Runnable interface contains a single run() method that defines the code to be executed when a thread runs. It's preferred over extending Thread class.

Take Test
Q.64 Hard Multithreading
What will happen if a thread tries to acquire a lock it already holds while inside a synchronized method?
A The thread will be blocked indefinitely
B An exception will be thrown
C The thread will successfully acquire the lock again (reentrant behavior)
D The thread will skip the synchronized block
Correct Answer:  C. The thread will successfully acquire the lock again (reentrant behavior)
EXPLANATION

Java's monitors are reentrant. A thread can acquire the same lock multiple times. An internal counter tracks lock acquisitions and releases.

Take Test
Q.65 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.66 Hard Multithreading
What is the difference between CountDownLatch and CyclicBarrier?
A No functional difference
B CountDownLatch is one-time use; CyclicBarrier is reusable
C CyclicBarrier is deprecated in Java 2024
D CountDownLatch works with ExecutorService only
Correct Answer:  B. CountDownLatch is one-time use; CyclicBarrier is reusable
EXPLANATION

CountDownLatch is used for one-time synchronization where threads wait for a countdown to reach zero. CyclicBarrier can be reused after threads are released.

Take Test
Q.67 Hard Multithreading
Which class is used to synchronize the execution of multiple threads at a specific point?
A Semaphore
B CyclicBarrier
C CountDownLatch
D Condition
Correct Answer:  B. CyclicBarrier
EXPLANATION

CyclicBarrier allows multiple threads to wait for each other at a specific point. CountDownLatch is for one-time synchronization; CyclicBarrier is reusable.

Take Test
Q.68 Hard Multithreading
Consider the following code. What will be the likely behavior?
java
synchronized void method1() { method2(); }
synchronized void method2() { }
A Deadlock will occur
B ReentrantException will be thrown
C Code will execute without issues due to reentrant locks
D Compilation error
Correct Answer:  C. Code will execute without issues due to reentrant locks
EXPLANATION

Java's intrinsic locks are reentrant, meaning the same thread can acquire the same lock multiple times. The thread can call method2() from method1() without deadlock.

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