Knowee
Questions
Features
Study Tools

What all gets printed when the following program is compiled and run?public class Test{ public static void main(String args[]){ int i=0, j=2; do{ i=++i; j--; }while(j>0); System.out.println(i); }}012The program does not compile because of statement "i=++i

Question

What all gets printed when the following program is compiled and run?public class Test{ public static void main(String args[]){ int i=0, j=2; do{ i=++i; j--; }while(j>0); System.out.println(i); }}012The program does not compile because of statement "i=++i

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

Solution

When the above Java program is compiled and run, the output will be 2.

Here's the step by step explanation:

  1. The program starts with the declaration of two integer variables i and j, with initial values 0 and 2 respectively.

  2. Then, a do-while loop is started. In this loop, the value of i is incremented by 1 (i=++i) and the value of j is decremented by 1 (j--).

  3. The loop continues as long as the value of j is greater than 0. Since the initial value of j is 2, the loop will run twice.

  4. In the first iteration, i becomes 1 and j becomes 1. In the second iteration, i becomes 2 and j becomes 0.

  5. After the loop ends, the current value of i (which is 2) is printed to the console.

So, the output of the program is 2.

This problem has been solved

Similar Questions

What all gets printed when the following program is compiled and run.public class Test{ public static void main(String args[]){ int i, j=1; i = (j>1)?2:1; switch(i){ case 0: System.out.println(0); break; case 1: System.out.println(1); case 2: System.out.println(2); break; case 3: System.out.println(3); break; } }}01212

Consider a Program:public class Test {            public static void main(String[] args) {             int[] numbers = {1, 2, 3, 4, 5};             for (int i = 0; i <= numbers.length; i++) {             System.out.println(numbers[i]);             }            }}Which of the following is an output of the above program ? a. It will print numbers from 1 to 5 b. It will print numbers from 1 to 5 and then throw an exception c. It will compile but not run d. It will not compile

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?public class Test{ public static void main(String args[]){ int i = 1; do{ i--; }while(i > 2); System.out.println(i); }}12-10

Consider following code segment1. import java.util.*;2. public class Program{3. public static void main(String[] args) {4. int []a = {3,2,1,0};5. int y =  a.length;6. while(y>=0){7.   System.out.print(a[--y]);8. } 9. }10. }  What would be the output, if it is executed as a program?*Never ending loop after printing 32100123 followed by an exception.01233210Compilation fails

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.