Best Reference Books

This set of Java MCQs focuses on “Implementing Runnable interface for Threads”. 

1.  Which of these method is used to implement Runnable interface? 
a) stop()
b) run()
c) runThread()
d) stopThread()View Answer

Answer: b
Explanation: To implement Runnable interface, a class needs only to implement a single method called run(). 

2.  Which of these interface is implemented by Thread class? 
a) Runnable
b) Connections
c) Set
d) MapConnectionsView Answer

Answer: a
Explanation: None. 

3.  Which of these method is used to begin the execution of a thread? 
a) run()
b) start()
c) runThread()
d) startThread()View Answer

Answer: b
Explanation: None. 

4.  Which of these method waits for the thread to terminate? 
a) sleep()
b) isAlive()
c) join()
d) stop()View Answer

Answer: c
Explanation: None. 

5.  Which of these statement is incorrect? 
a) A thread can be formed by implementing Runnable interface only
b) A thread can be formed by a class that extends Thread class
c) start() method is used to begin execution of the thread
d) run() method is used to begin execution of a thread before start() method in special casesView Answer

Answer: d
Explanation: run() method is used to define the code that constitutes the new thread, it contains the code to be executed.  start() method is used to begin execution of the thread that is execution of run().  run() itself is never used for starting execution of the thread. 

Answer: c
Explanation: Thread t has been made by using Runnable interface, hence it is necessary to use inherited abstract method run() method to specify instructions to be implemented on the thread, since no run() method is used it gives a compilation error. 
Output:

Answer: d
Explanation: Thread t has been made with default priority value 5 but in run method the priority has been explicitly changed to MAX_PRIORITY of class thread, that is 10 by code ‘t.setPriority(Thread. MAX_PRIORITY);’ using the setPriority function of thread t.
Output:

Answer: d
Explanation: Threads t1 & t2 are created by class newthread that is implementing runnable interface, hence both the threads are provided their own run() method specifying the actions to be taken.  When constructor of newthread class is called first the run() method of t1 executes than the run method of t2 printing 2 times “false” as both the threads are not equal one is having different priority than other, hence falsefalse is printed. 
Output: