Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

270 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 171–180 of 270
Topics in Java Programming
Q.171 Easy Multithreading
Which of the following best describes the purpose of the Runnable interface in Java?
A It automatically creates multiple threads
B It provides a way to define code that can be executed by a thread
C It manages thread lifecycle
D It provides synchronization mechanisms
Correct Answer:  B. It provides a way to define code that can be executed by a thread
EXPLANATION

The Runnable interface contains a single run() method that defines the code to be executed when a thread runs. It's preferred over extending Thread class.

Take Test
Q.172 Easy Multithreading
What is the return type of the join() method in Java threading?
A Thread
B boolean
C void
D int
Correct Answer:  C. void
EXPLANATION

The join() method returns void. It causes the calling thread to wait until the thread on which it is called completes its execution.

Take Test
Q.173 Easy Multithreading
Which exception is thrown when a thread is interrupted while waiting?
A ThreadException
B InterruptedException
C WaitException
D SleepException
Correct Answer:  B. InterruptedException
EXPLANATION

InterruptedException is thrown when a thread is interrupted during sleep(), wait(), or join() operations.

Take Test
Q.174 Easy Multithreading
Which of the following statements about the notify() method is correct?
A It wakes up all waiting threads simultaneously
B It wakes up only one random waiting thread
C It terminates the current thread immediately
D It pauses the current thread indefinitely
Correct Answer:  B. It wakes up only one random waiting thread
EXPLANATION

notify() wakes up a single arbitrary thread that is waiting on the same monitor. notifyAll() wakes up all waiting threads.

Take Test
Q.175 Easy Multithreading
What is the primary purpose of the synchronized keyword in Java?
A To increase thread execution speed
B To prevent race conditions by ensuring mutual exclusion
C To create new threads automatically
D To handle exceptions in multithreaded environments
Correct Answer:  B. To prevent race conditions by ensuring mutual exclusion
EXPLANATION

The synchronized keyword ensures that only one thread can execute a critical section at a time, preventing race conditions and data inconsistency.

Take Test
Q.176 Easy Multithreading
In Java multithreading, which method is used to pause the execution of a thread for a specified number of milliseconds without releasing locks?
A Thread.sleep()
B Thread.pause()
C Thread.halt()
D Thread.suspend()
Correct Answer:  A. Thread.sleep()
EXPLANATION

Thread.sleep() pauses thread execution for specified milliseconds while retaining all locks. pause(), halt(), and suspend() are not standard Java methods for this purpose.

Take Test
Q.177 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.178 Easy Multithreading
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.

Take Test
Q.179 Easy Multithreading
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.

Take Test
Q.180 Easy Multithreading
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().

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