Knowee
Questions
Features
Study Tools

What will be the output of the program?class MyThread extends Thread { MyThread() {} MyThread(Runnable r) {super(r); } public void run() { System.out.print("Inside Thread "); } } class MyRunnable implements Runnable { public void run() { System.out.print(" Inside Runnable"); } } class Test { public static void main(String[] args) { new MyThread().start(); new MyThread(new MyRunnable()).start(); } }Prints "Inside Thread Inside Thread"Prints "Inside Thread Inside Runnable"Does not compileThrows exception at runtime

Question

What will be the output of the program?class MyThread extends Thread { MyThread() {} MyThread(Runnable r) {super(r); } public void run() { System.out.print("Inside Thread "); } } class MyRunnable implements Runnable { public void run() { System.out.print(" Inside Runnable"); } } class Test { public static void main(String[] args) { new MyThread().start(); new MyThread(new MyRunnable()).start(); } }Prints "Inside Thread Inside Thread"Prints "Inside Thread Inside Runnable"Does not compileThrows exception at runtime

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

Solution

The output of the program will be "Inside Thread Inside Runnable".

Here's the step by step explanation:

  1. The main method in the Test class starts two threads using the start() method.

  2. The first thread is started by creating a new instance of MyThread and calling start(). This calls the run() method in the MyThread class, which prints "Inside Thread ".

  3. The second thread is started by creating a new instance of MyThread, but this time a MyRunnable object is passed to the constructor. This makes the thread call the run() method of the MyRunnable object instead of the run() method in MyThread. The run() method in MyRunnable prints " Inside Runnable".

So, the output of the program is "Inside Thread Inside Runnable".

This problem has been solved

Similar Questions

What will be the output of the program?class MyThread extends Thread { public static void main(String [] args) { MyThread t = new MyThread(); /* Line 5 */ t.run(); /* Line 6 */ } public void run() { for(int i=1; i < 3; ++i) { System.out.print(i + ".."); } }}This code will not compile due to line 5.This code will not compile due to line 6.1..2..1..2..3..[#]

What will be the output of the program?class MyThread extends Thread { public static void main(String [] args) { MyThread t = new MyThread(); t.start(); System.out.print("one. "); t.start(); System.out.print("two. "); } public void run() { System.out.print("Thread "); }}Compilation failsAn exception occurs at runtime.It prints "Thread one. Thread two."The output cannot be determined

Select the correct answerWhat is the output of the following program?class Nptel extends Thread { public void run() { System.out.println("Running"); }}public class ThreadTest { public static void main(String args[]) throws Inter ruptedException { Runnable r = new Nptel(); Thread myThread = new Thread(r); myThread.start(); }}Options“Running”Compiler ErrorRuntime ExceptionNo output, but no error

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

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.