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
Solution
The correct answer is D) Exception.
Here's the step-by-step explanation:
-
The class
MyRunnableimplements theRunnableinterface and overrides therun()method. This method prints "Child thread" when executed. -
In the
mainmethod of theTestclass, an instance ofMyRunnableis created. -
Two
Threadinstancest1andt2are created.t1is created with the default constructor, whilet2is created withr(theMyRunnableinstance) as an argument. -
The
start()method is called ont1. However,t1was created with the default constructor and doesn't have aRunnableobject associated with it. Therefore, itsrun()method does nothing. -
Since the
start()method is not called ont2, therun()method ofMyRunnableis not executed, so "Child thread" is not printed. -
The
start()method oft1will throw ajava.lang.NullPointerExceptionbecause there is noRunnableobject associated witht1. This is why the correct answer is D) Exception.
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]
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.