MCQs on Thread Class

By Anjnee Bhatnagar|Updated : June 16th, 2022

MCQ Quiz questions for thread class of Java are available with the answers and their detailed solutions. You can download the free PDF of Thread class MCQs to enhance the preparation. Objective-based MCQ questions and answers will help in the complete study of the Java Thread class topic.

1. Which of the following directly stops the execution of a thread?

  1. notify()
  2. wait()
  3. notifyall()
  4. exists synchronized code

Answer: Option B

Solution: The best choice is B. The wait() method makes the active thread wait until another thread calls this object notify() or notifyAll() methods.

Option A is incorrect. A single thread that is waiting on this object's monitor is roused by the notify() function.

Option C is incorrect. Notifies every thread that is waiting on this object's monitor by calling notifyAll().

Option D is incorrect. The thread that is now holding the lock, or the thread that is in the synchronized method, typically quits the synchronized method when a lock is released. Until another thread enters a synchronized procedure on that object, the lock is then released. 

2. The method that must be defined by a class implementing the lang.runnable interface?

  1. void run()
  2. public void start()
  3. void run(int priority)
  4. public void run()

Answer: Option D

Solution: The proper choice is D since, by default, all methods in an interface are abstract and must be overridden by the class that implements the interface. Only the void run() function, the sole method of the Runnable interface, has to be implemented. Because they narrow the access privileges—that is, package(default) access is smaller than public access—Options A and C are wrong. Option B is wrong because it is not a method in the Runnable interface.

3.Which of the following will contain the body of the thread?

  1. run();
  2. start();
  3. stop();
  4. main();

Answer: Option A

Solution: The best choice is option A. A thread's run() method is comparable to an application's main() method. The run method of the object is called in that independently running thread as soon as the thread is started. Option B is incorrect. This thread's run method is called by the Java Virtual Machine when the start() method is invoked. Option C is incorrect. Deprecated is the stop() method. It compels the thread to halt its operation. Option D is incorrect; main () is a program's primary point of entry.

4.Which method is used to start the thread execution?

  1. init();
  2. resume();
  3. run();
  4. start();

Answer: Option D

Solution: The right answer is D. This thread's run method is called by the Java Virtual Machine when the start() method is invoked. Option A is incorrect. The Thread class doesn't have an init() function. Option C is incorrect. A thread's run() method is comparable to an application's main() method. The run method of the object is called in that independently running thread as soon as the thread is started. Option B is incorrect. Deprecated is the resume() method. A suspended thread is picked back up.

5. Which cannot directly cause a thread to stop executing?

  1. Calling the SetPriority()method on a Thread
  2. Calling the wait()method on an object.
  3. Calling notify()method on an object.
  4. Calling read()method on an InputStream

Answer: Option C

Solution: The correct answer is choice C which is notify() function call that wakes up a single thread which is waiting on its object.

Comments

write a comment

Follow us for latest updates