yield(), join() and sleep() Methods MCQ

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() method

Q2) 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) WAITING

Q3) 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 Main

The 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 Child

The 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 output

The 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 second

On 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 second

Q9) 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() method

Q10) How can one thread interrupt another thread?

a) yield() method
b) join() method
c) sleep() method
d) interrupt() method

View Answer Answer:- d) interrupt() method

If 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!

Leave a Comment

Your email address will not be published. Required fields are marked *