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
Question
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
Solution
The output of the program will be an exception at runtime. In Java, you cannot start a thread twice. When you call the start() method for the second time on the same thread, it will throw a IllegalThreadStateException. So, after printing "one. ", the program will throw an exception when it tries to start the thread again.
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 { 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
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
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
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
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.