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
Question
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
Solution
The correct answer is "CT CT".
Here's the step-by-step explanation:
-
The Test class extends the Thread class and implements the Runnable interface. Both Thread class and Runnable interface have a method called run(). In this case, the run() method is overridden to print "CT ".
-
In the main method, an object of the Test class is created.
-
The run() method is called on the object, which prints "CT ".
-
The start() method is called on the object. The start() method in turn calls the run() method, which prints "CT " again.
So, the output of the program is "CT CT".
Similar Questions
Select the correct answerWhat will be the output of the following Java program?class newthread implements Runnable { Thread thr1; newthread() { thr1 = new Thread(this,"My Thread"); thr1.start(); } } class Multithreaded_programing { public static void main(String args[]) { new newthread(); } }OptionsThread[My Thread,5,main]My ThreadCompilation ErrorRuntime 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 will be the output of the following Java code? class newthread extends Thread { newthread() { super("My Thread"); start(); } public void run() { System.out.println(this); } } class Code_Tantra { public static void main(String args[]) { new newthread(); } }OptionsCompilation ErrorThread[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
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
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.