Showing 91–100 of 100 questions
in 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.
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.
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.
Which method is called to make a thread wait until another thread completes?
A
wait()
B
join()
C
sleep()
D
yield()
Correct Answer:
B. join()
EXPLANATION
The join() method causes the current thread to wait until the thread on which it is called completes its execution.
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.
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.
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.
Which method is used to stop a thread in Java?
A
stop()
B
interrupt()
C
terminate()
D
kill()
Correct Answer:
B. interrupt()
EXPLANATION
The interrupt() method is the proper way to stop a thread in Java. The stop() method is deprecated and dangerous.
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.
Which interface must be implemented to create a thread in Java?
A
Runnable
B
Threadable
C
Executable
D
Processable
Correct Answer:
A. Runnable
EXPLANATION
The Runnable interface is the correct interface to implement for creating threads in Java. It has a single abstract method run().