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-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) synchronizedThe 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) StarvationIn 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) falseThe 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) BackgroundDaemon 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 threadQ6) 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) mainIn 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) mainThe “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) systemThe “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 do you find anything incorrect? Let us know in the comments. Thank you!