Knowee
Questions
Features
Study Tools

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

🧐 Not the exact question you are looking for?Go ask a question

Solution 1

The output of the given Java code will be 0.

Here's the step by step explanation:

  1. The integer variable i is initialized with a value of 1.
  2. A do-while loop is started. In this loop, the value of i is decremented by 1 (i--) in each iteration.
  3. The condition for the loop to continue is i > 2. However, since i is initialized as 1 and is decremented in the loop, i will never be greater than 2. Therefore, the loop will only run once.
  4. After the loop has run once, i will be 0 (because 1 - 1 = 0).
  5. The value of i (which is now 0) is then printed to the console.

So, the output of the program will be 0.

This problem has been solved

Solution 2

The output of the given Java code will be 0.

Here's the step by step explanation:

  1. The integer variable i is initialized with a value of 1.

  2. Then, a do-while loop is started. In this loop, the value of i is decremented by 1 in each iteration.

  3. The condition for the do-while loop is i > 2. This means the loop will continue as long as i is greater than 2.

  4. However, since i is initialized as 1, and it is decremented in the first iteration of the loop, the condition i > 2 is never true.

  5. Therefore, the loop only runs once, and i becomes 0 after the first iteration.

  6. Finally, the value of i is printed, which is 0.

This problem has been solved

Solution 3

The output of the given Java code will be 0.

Here's the step by step explanation:

  1. The variable i is initialized with the value 1.
  2. The do-while loop starts. In this loop, the code inside the do {} block is executed at least once before the condition in the while() is checked.
  3. Inside the loop, the value of i is decremented by 1 (i--), so i becomes 0.
  4. After the first execution of the loop, the condition i > 2 is checked. Since i is 0 and 0 is not greater than 2, the loop ends.
  5. The value of i (which is 0) is printed out.

This problem has been solved

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

1/3

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.