Java MCQ – Home
Java Basic MCQ
➤ Java Hello World MCQ
➤ Find Java Keywords
➤ Java Identifier Quiz
➤ Java Data Types Quiz
➤ If-Else MCQ in Java-1
➤ If-Else MCQ in Java-2
Object class MCQ
➤ Object Class Quiz
➤ equals() Method Quiz
➤ Hashcode Value Quiz
➤ toString() Method Quiz
➤ Clone() Method Quiz
Multithreading MCQ
➤ Define a Thread-1
➤ Define a Thread-2
➤ Get/set ThreadName
➤ Thread State MCQ
➤ Thread Priority MCQ
➤ Yield(), join() & sleep()
➤ Synchronization MCQ
➤ Interthread Comms
➤ Deadlock, Daemon
Exception Handling
➤ Exception Handling-1
➤ Exception Handling-2
➤ Exception Handling-3
➤ Java try-catch MCQ-1
➤ Java try-catch MCQ-2
➤ Java try-catch MCQ-3
➤ Nested try-catch MCQ
➤ throw Keyword MCQ
➤ finally Block MCQ-1
➤ finally Block MCQ-2
➤ throws Keyword MCQ
Generics MCQ
➤ Java Generics Quiz-1
➤ Java Generics Quiz-2
➤ Java Generics Quiz-3
Collection Framework
➤ Collections Quiz-1
➤ Collections Quiz-2
➤ ArrayList MCQ-1
➤ ArrayList MCQ-2
➤ LinkedList MCQ
➤ Vector Stack MCQ
➤ Java Cursors MCQ
➤ Java TreeSet MCQ
➤ TreeSet & Comparator
Java Multithreading MCQ Part-6 | Thread Priority MCQ | Also see:- Define Thread in Java Multithreading Interview Questions
Q1) Which method can be called to pause a thread execution until some other thread execution is completed?
a) yield() method
b) join() method
c) sleep() method
d) All of these
View Answer
Answer:- b) join() methodQ2) If we call the yield() method then in which state thread will enter?
a) TIMED_WAITING
b) WAITING
c) BLOCKED
d) TERMINATED
View Answer
Answer:- b) WAITINGQ3) What will be the output of the below program?
class MyThread extends Thread {
public void run() {
System.out.print("Child ");
}
}
public class Test {
public static void main(String[] args)
throws InterruptedException {
MyThread t = new MyThread();
t.start();
t.join();
System.out.print("Main ");
}
}
a) Main Child
b) Child Main
c) Main Main
d) Child Child
View Answer
Answer:- b) Child MainThe join() method is called on the child thread by the main thread, therefore the main thread will wait till completion of the child thread.
Q4) What will be the output of the below program?
class MyThread extends Thread {
public static Thread mt;
public void run() {
try {
mt.join();
} catch(InterruptedException ie) {}
System.out.print("Child ");
}
}
public class Test {
public static void main(String[] args)
throws InterruptedException {
MyThread.mt = Thread.currentThread();
MyThread t = new MyThread();
t.start();
Thread.sleep(1000);
System.out.print("Main ");
}
}
a) Main Child
b) Child Main
c) Main Main
d) Child Child
View Answer
Answer:- a) Main ChildThe join() method is called on the main thread by the child thread, therefore the child thread has to wait till completion of the main thread.
Q5) What will be the output of the below program?
class MyThread extends Thread {
public static Thread mt;
public void run() {
try {
mt.join();
} catch(InterruptedException ie) {}
System.out.print("Child ");
}
}
public class Test {
public static void main(String[] args)
throws InterruptedException {
MyThread.mt = Thread.currentThread();
MyThread t = new MyThread();
t.start();
t.join();
System.out.print("Main ");
}
}
a) Main Child
b) Child Main
c) Waiting for each other to complete the execution
d) Deadlock
View Answer
Answer:- c) Waiting for each other to complete the execution.The main thread is waiting for completion of the child thread and similarly the child thread is waiting for completion of the main thread. Hence, both will be waiting for each other forever. Note that it is not a deadlock situation. Deadlock occurs only because of synchronized keywords. You can verify, see more:- Deadlock in Lock, How to detect deadlock in Java.
Q6) What will be the output of the below program?
public class Test extends Thread {
public static void main(String[] args)
throws InterruptedException {
Thread.currentThread().join();
System.out.print("Main ");
}
}
a) Main
b) Main Main
c) Program Stuck, no output
d) Deadlock, no output
View Answer
Answer:- c) Program Stuck, no outputThe main thread itself is waiting for the completion of the main thread. Therefore the program will be stuck, and we won’t get any output. But this is not a deadlock situation. The deadlock situation came only because of a synchronized block.
Q7) After calling join(1000) thread will wait for?
a) Exactly 1 second
b) Minimum 1 second
c) Maximum 1 second
d) Will wait till child thread not completed
View Answer
Answer:- c) Maximum 1 secondOn calling join(1000), the thread will wait a maximum 1000 millisecond i.e. 1 second. Before 1 second child thread is completed then immediately the thread will start its execution. But if the child thread is not completed within 1 second, then the thread comes out of TIMED_WAITING state and tries to start its execution.
Q8) On calling sleep(1000) thread will wait for?
a) Exactly 1 second
b) Minimum 1 second
c) Maximum 1 second
d) Will wait till child thread not completed
View Answer
Answer:- a) Exactly 1 secondQ9) If a thread wants to pause its execution to give the chance for remaining threads of the same priority then which method should be called?
a) yield() method
b) join() method
c) sleep() method
d) All of these
View Answer
Answer:- a) yield() methodQ10) How can one thread interrupt another thread?
a) yield() method
b) join() method
c) sleep() method
d) interrupt() method
View Answer
Answer:- d) interrupt() methodIf you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!