Java Thread State MCQ

Java Multithreading MCQ Part-4 | Java Thread State MCQ | Also see:- Define Thread in Java Multithreading Interview Questions

Q1) Exception will be raised in which thread?

class MyThread extends Thread {
   public void run() {
      Thread.currentThread().setName("KnowProgram");
      System.out.println(10/0);
   }
}

public class Test {
   public static void main(String[] args) {
      MyThread mt = new MyThread();
      mt.start();
   }
}

a) Exception in thread “KnowProgram”
b) Exception in thread “Thread-0”
c) Exception in thread “main”
d) None of these

View Answer Answer:- a) Exception in thread “KnowProgram”

Q2) Find the output of the below program?

class MyThread extends Thread {}
public class Test {
   public static void main(String[] args) {
      MyThread mt = new MyThread();
      mt.setName("KP");
      mt.start();
      System.out.print(mt.getName()+" ");
      MyThread mt1 = new MyThread();
      mt1.setName("KP");
      mt1.start();
      System.out.println(mt.getName());
   }
}

a) KP KP
b) KP thread-0
c) KP thread-1
d) Compile time error or Run-time Exception

View Answer Answer:- a) KP KP

We can create multiple threads with the same name.

Q3) Which of the following are thread states?

a) NEW
b) RUNNABLE
c) BLOCKED
d) All of these

View Answer Answer:- d) All of these

Q4) What will be the output of the below program?

class MyThread extends Thread { }
public class Test {
   public static void main(String[] args) {
      Thread t = new Thread();
      System.out.println(t.getState());
      t.start();
   }
}

a) NEW
b) RUNNABLE
c) BLOCKED
d) None of these

View Answer Answer:- a) NEW

Q5) What will be the output of the below program?

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

a) NEW
b) RUNNABLE
c) BLOCKED
d) None of these

View Answer Answer:- b) RUNNABLE

Q6) What will be the output of the below program?

class MyThread extends Thread {
   public void run() {
      int count = 0;
      for (int i=0; i<10; i++) {
         count += i;
      }
   }
}

public class Test {
   public static void main(String[] args) throws Exception {
      Thread t = new Thread();
      t.start();
      Thread.sleep(1000);
      System.out.println(t.getState());
   }
}

a) NEW
b) RUNNABLE
c) BLOCKED
d) TERMINATED

View Answer Answer:- d) TERMINATED

Q7) What will be the output of the below program?

class MyThread extends Thread { }
public class Test {
   public static void main(String[] args) {
      Thread t = new Thread();
      System.out.println(t.isAlive());
      t.start();
   }
}

a) true
b) false
c) Compile time error
d) None of these

View Answer Answer:- b) false

Q8) What will be the output of the below program?

class MyThread extends Thread { }
public class Test {
   public static void main(String[] args) {
      Thread t = new Thread();
      t.start();
      System.out.println(t.isAlive());
   }
}

a) true
b) false
c) Compile time error
d) None of these

View Answer Answer:- a) true

Q9) What will be the output of the below program?

class MyThread extends Thread {
   public void run() {
      int count = 0;
      for (int i=0; i<10; i++) {
         count += i;
      }
   }
}

public class Test {
   public static void main(String[] args) throws Exception {
      Thread t = new Thread();
      t.start();
      Thread.sleep(1000);
      System.out.println(t.isAlive());
   }
}

a) true
b) false
c) Compile time error
d) None of these

View Answer Answer:- b) false

Q10) If the thread is in a RUNNABLE state then isAlive() method return?

a) true
b) false
c) Compile time error
d) None of these

View Answer Answer:- a) true

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 *