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 41–50 of 50
Topics in Java Programming
Q.41 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.

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

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

Test
Q.44 Medium Multithreading
What is the difference between notify() and notifyAll()?
A notify() wakes one waiting thread, notifyAll() wakes all waiting threads
B notifyAll() wakes one thread, notify() wakes all threads
C They are identical in functionality
D notify() is for static methods, notifyAll() is for instance methods
Correct Answer:  A. notify() wakes one waiting thread, notifyAll() wakes all waiting threads
EXPLANATION

notify() wakes up a single thread that is waiting on the object's monitor, while notifyAll() wakes up all threads waiting on that object.

Test
Q.45 Medium Multithreading
Consider a synchronized method. Can multiple threads call it simultaneously on the same object?
A Yes, multiple threads can call it anytime
B No, only one thread can execute it at a time for the same object
C Yes, if they are different thread types
D Only if the method is static
Correct Answer:  B. No, only one thread can execute it at a time for the same object
EXPLANATION

A synchronized method locks the object, allowing only one thread to execute it at a time for that object instance.

Test
Q.46 Medium Multithreading
What is a race condition in multithreading?
A When threads compete for CPU resources
B When the outcome of a program depends on the timing of thread execution
C When a thread runs faster than others
D When threads communicate with each other
Correct Answer:  B. When the outcome of a program depends on the timing of thread execution
EXPLANATION

A race condition occurs when multiple threads access shared data and the final result depends on the order of execution, which is unpredictable.

Test
Q.47 Medium Multithreading
What will be the output of calling start() multiple times on the same thread object?
A Thread will run multiple times
B IllegalThreadStateException will be thrown
C Thread will run once and ignore subsequent calls
D It depends on the JVM implementation
Correct Answer:  B. IllegalThreadStateException will be thrown
EXPLANATION

Calling start() on an already started thread throws IllegalThreadStateException because a thread can only be started once.

Test
Q.48 Medium Multithreading
Which of the following is NOT a thread state in Java?
A NEW
B RUNNABLE
C WAITING
D SLEEPING
Correct Answer:  D. SLEEPING
EXPLANATION

The thread states are NEW, RUNNABLE, RUNNING, BLOCKED, WAITING, TIMED_WAITING, and TERMINATED. SLEEPING is not an official state.

Test
Q.49 Medium Multithreading
What is the purpose of the synchronized keyword in Java multithreading?
A To increase thread speed
B To prevent thread interference and memory consistency errors
C To create new threads automatically
D To pause all threads
Correct Answer:  B. To prevent thread interference and memory consistency errors
EXPLANATION

The synchronized keyword provides mutual exclusion to ensure that only one thread can access a resource at a time, preventing race conditions.

Test
Q.50 Medium Multithreading
What is the difference between Thread.sleep() and Thread.yield()?
A sleep() pauses the thread, yield() makes it runnable again
B yield() pauses the thread, sleep() makes it runnable again
C sleep() causes the thread to sleep for a specified time, yield() hints to scheduler to give chance to other threads
D Both are identical in functionality
Correct Answer:  C. sleep() causes the thread to sleep for a specified time, yield() hints to scheduler to give chance to other threads
EXPLANATION

Thread.sleep() pauses execution for a specified milliseconds, while yield() is a hint to the scheduler that the current thread is willing to yield its turn.

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