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
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
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
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.
4. What will be the output of the following Java code?
class multithreaded_programing
{
public static void main(String args[])
{
Thread t = Thread.currentThread();
t.setName("New Thread");
System.out.println(t);
}
}
a) Thread[5,main]
b) Thread[New Thread,5]
c) Thread[main,5,main]
d) Thread[New Thread,5,main]
View Answer
Explanation: 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?
class multithreaded_programing
{
public static void main(String args[])
{
Thread t = Thread.currentThread();
t.setName("New Thread");
System.out.println(t.getName());
}
}
a) main
b) Thread
c) New Thread
d) Thread[New Thread,5,main]
View Answer
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
6. What is the name of the thread in output in the following Java program?
class multithreaded_programing
{
public static void main(String args[])
{
Thread t = Thread.currentThread();
System.out.println(t.getPriority());
}
}
a) 0
b) 1
c) 4
d) 5
View Answer
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?
class multithreaded_programing
{
public static void main(String args[])
{
Thread t = Thread.currentThread();
System.out.println(t.isAlive());
}
}
a) 0
b) 1
c) true
d) false
View Answer
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]
- Practice Information Technology MCQs
- Apply for Java Internship
- Practice Programming MCQs
- Apply for Computer Science Internship
- Check Programming Books