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;"
Solution
When the above Java program is compiled and run, the output will be 2.
Here's the step by step explanation:
-
The program starts with the declaration of two integer variables, i and j, with initial values 0 and 2 respectively.
-
Then, a do-while loop is started. This loop will continue as long as the condition (j > 0) is true.
-
Inside the loop, the value of i is incremented by 1 using the pre-increment operator (++i). This means that the value of i is increased before it is used in the expression. So, i becomes 1 in the first iteration.
-
Then, the value of j is decremented by 1 (j--). So, j becomes 1.
-
The loop condition is checked again. Since j is still greater than 0, the loop continues for one more iteration.
-
In the second iteration, i is incremented again to 2 and j is decremented to 0.
-
Now, the loop condition (j > 0) is false. So, the loop ends.
-
Finally, the value of i (which is now 2) is printed to the console.
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 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
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
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.