Java MCQ Questions - Threads
This section focuses on the "Threads" in Java programming. These Multiple Choice Questions (MCQ) should be practiced to improve the Java programming skills required for various interviews (campus interviews, walk-in interviews, company interviews), placements and other competitive examinations.
1. What is the name of the method used to start a thread execution?
A. run();
B. init();
C. start();
D. resume();
View Answer
Ans : C
Explanation: The start() method causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.
2. Which cannot directly cause a thread to stop executing?
A. Calling the SetPriority() method on a Thread object.
B. Calling read() method on an InputStream object.
C. Calling notify() method on an object.
D. Calling the wait() method on an object.
View Answer
Ans : C
Explanation: notify() - wakes up a single thread that is waiting on this object's monitor.
3. Which of the following will directly stop the execution of a Thread?
A. notify()
B. notifyall()
C. wait()
D. exits synchronized code
View Answer
Ans : C
Explanation: . wait() causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.
4. Which function of pre defined class Thread is used to check weather current thread being checked is still running?
A. isAlive()
B. Alive()
C. isRunning()
D. Join()
View Answer
Ans : 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.
5. Which method must be defined by a class implementing the java.lang.Runnable interface?
A. public void run()
B. void run()
C. void run(int priority)
D. public void start()
View Answer
Ans : A
Explanation: In an interface all methods are abstract by default therefore they must be overridden by the implementing class. The Runnable interface only contains 1 method, the void run() method therefore it must be implemented.
6. Assume the following method is properly synchronized and called from a thread A on an object B:
wait(2000);
After calling this method, when will the thread A become a candidate to get another turn at the CPU?
A. After thread A is notified, or after two seconds.
B. Two seconds after thread A is notified.
C. After the lock on B is released, or after two seconds.
D. Two seconds after lock B is released.
View Answer
Ans : A
Explanation: Either of the two events (notification or wait time expiration) will make the thread become a candidate for running again.
7. Which will contain the body of the thread?
A. main();
B. stop();
C. start();
D. run();
View Answer
Ans : D
Explanation: The run() method to a thread is like the main() method to an application. Starting the thread causes the object's run method to be called in that separately executing thread.
8. Which class or interface defines the wait(), notify(),and notifyAll() methods?
A. Object
B. Class
C. Runnable
D. Thread
View Answer
Ans : A
Explanation: The Object class defines these thread-specific methods.
9. Which of these method of Thread class is used to find out the priority given to a thread?
A. ThreadPriority()
B. get()
C. getPriority()
D. getThreadPriority()
View Answer
Ans : C
Explanation: getPriority() method of Thread class is used to find out the priority given to a thread.
10. Which of these method of Thread class is used to Suspend a thread for a period of time?
A. stop()
B. sleep()
C. terminate()
D. suspend()
View Answer
Ans : B
Explanation: sleep() method of Thread class is used to Suspend a thread for a period of time.
11. Which of the following line of code is suitable to start a thread ?
class X implements Runnable
{
public static void main(String args[])
{
/* Missing code? */
}
public void run() {}
}
A. Thread t = new Thread(X);
B. Thread t = new Thread(X); t.start();
C. X run = new X(); Thread t = new Thread(run); t.start();
D. Thread t = new Thread(); x.run();
View Answer
Ans : C
Explanation: C is suitable to start a thread.
12. What will be the output of the program?
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
Ans : D
Explanation: No explanation.
13. Number of threads in below java program is:
public class ThreadExtended extends Thread {
public void run() {
System.out.println("Thread is running no");
}
public static void main(String[] args)
{
ThreadExtended threadE = new ThreadExtended();
threadE.start();
}
}
A. 0
B. 1
C. 2
D. 3
View Answer
Ans : C
Explanation: Main program is also run as a thread. And, program has created one child thread. Hence, total 2 threads are there in the program.
14. which of these will create and start this thread?
public class MyRunnable implements Runnable
{
public void run()
{
// some code here
}
}
A. new Runnable(MyRunnable).start();
B. new Thread(MyRunnable).run();
C. new Thread(new MyRunnable()).start();
D. new MyRunnable().start();
View Answer
Ans : C
Explanation: The class implements Runnable, an instance of it has to be passed to the Thread constructor, and then the instance of the Thread has to be started.
15. What is the priority of the thread in output of this 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
Ans : 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.
16. What will be the output of the program?
class MyThread extends Thread
{
public static void main(String [] args)
{
MyThread t = new MyThread();
t.start();
System.out.print("one. ");
t.start();
System.out.print("two. ");
}
public void run()
{
System.out.print("Thread ");
}
}
A. Compilation fails
B. An exception occurs at runtime.
C. It prints "Thread one. Thread two."
D. The output cannot be determined.
View Answer
Ans : B
Explanation: When the start() method is attempted a second time on a single Thread object, the method will throw an IllegalThreadStateException (you will not need to know this exception name for the exam). Even if the thread has finished running, it is still illegal to call start() again.
17. What is the name of the thread in output of this program?
class multithreaded_programing
{
public static void main(String args[])
{
Thread t = Thread.currentThread();
System.out.println(t.getPriority());
}
}
A. 1
B. 4
C. 0
D. 5
View Answer
Ans : D
Explanation: The default priority given to a thread is 5.
18. What is the name of the thread in output of this program?
class multithreaded_programing
{
public static void main(String args[])
{
Thread t = Thread.currentThread();
System.out.println(t.isAlive());
}
}
A. 1
B. 0
C. TRUE
D. FALSE
View Answer
Ans : C
Explanation: Thread t is seeded to currently program, hence when you run the program the thread becomes active & code t.isAlive returns true.
19. The following block of code creates a Thread using a Runnable target:Which of the following classes can be used to create the target, so that the preceding code compiles correctly?
Runnable target = new MyRunnable();
Thread myThread = new Thread(target);
A. public class MyRunnable extends Object{public void run(){}}
B. public class MyRunnable implements Runnable{void run(){}}
C. public class MyRunnable implements Runnable{public void run(){}}
D. public class MyRunnable extends Runnable{public void run(){}}
View Answer
Ans : C
Explanation: The class correctly implements the Runnable interface with a legal public void run() method.
20. The static method Thread.currentThread() returns a reference to the currently executing Thread object. What is the result of this code?
class Test
{
public static void main(String [] args)
{
printAll(args);
}
public static void printAll(String[] lines)
{
for(int i = 0; i < lines.length; i++)
{
System.out.println(lines[i]);
Thread.currentThread().sleep(1000);
}
}
}
A. Each String in the array lines will output, and there is no guarantee there will be a pause because currentThread() may not retrieve this thread.
B. Each String in the array lines will output, with no pause in between because this method is not executed in a Thread.
C. Each String in the array lines will output, with a 1-second pause.
D. This code will not compile.
View Answer
Ans : D
Explanation: The sleep() method must be enclosed in a try/catch block, or the method printAll() must declare it throws the InterruptedException.
21. What is multithreaded programming?
A. It is a process in which two different processes run simultaneously
B. It’s a process in which a single process can access information from many sources
C. It is a process in which two or more parts of same process run simultaneously
D. It is a process in which many different process are able to access same information
View Answer
Ans : C
Explanation: Multithreaded programming a process in which two or more parts of the same process run simultaneously.
22. Which of these are types of multitasking?
A. Process based
B. Thread based
C. Process and Thread based
D. None of the mentioned
View Answer
Ans : C
Explanation: There are two types of multitasking: Process based multitasking and Thread based multitasking.
23. Thread priority in Java is?
A. Integer
B. Float
C. double
D. long
View Answer
Ans : A
Explanation: Java assigns to each thread a priority that determines hoe that thread should be treated with respect to others. Thread priority is integers that specify relative priority of one thread to another.
24. What will happen if two thread of the same priority are called to be processed simultaneously?
A. Anyone will be executed first lexographically
B. Both of them will be executed simultaneously
C. None of them will be executed
D. It is dependent on the operating system
View Answer
Ans : D
Explanation: In cases where two or more thread with same priority are competing for CPU cycles, different operating system handle this situation differently. Some execute them in time sliced manner some depending on the thread they call.
25. Which of these statements is incorrect?
A. By multithreading CPU idle time is minimized, and we can take maximum use of it
B. By multitasking CPU idle time is minimized, and we can take maximum use of it
C. Two thread in Java can have the same priority
D. A thread can exist only in two states, running and blocked
View Answer
Ans : D
Explanation: Thread exist in several states, a thread can be running, suspended, blocked, terminated & ready to run.
26. What requires less resources?
A. Thread
B. Process
C. Thread and Process
D. Neither Thread nor Process
View Answer
Ans : A
Explanation: Thread is a lightweight and requires less resources to create and exist in the process. Thread shares the process resources.
27. What does not prevent JVM from terminating?
A. Process
B. Daemon Thread
C. User Thread
D. JVM Thread
View Answer
Ans : B
Explanation: Daemon thread runs in the background and does not prevent JVM from terminating. Child of daemon thread is also daemon thread.
28. What decides thread priority?
A. Process
B. Process scheduler
C. Thread
D. Thread scheduler
View Answer
Ans : D
Explanation: Thread scheduler decides the priority of the thread execution. This cannot guarantee that higher priority thread will be executed first, it depends on thread scheduler implementation that is OS dependent.
29. What is true about time slicing?
A. Time slicing is OS service that allocates CPU time to available runnable thread
B. Time slicing is the process to divide the available CPU time to available runnable thread
C. Time slicing depends on its implementation in OS
D. Time slicing allocates more resources to thread
View Answer
Ans : B
Explanation: Time slicing is the process to divide the available CPU time to available runnable thread.
30. Deadlock is a situation when thread is waiting for other thread to release acquired object.
A. TRUE
B. FALSE
C. Can be true or false
D. can not say
View Answer
Ans : A
Explanation: Deadlock is java programming situation where one thread waits for an object lock that is acquired by other thread and vice-versa.
Also check :
Discussion