Home Subjects Java Programming Multithreading

Java Programming
Multithreading

Java OOP, collections, multithreading

22 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 1–10 of 22
Topics in Java Programming
Q.1 Easy Multithreading
A developer needs to ensure that exactly 5 threads complete their tasks before proceeding to the next phase. Which synchronization utility is most appropriate?
A Semaphore
B CountDownLatch
C Phaser
D Mutex
Correct Answer:  B. CountDownLatch
EXPLANATION

CountDownLatch is designed for one-time barrier scenarios where threads wait for a countdown to reach zero. With an initial count of 5, it perfectly suits this use case. Semaphore is reusable, Phaser handles multiple phases, and Mutex is not a Java class.

Test
Q.2 Easy Multithreading
Which of the following methods will cause a thread to release all locks it holds while waiting?
A wait()
B sleep()
C yield()
D join()
Correct Answer:  A. wait()
EXPLANATION

The wait() method causes a thread to release all acquired locks and enter a waiting state until notify() or notifyAll() is called. sleep(), yield(), and join() do not release locks.

Test
Q.3 Easy Multithreading
Which of the following thread states is NOT a valid Java thread state?
A RUNNABLE
B BLOCKED
C SUSPENDED
D TERMINATED
Correct Answer:  C. SUSPENDED
EXPLANATION

Java thread states are: NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, and TERMINATED. SUSPENDED is not a valid state in Java's threading model.

Test
Q.4 Easy Multithreading
What is the difference between start() and run() methods in threading?
A start() creates a new thread, run() executes in the same thread
B run() creates a new thread, start() executes in the same thread
C Both are identical
D start() is deprecated in Java 21
Correct Answer:  A. start() creates a new thread, run() executes in the same thread
EXPLANATION

start() creates a new thread and calls run() in that new thread, while directly calling run() executes it in the current thread without creating a new thread.

Test
Q.5 Easy Multithreading
Which method must be implemented to create a thread in Java?
A start()
B run()
C execute()
D begin()
Correct Answer:  B. run()
EXPLANATION

The run() method must be implemented when creating a thread either by extending Thread class or implementing Runnable interface. The start() method calls run() internally.

Test
Q.6 Easy Multithreading
What is the output of the following code?
Thread t = new Thread(() -> System.out.println(Thread.currentThread().getName()));
t.start();
A Thread-0
B main
C Compilation error
D Runtime exception
Correct Answer:  A. Thread-0
EXPLANATION

When a new Thread is created without a name parameter, it gets a default name 'Thread-n' where n is a counter. The thread will print 'Thread-0' as it is the first thread created.

Test
Q.7 Easy Multithreading
Which of the following collections is thread-safe in Java?
A ConcurrentHashMap
B HashMap
C TreeMap
D LinkedHashMap
Correct Answer:  A. ConcurrentHashMap
EXPLANATION

ConcurrentHashMap is designed for concurrent access without requiring external synchronization. HashMap and others are not thread-safe.

Test
Q.8 Easy Multithreading
Which method is used to prevent race conditions by allowing only one thread to access a resource at a time?
A synchronized
B volatile
C atomic
D transient
Correct Answer:  A. synchronized
EXPLANATION

The synchronized keyword creates a critical section that only one thread can access at a time, preventing race conditions. volatile ensures visibility but not atomicity.

Test
Q.9 Easy Multithreading
Which collection class is thread-safe without explicit synchronization?
A ArrayList
B HashMap
C ConcurrentHashMap
D TreeMap
Correct Answer:  C. ConcurrentHashMap
EXPLANATION

ConcurrentHashMap uses segment-based locking (in older versions) or fine-grained locking to provide thread-safety without synchronizing the entire map. Other options require external synchronization or Collections.synchronizedMap().

Test
Q.10 Easy Multithreading
Which of the following will cause a deadlock situation?
A Two threads waiting for locks held by each other in a circular manner
B A thread acquiring the same lock twice
C A thread calling Thread.sleep() while holding a lock
D Using ExecutorService with a fixed thread pool
Correct Answer:  A. Two threads waiting for locks held by each other in a circular manner
EXPLANATION

Deadlock occurs when two or more threads are waiting indefinitely for locks held by each other. Option B creates a reentrant situation (allowed in ReentrantLock). Option C is problematic but not necessarily deadlock. Option D is safe if used properly.

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