Java Questions & Answers – Thread class

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

1. 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.

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

Answer: a
Explanation: None.

3. 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.

5. 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.

6. 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.

7. What is the output of this program?

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

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:
$ javac multithreaded_programing.java
$ java multithreaded_programing
Thread[New Thread,5,main]

8. What is the priority of the thread in output of this program?

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

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:
$ javac multithreaded_programing.java
$ java multithreaded_programing
New Thread

9. What is the name of the thread in output of this program?

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

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

10. What is the name of the thread in output of this program?

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

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

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

About

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.


Sanfoundry Training Center of Excellence (CoE)

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.







Subscribe Newsletter & Posts
Sanfoundry Internships
Follow Manish & Sanfoundry