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
Question
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
Solution
The correct answer is "1 3".
Here's the step-by-step explanation:
-
The
run()method of theNptelclass is called in themainmethod. Thisrun()method contains a for loop that iterates from 1 to 4 (i.e., less than 5). -
Inside the for loop, the
System.out.print(i++ + " ")statement is executed. This statement prints the current value ofiand then incrementsiby 1 due to the post-increment operator (i++). -
So, in the first iteration,
iis 1. It prints 1 and then incrementsito 2. -
In the second iteration,
iis now 2. It prints 2 and then incrementsito 3. But due to thei++in the print statement,iis incremented again to 4. -
In the third iteration,
iis now 4. It prints 4 and then incrementsito 5. But since the loop condition isi < 5, the loop ends here. -
Therefore, the output of the program is "1 3".
Similar Questions
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 answerFor the program given below, what will be the output after its execution?public class Nptel extends Thread { public static void main(String[] args) { Thread thread = Thread.currentThread(); System.out.println(thread.activeCount()); }}Optionstrue01false
Select the correct answerWhat is the output of the following program?public class Nptel{ public static void main(String[] args) { try { int a = 5 / 0; } catch (Exception e) { catch (ArithmeticException a) { } } System.out.println("Programming In Java"); }}Options“Programming In Java”Compile time errorArithmeticExceptionRun time error
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 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
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.