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
Question
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
Solution
The correct answer is "Thread[My Thread,5,main]".
Here's the step by step explanation:
-
A new class
newthreadis created which extends theThreadclass. This means thatnewthreadis a subclass ofThreadand inherits all its methods and properties. -
In the constructor of
newthread, thesuperkeyword is used to call the constructor of the parentThreadclass. The string "My Thread" is passed as an argument, which sets the name of the thread. -
The
start()method is called, which causes this thread to begin execution. The Java Virtual Machine calls therunmethod of this thread. -
The
run()method is overridden in thenewthreadclass. This method is called when the thread is started. In this method,System.out.println(this);is used to print the string representation of the current thread, which includes its name, priority, and the group it belongs to. -
In the
mainmethod of theCode_Tantraclass, a new instance ofnewthreadis created. This starts the execution of the new thread. -
The output of the program is the string representation of the
newthreadinstance, which is "Thread[My Thread,5,main]". The "5" is the default priority of the thread and "main" is the name of the group it belongs to.
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
elect the correct answerWhat will be the output of the following Java code?class Main{ public static void main(String args[]) { Thread t = Thread.currentThread(); t.setName("New Thread"); System.out.println(t); }}OptionsThread[main,5,main]Thread[New Thread,5]Thread[5,main]Thread[New Thread,5,main]
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
Select the correct answerWhat will be the output of the following Java code?class Code{ Code()throws IOException { } }class Tantra extends Code{ Tantra() { } public static void main(String[]args) { }}Optionscompile and runs finerun time errorcompile time errorunreported exception java.io.IOException in default constructor
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.