Java Questions & Answers – Java.lang – ThreadGroup class & Runnable Interface

This set of Java Multiple Choice Questions & Answers (MCQs) focuses on “Java.lang – ThreadGroup class & Runnable Interface”.

1. Which of the interface contains all the methods used for handling thread related operations in Java?
a) Runnable interface
b) Math interface
c) System interface
d) ThreadHandling interface
View Answer

Answer: a
Explanation: Runnable interface defines all the methods for handling thread operations in Java.

2. Which of these class is used to make a thread?
a) String
b) System
c) Thread
d) Runnable
View Answer

Answer: c
Explanation: Thread class is used to make threads in java, Thread encapsulates a thread of execution. To create a new thread the program will either extend Thread or implement the Runnable interface.

3. Which of this interface is implemented by Thread class?
a) Runnable
b) Connections
c) Set
d) MapConnections
View Answer

Answer: a
Explanation: None.
advertisement
advertisement

4. Which of these methods of a Thread class is used to suspend a thread for a period of time?
a) sleep()
b) terminate()
c) suspend()
d) stop()
View Answer

Answer: a
Explanation: None.

5. What will be the output of the following Java program?

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
  1.     class newthread implements Runnable 
  2.     {
  3. 	Thread t1,t2;
  4.         newthread() 
  5.         {
  6.             t1 = new Thread(this,"Thread_1");
  7.             t2 = new Thread(this,"Thread_2");
  8.             t1.start();
  9.             t2.start();
  10. 	}
  11. 	public void run() 
  12.         {
  13.             t2.setPriority(Thread.MAX_PRIORITY);	
  14. 	    System.out.print(t1.equals(t2));
  15.         }    
  16.     }
  17.     class multithreaded_programing 
  18.     {
  19.         public static void main(String args[]) 
  20.         {
  21.             new newthread();        
  22.         }
  23.     }

a) true
b) false
c) truetrue
d) falsefalse
View Answer

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:

advertisement
$ javac multithreaded_programing.java
$ java multithreaded_programing
falsefalse

6. What will be the output of the following Java program?

advertisement
  1.     class newthread implements Runnable 
  2.     {
  3. 	Thread t;
  4.         newthread() 
  5.         {
  6.             t = new Thread(this,"New Thread");
  7.             t.start();
  8. 	}
  9. 	public void run()
  10.         {
  11.             t.setPriority(Thread.MAX_PRIORITY);	
  12.             System.out.println(t);
  13. 	}
  14.     }
  15.     class multithreaded_programing 
  16.     {
  17.         public static void main(String args[]) 
  18.         {
  19.             new newthread();        
  20.         }
  21.     }

a) Thread[New Thread,0,main]
b) Thread[New Thread,1,main]
c) Thread[New Thread,5,main]
d) Thread[New Thread,10,main]
View Answer

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:

$ javac multithreaded_programing.java
$ java multithreaded_programing
Thread[New Thread,10,main]

7. What will be the output of the following Java program?

  1.     class newthread implements Runnable 
  2.     {
  3. 	Thread t;
  4.         newthread() 
  5.         {
  6.             t = new Thread(this,"My Thread");
  7.             t.start();
  8. 	}
  9.     }
  10.     class multithreaded_programing 
  11.     {
  12.         public static void main(String args[]) 
  13.         {
  14.             new newthread();        
  15.         }
  16.     }

a) My Thread
b) Thread[My Thread,5,main]
c) Compilation Error
d) Runtime Error
View Answer

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:

$ javac multithreaded_programing.java
The type newthread must implement the inherited abstract method Runnable.run()

8. What will be the output of the following Java program?

  1.     class newthread implements Runnable 
  2.     {
  3. 	Thread t;
  4.         newthread() 
  5.         {
  6.             t = new Thread(this,"My Thread");
  7.             t.start();
  8. 	}
  9. 	public void run() 
  10.         {
  11. 	    System.out.println(t.getName());
  12. 	}
  13.     }
  14.     class multithreaded_programing 
  15.     {
  16.         public static void main(String args[]) 
  17.         {
  18.             new newthread();        
  19.         }
  20.     }

a) My Thread
b) Thread[My Thread,5,main]
c) Compilation Error
d) Runtime Error
View Answer

Answer: a
Explanation: None.
Output:

$ javac multithreaded_programing.java
$ java multithreaded_programing
My Thread

9. What will be the output of the following Java program?

  1.     class newthread implements Runnable 
  2.     {
  3. 	Thread t;
  4.         newthread() 
  5.         {
  6.             t = new Thread(this,"My Thread");
  7.             t.start();
  8. 	}
  9. 	public void run() 
  10.         {
  11. 	    System.out.println(t);
  12. 	}
  13.     }
  14.     class multithreaded_programing 
  15.     {
  16.         public static void main(String args[]) 
  17.         {
  18.             new newthread();        
  19.         }
  20.     }

a) My Thread
b) Thread[My Thread,5,main]
c) Compilation Error
d) Runtime Error
View Answer

Answer: b
Explanation: None.
Output:

$ javac multithreaded_programing.java
$ java multithreaded_programing
Thread[My Thread,5,main]

Sanfoundry Global Education & Learning Series – Java Programming Language.

To practice all areas of Java language, here is complete set on 1000+ Multiple Choice Questions and Answers on Java.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.