Java Questions & Answers – Thread class

This section of our 1000+ Java MCQs focuses on Thread class of Java Programming Language.

1. Which of these method of Thread class is used to find out the priority given to a thread?
a) get()
b) ThreadPriority()
c) getPriority()
d) getThreadPriority()
View Answer

Answer: c
Explanation: None.

2. Which of these method of 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.

3. Which function of pre defined class Thread is used to check weather current thread being checked is still running?
a) isAlive()
b) Join()
c) isRunning()
d) Alive()
View Answer

Answer: a
Explanation:isAlive() function is defined in class Thread, it is used for implementing multithreading and to check whether the thread called upon is still running or not.
advertisement
advertisement

4. What will be the output of the following Java code?

  1.     class multithreaded_programing
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             Thread t = Thread.currentThread();
  6.             t.setName("New Thread");
  7.             System.out.println(t);        
  8.         }
  9.     }

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

Answer: d
Explanation: None.
Output:

Note: Join free Sanfoundry classes at Telegram or Youtube
$ javac multithreaded_programing.java
$ java multithreaded_programing
Thread[New Thread,5,main]

5. What is the priority of the thread in output in the following Java program?

advertisement
  1.     class multithreaded_programing
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             Thread t = Thread.currentThread();
  6.             t.setName("New Thread");
  7.             System.out.println(t.getName());        
  8.         }
  9.     }

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

Answer: c
Explanation: The getName() function is used to obtain the name of the thread, in this code the name given to thread is ‘New Thread’.
Output:

advertisement
$ javac multithreaded_programing.java
$ java multithreaded_programing
New Thread

6. What is the name of the thread in output in the following Java program?

  1.     class multithreaded_programing
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             Thread t = Thread.currentThread();
  6.             System.out.println(t.getPriority());        
  7.         }
  8.     }

a) 0
b) 1
c) 4
d) 5
View Answer

Answer: d
Explanation: The default priority given to a thread is 5.
Output:

$ javac multithreaded_programing.java
$ java multithreaded_programing
5

7. What is the name of the thread in output in the following Java program?

  1.     class multithreaded_programing
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             Thread t = Thread.currentThread();
  6.             System.out.println(t.isAlive());        
  7.         }
  8.     }

a) 0
b) 1
c) true
d) false
View Answer

Answer: c
Explanation: Thread t is seeded to currently program, hence when you run the program the thread becomes active & code ‘t.isAlive’ returns true.
Output:

$ javac multithreaded_programing.java
$ java multithreaded_programing
true

Sanfoundry Global Education & Learning Series – Java Programming Language.

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.