Java Interthread Communication MCQ

Java Multithreading MCQ Part-8 | Java Interthread Communication MCQ | Also See:- Inter-Thread Communication, Deadlock, Daemon Thread Interview Questions

Q1) The wait(), notify(), and notifyAll() methods are present in which class or interface?

a) Object class
b) Thread class
c) Runnable class
d) None of these

View Answer Answer:- a) Object class

Thread can call wait(), notify(), and notifyAll() methods on any Java object. To call a method on any object the method must be present in that or parent class, since java.lang.Object is the superclass of all Java classes therefore they are defined in the java.lang.Object class.

Q2) The wait(), notify(), and notifyAll() methods can be called from?

a) All Synchronized block/method
b) Only Static synchronized block/method
c) Only Synchronized Block
d) Only Synchronized Method

View Answer Answer:- a) All Synchronized block/method

We can call these methods only from the synchronized area (All Synchronized block/method) else we will get an IllegalMonitorStateException.

Q3) When a thread calls the wait() method, which of the following options is valid?

a) Immediately the thread will enter into the waiting state without releasing any lock.
b) Thread will release the lock of that object but may not immediately.
c) Thread will release all acquired locks and immediately enter into the waiting state.
d) Thread will immediately release the lock of that particular object and enter into the waiting state.

View Answer Answer:- d) Thread will immediately release the lock of that particular object and enter into the waiting state.

If a thread calls wait() method on an object, immediately it releases the lock of that particular object and enters into the waiting state. Note that only the lock of that particular object is released, it won’t release the lock of other objects.

Q4) When a thread calls notify() method, which of the following options is valid?

a) Thread will immediately release the lock of that particular object.
b) Thread will release the lock of that object but may not immediately.
c) Thread will not release the lock, it will continue its execution. At the end of the thread completion, it will release the lock.
d) None of these.

View Answer Answer:- b) Thread will release the lock of that object but may not immediately.

Q5) Below “line-1” is valid or not?

Stack s1 = new Stack();
Stack s2 = new Stack();

synchronized(s2) {
   s1.wait(); // line-1
}

a) Valid
b) Invalid
c) Valid only in Windows OS
d) None of these

View Answer Answer:- b) Invalid

If the wait() method is called on the “s1” object then the thread must have a lock of the s1 object. Here, synchronized(s2) is used therefore the thread will have lock of “s2” object, but not of “s1” object. Hence on calling s1.wait() it will give an exception:- java.lang.IllegalMonitorStateException: current thread is not owner.

Q6) On calling notify() method how many threads will get the notification?

If 25 threads are in waiting for the state to get the notification, among them 15 threads are in waiting for state after obj1.wait() is called, remaining 10 threads are in waiting for state after obj2.wait() is called. When the obj1.notify() method is called from another thread then how many threads will get the notification.

a) 1
b) 15
c) 10
d) 25

View Answer Answer:- a) 1

The notify() method will give notification to only one thread, no matter how many threads are in waiting state to get the notification on that particular object. While thread will get notification, it will be decided by the JVM.

Q7) On calling the notifyAll() method how many threads will get the notification?

If 25 threads are in waiting for the state to get the notification, among them 15 threads are in waiting for state after obj1.wait() is called, remaining 10 threads are in waiting for state after obj2.wait() is called. When obj2.notifyAll() method is called from another thread then how many threads will get the notification.

a) 1
b) 15
c) 10
d) 25

View Answer Answer:- c) 10

The notifyAll() used to give the notification for all waiting threads on a particular object. Since obj2.notifyAll() method is called therefore all the threads waiting for “obj2” object updation will get notification.

Q8) The wait() method throws which of the following Exception?

a) IllegalMonitorStateException
b) InterruptedException
c) It doesn’t throw any exceptions.
d) None of these

View Answer Answer:- b) InterruptedException

Q9) After calling the wait() method thread went to the waiting state, and it will come out of this waiting state on which events?

a) If the waiting thread got a notification
b) If time expires.
c) If the waiting thread got interrupted.
d) All of these

View Answer Answer:- d) All of these

Q10) Find the output of the below program?

public class Test {
   public static void main(String[] args) {
     MyThread t = new MyThread();
     t.start();
     synchronized(t) {
        System.out.print("A"); 
        try {
           t.wait();
        } catch (InterruptedException e) { }
        System.out.print("B");
        System.out.print(t.total);
     }
   }
}

class MyThread extends Thread {
   int total = 0;
   public void run() {
      synchronized (this) {
         System.out.print("C");
         for (int i=0; i<=10; i++) {
            total = total + i;
         }
         System.out.print("D");
         this.notify();
      }
   }
}

a) ABCD55
b) ACDB55
c) ACB55D
d) ACBD55

View Answer Answer:- b) ACDB55

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or 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 *