250+ TOP MCQs on Thread class and Answers

Java MCQs 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()

Answer: c
Clarification: 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()

Answer: a
Clarification: 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()

Answer: a
Clarification: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.

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]

Answer: d
Clarification: None.
Output:

$ 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?

  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]

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

$ 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

Answer: d
Clarification: 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

Answer: c
Clarification: 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

Leave a Reply

Your email address will not be published. Required fields are marked *