Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

270 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 161–170 of 270
Topics in Java Programming
Q.161 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.162 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.163 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.164 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.165 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.166 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.167 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.168 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
Q.169 Easy Multithreading
What is the primary purpose of the volatile keyword in Java multithreading?
A To prevent thread creation
B To ensure visibility of variable changes across threads without synchronization
C To make a variable immutable
D To automatically lock the variable
Correct Answer:  B. To ensure visibility of variable changes across threads without synchronization
EXPLANATION

The volatile keyword ensures that any read of a volatile variable will read the most recent write by any thread. It provides visibility guarantees but not atomicity, making it lighter than synchronization for simple flag checks.

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

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