Knowee
Questions
Features
Study Tools

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

Question

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

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

Solution

The correct answer is "Compiler Error".

Here's why:

The class Nptel extends Thread and overrides the run() method. However, in the main method, an instance of Nptel is created and assigned to a Runnable reference. This is not allowed because Nptel is not a Runnable. It's a Thread.

So, when you try to create a new Thread with r (which is a Nptel object), the compiler will throw an error because it expects a Runnable object, not a Thread object.

Therefore, the program will not compile, and the correct answer is "Compiler Error".

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(); 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?public class Nptel extends Thread { public void run() { for (int i = 1; i < 5; i++) { System.out.print(i++ + " "); } } public static void main(String args[]) { Nptel t1 = new Nptel(); t1.run(); }}Options1 2 3 41 3Runtime error1 2

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 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

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

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.