Java Deadlock, Daemon Thread & Thread Group MCQ

Java Multithreading MCQ Part-9 | Java Deadlock, Daemon Thread & Thread Group MCQ | Also See:- Inter-Thread Communication, Deadlock, Daemon Thread Interview Questions

Deadlock

Q1) Deadlock situations occur because of which keyword?

a) this
b) synchronized
c) super
d) static

View Answer Answer:- b) synchronized

The deadlock situation occurs only because of synchronized keyword.

Q2) Is it an example of deadlock or starvation or both?

public class ThreadTest {
   private static int i = 0;
 
   public static void doSomething() {
      synchronized(ThreadTest.class) {
         while(true) {
            System.out.println("count:" + ++i)
          }
      }
   }

   public static void main(String args[]) { 
      New Thread(() -> doSomething()).start();
      New Thread(() -> doSomething()).start();
   }
}

a) Deadlock
b) Starvation
c) Both (a) and (b)
d) None of these

View Answer Answer:- b) Starvation

In the deadlock situation, two threads are waiting for each other, but in this case only the second thread is waiting for completion of the first thread. Since the first thread is executing an infinite loop therefore it will never complete but it is not waiting for any resources, it is performing its own job.

Daemon Thread

Q3) Find the output of the below program?

public class Test {
   public static void main(String[] args) {
      System.out.println(Thread.currentThread().isDaemon());
   }
}

a) true
b) false
c) Compile time error
d) Run time exception

View Answer Answer:- b) false

The default nature of the main thread is non-daemon.

Q4) Daemon thread runs in?

a) Background
b) Foreground
c) Both
d) None of these

View Answer Answer:- a) Background

Daemon threads always run in the background to provide support for non-daemon threads.

Q5) Garbage collector thread in Java is an example of?

a) non-daemon thread
b) Daemon thread
c) Green thread
d) None of these

View Answer Answer:- b) Daemon thread

Q6) Find the output of the below program?

public class Test {
   public static void main(String[] args) {
      MyThread t1 = new MyThread();
      t1.start();
   }
}
class MyThread extends Thread {
   public void run(){
      Thread.currentThread().setDaemon(true);
   }
}

a) Program will run successfully
b) Compile time error
c) Exception in thread “thread-0”
d) Exception in thread “main”

View Answer Answer:- c) Exception in thread “thread-0”

Daemon nature can’t be changed once thread is started, else we will get a runtime exception. Since the setDaemon() method is called from the “thread-0” therefore we will get a run-time exception for “thread-0”.

Q7) When will the daemon thread be completed?

a) once non-daemon threads execution is completed.
b) When wait() method is called.
c) When interrupt() method is called.
d) None of these

View Answer Answer:- a) once non-daemon threads execution is completed.

Since daemon threads are service threads, therefore its execution is terminated automatically when all non-daemon threads execution is completed.

Also see:- Daemon thread in Java with example of isDaemon() and setDaemon() Method

Thread Group

Q8) In Java, the main() method executed by which thread?

a) Thread-0
b) main
c) Garbage Collector
d) None of these

View Answer Answer:- b) main

In Java, the main() method is executed by “main” thread.

Q9) The “main” thread in Java belongs to which thread group?

a) system
b) main
c) Both (a) and (b)
d) None of these

View Answer Answer:- b) main

The “main” thread in Java belongs to the “main” thread group. Hence, the main() method is executed by the “main” thread which is part of the “main” thread group.

Q10) Which is the root for all thread groups in Java?

a) system
b) main
c) Both (a) and (b)
d) None of these

View Answer Answer:- a) system

The “system” group is the root for all thread groups in Java. Every thread group in Java is a child group (or sub-group) of a system group either directly or indirectly.

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 *