What will be the output?public class Test{ public static void main(String args[]){ int i = 1; do{ i--; }while(i > 2); System.out.println(i); }}12-10
Question
What will be the output?public class Test{ public static void main(String args[]){ int i = 1; do{ i--; }while(i > 2); System.out.println(i); }}12-10
Solution 1
The output of the given Java code will be 0.
Here's the step by step explanation:
- The integer variable
iis initialized with a value of 1. - A do-while loop is started. In this loop, the value of
iis decremented by 1 (i--) in each iteration. - The condition for the loop to continue is
i > 2. However, sinceiis initialized as 1 and is decremented in the loop,iwill never be greater than 2. Therefore, the loop will only run once. - After the loop has run once,
iwill be 0 (because 1 - 1 = 0). - The value of
i(which is now 0) is then printed to the console.
So, the output of the program will be 0.
Solution 2
The output of the given Java code will be 0.
Here's the step by step explanation:
-
The integer variable
iis initialized with a value of 1. -
Then, a do-while loop is started. In this loop, the value of
iis decremented by 1 in each iteration. -
The condition for the do-while loop is
i > 2. This means the loop will continue as long asiis greater than 2. -
However, since
iis initialized as 1, and it is decremented in the first iteration of the loop, the conditioni > 2is never true. -
Therefore, the loop only runs once, and
ibecomes 0 after the first iteration. -
Finally, the value of
iis printed, which is 0.
Solution 3
The output of the given Java code will be 0.
Here's the step by step explanation:
- The variable
iis initialized with the value 1. - The
do-whileloop starts. In this loop, the code inside thedo {}block is executed at least once before the condition in thewhile()is checked. - Inside the loop, the value of
iis decremented by 1 (i--), soibecomes 0. - After the first execution of the loop, the condition
i > 2is checked. Sinceiis 0 and 0 is not greater than 2, the loop ends. - The value of
i(which is 0) is printed out.
Similar Questions
Determine output:public class Test{ public static void main(String args[]){ int i, j; for(i=1, j=0;i<10;i++) j += i; System.out.println(i); }}1011920
Determine output:public class Test{ public static void main(String args[]){ int i; for(i = 1; i < 6; i++){ if(i > 3) continue ; } System.out.println(i); }}23466
e correct answerWhat is the output of the following Java program?class Main { public static void main(String args[]) { final int i; i = 20; i = 30; System.out.println(i); }}
) What will be the output of the following program?public class Test { public static void main(String[] args) { int count = 1; while (count <= 15) { System.out.println(count % 2 == 1 ? "***" : "+++++"); ++count; } // end while } // end main }
What will be the output of the program?public class Sample{ public static void main(String [] args) { int i = 10; do while ( i < 10 ) System.out.print("The value of i is " + i); while ( i > 10 ) ; }}Select one:a.No output is produced.b.The value of i is 10 The value of i is 10c.Compilation errord.The value of i is 10
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.