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? Answer: c 2. Which of these method of Thread class is used to Suspend a thread for a period of time? Answer: a 3. Which function of pre defined class Thread is used to check weather current thread being checked is still running? Answer: a 4. What will be the output of the following Java code? a) Thread[5,main] 5. What is the priority of the thread in output in the following Java program? a) main 6. What is the name of the thread in output in the following Java program? a) 0 7. What is the name of the thread in output in the following Java program? a) 0
a) get()
b) ThreadPriority()
c) getPriority()
d) getThreadPriority()
Clarification: None.
a) sleep()
b) terminate()
c) suspend()
d) stop()
Clarification: None.
a) isAlive()
b) Join()
c) isRunning()
d) Alive()
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.
class multithreaded_programing
{
public static void main(String args[])
{
Thread t = Thread.currentThread();
t.setName("New Thread");
System.out.println(t);
}
}
b) Thread[New Thread,5]
c) Thread[main,5,main]
d) Thread[New Thread,5,main]
Clarification: None.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
Thread[New Thread,5,main]
class multithreaded_programing
{
public static void main(String args[])
{
Thread t = Thread.currentThread();
t.setName("New Thread");
System.out.println(t.getName());
}
}
b) Thread
c) New Thread
d) Thread[New Thread,5,main]
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
class multithreaded_programing
{
public static void main(String args[])
{
Thread t = Thread.currentThread();
System.out.println(t.getPriority());
}
}
b) 1
c) 4
d) 5
Clarification: The default priority given to a thread is 5.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
5
class multithreaded_programing
{
public static void main(String args[])
{
Thread t = Thread.currentThread();
System.out.println(t.isAlive());
}
}
b) 1
c) true
d) false
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