Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 641–650 of 958
Topics in Java Programming
Q.641 Hard Multithreading
Which interface allows multiple threads to access a resource with a permit system?
A Runnable
B Semaphore
C Lock
D Executor
Correct Answer:  B. Semaphore
EXPLANATION

Semaphore is a synchronization utility that uses a counter to control access to a shared resource. Threads can acquire and release permits.

Take Test
Q.642 Hard Multithreading
What will happen if you call sleep() inside a synchronized block?
A The lock is released during sleep
B The lock is retained during sleep, blocking other threads
C An exception is thrown
D The synchronized block is skipped
Correct Answer:  B. The lock is retained during sleep, blocking other threads
EXPLANATION

When sleep() is called inside a synchronized block, the thread retains the lock on the object, preventing other threads from entering the synchronized section.

Take Test
Q.643 Hard Multithreading
How does the volatile keyword help in multithreading?
A It prevents race conditions completely
B It ensures visibility of variable changes across threads without full synchronization
C It increases thread speed
D It prevents deadlocks
Correct Answer:  B. It ensures visibility of variable changes across threads without full synchronization
EXPLANATION

The volatile keyword ensures that changes to a variable are immediately visible to all threads, providing memory visibility without the overhead of full synchronization.

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

Take Test
Q.645 Easy Multithreading
Which of the following is a valid way to create a thread in Java?
A Extend Thread class and override run() method
B Implement Runnable interface and override run() method
C Both A and B are valid
D Neither A nor B is valid
Correct Answer:  C. Both A and B are valid
EXPLANATION

Both extending Thread class and implementing Runnable interface are valid approaches to create threads in Java.

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

Take Test
Q.647 Hard Multithreading
What happens when wait() is called from a thread that doesn't hold the lock?
A The thread waits indefinitely
B IllegalMonitorStateException is thrown
C The program exits
D The thread continues normally
Correct Answer:  B. IllegalMonitorStateException is thrown
EXPLANATION

wait() must be called from within a synchronized context. Calling it without holding the lock throws IllegalMonitorStateException.

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

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

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

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