Define a Thread in Java MCQ-2

Java Multithreading MCQ Part-2 | Define a Thread in Java MCQ-2 | Also see:- Define Thread in Java Multithreading Interview Questions

Q1) What is the possible output of the below program?

class MyThread extends Thread {
   public void run() {
      System.out.print("run() Method ");
   }
}

public class ThreadDemo {
   public static void main(String[] args) {
      MyThread t = new MyThread();
      t.start(); 
      System.out.print("main() Method ");
      t.start(); 
   }
}

a) run() Method main() method run() method
b) run() Method run method() main() method
c) Compile time error
d) Run-time Exception

View Answer Answer:- d) Run-time Exception

After starting a thread if we are trying to restart the same thread then we will get a runtime exception saying “java.lang.IllegalThreadStateException.”

Q2) Find the output of the below program?

class MyRunnable implements Runnable {
   public void run() {
      System.out.println("Child thread");
   }
}

public class Test {
   public static void main(String[] args) {
      MyRunnable r = new MyRunnable();
      Thread t1 = new Thread();
      Thread t2 = new Thread(r);
      t1.start();
   }
}

a) Child Thread
b) No Output
c) Compile time error
d) Exception

View Answer Answer:- b) No Output

The start() method is called on the t1 thread object (t1.start()) therefore the start() method of Thread will be executed which calls run() method of Thread class having empty implementation. Hence we won’t get any output. Note that we call the t1.start() method, there is no relation between Thread class and MyRunnable class. The run() method of MyRunnable class will not be executed.

Q3) A new thread will be created or not, what will be the output of the below program?

class MyRunnable implements Runnable {
   public void run() {
      System.out.println("Child thread");
   }
}

public class Test {
   public static void main(String[] args) {
      MyRunnable r = new MyRunnable();
      Thread t1 = new Thread();
      Thread t2 = new Thread(r);
      t1.run();
   }
}

a) No; Child thread
b) Yes; Child thread
c) No; No Output
d) Yes; No Output

View Answer Answer:- c) No; No Output

Here a new thread will not be created. Thread class run() method will be executed as a normal method call which has an empty implementation therefore we won’t get any output from this method. The run() method of MyRunnable class will not be executed. Complete program will be executed by the main thread itself.

Q4) Find the output of the below program?

class MyRunnable implements Runnable {
   public void run() {
      System.out.println("Child thread");
   }
}

public class Test {
   public static void main(String[] args) {
      MyRunnable r = new MyRunnable();
      Thread t1 = new Thread();
      Thread t2 = new Thread(r);
      t2.start();
   }
}

a) Child Thread
b) No Output
c) Compile time error
d) Exception

View Answer Answer:- a) Child Thread

A new thread will be created and the MyRunnable class run() method will be executed.

Q5) A new thread will be created or not, what will be the output of the below program?

class MyRunnable implements Runnable {
   public void run() {
      System.out.println("Child thread");
   }
}

public class Test {
   public static void main(String[] args) {
      MyRunnable r = new MyRunnable();
      Thread t1 = new Thread();
      Thread t2 = new Thread(r);
      t2.run();
   }
}

a) No; Child thread
b) Yes; Child thread
c) No; No Output
d) Yes; No Output

View Answer Answer:- a) No; Child thread

The run() method will be executed as a normal method call. New thread will not be created.

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

class MyRunnable implements Runnable {
   public void run() {
      System.out.println("Child thread");
   }
}

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

a) Child thread
b) No Output
c) Compile time error
d) Run-time Exception

View Answer Answer:- c) Compile time error

Here ‘r’ is of MyRunnable type which doesn’t contain the start() method therefore we will get a compile time error:- “can’t find symbol method start() location class MyRunnable.”

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

class MyRunnable implements Runnable { }

public class Test { 
   public static void main(String[] args) { 
      MyRunnable r = new MyRunnable();
      r.run();
   }
}

a) Child thread
b) No Output
c) Compile time error
d) Run-time Exception

View Answer Answer:- c) Compile time error

The run() method in Runnable interface is defined as an abstract method, therefore the child class must implement it else we will get a compile time error.

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

class MyRunnable implements Runnable {
   public void run() {
      System.out.println("Child thread");
   }
}

public class Test {
   public static void main(String[] args) {
      MyRunnable r = new MyRunnable();
      r.run();
   }
}

a) Child thread
b) No Output
c) Compile time error
d) Run-time Exception

View Answer Answer:- a) Child thread

The run() method in MyRunnable interface, is an abstract method. Since it is overridden in MyRunnable class therefore the run() method of MyRunnable interface will be executed.

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

class MyThread extends Thread {
   public void run() {
      System.out.print("run");
   }
}

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

a) run
b) start
c) No output
d) Compile time error

View Answer Answer:- c) No output

The start() method is called on the “Thread” class object, not on the “MyThread” class object. The start() method of Thread class calls run() method which has an empty implementation therefore no output.

Q10) Runnable interface contains which method?

a) run()
b) start()
c) both run() and start()
d) None of these

View Answer Answer:- a) run()

The Runnable interface contains only run() method definition but not implementation.

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 *