Knowee
Questions
Features
Study Tools

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 ThreadB) No OutputC) Compile time errorD) Exception

Question

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 ThreadB) No OutputC) Compile time errorD) Exception

🧐 Not the exact question you are looking for?Go ask a question

Solution

The correct answer is D) Exception.

Here's the step-by-step explanation:

  1. The class MyRunnable implements the Runnable interface and overrides the run() method. This method prints "Child thread" when executed.

  2. In the main method of the Test class, an instance of MyRunnable is created.

  3. Two Thread instances t1 and t2 are created. t1 is created with the default constructor, while t2 is created with r (the MyRunnable instance) as an argument.

  4. The start() method is called on t1. However, t1 was created with the default constructor and doesn't have a Runnable object associated with it. Therefore, its run() method does nothing.

  5. Since the start() method is not called on t2, the run() method of MyRunnable is not executed, so "Child thread" is not printed.

  6. The start() method of t1 will throw a java.lang.NullPointerException because there is no Runnable object associated with t1. This is why the correct answer is D) Exception.

This problem has been solved

Similar Questions

Find the output of the below code:public class CN{public static void main(String args[]) { Thread startThread1 = new Thread(new Output("start1")); Thread runThread = new Thread(new Output("run")); Thread startThread2 = new Thread(new Output("start2")); startThread2.start(); startThread1.start(); runThread.run(); }private static class Output implements Runnable{ private String str; public Output(String str){ this.str = str; } @Override public void run() { System.out.println(str + " is executed by Thread : " + Thread.currentThread().getName()); }}}

Select the correct answerWhat will be the output of the following Java code?class newthread extends Thread { Thread T; newthread() { T = new Thread(this,"T1"); T.start(); } public void run() { try { T.join() System.out.println(T.getName()); } catch(Exception e) { System.out.print("Exception"); } } } class multithreaded_programing { public static void main(String args[]) { new newthread(); } }OptionsExceptionThread[My Thread,5,main] My ThreadRuntime Error

Select the correct answerWhat is the output in the following Java program?class Main{  public static void main(String args[])  {    Thread t = Thread.currentThread();    System.out.println(t.isAlive());       }}Options0truefalse1

Select the correct answerWhat is the output of the following program?public class Test extends Thread implements Runnable{  public void run()  {    System.out.printf("CT ");  }  public static void main(String[] args) throws InterruptedException  {    Test obj = new Test();    obj.run();    obj.start();  }}OptionsRuntime errorCompilation errorCT CTNone of the above

What will be the output of the following Java code?class multithreaded_programing { public static void main(String args[]) {Thread t = Thread.currentThread();System.out.println(t); } } Thread[5,main]Thread[main,5]Thread[main,0]Thread[main,5,main]

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.