Multiple choice

What will be the output of the program?

int i = 1, j = 10; do { if(i++ > --j) /* Line 4 / { continue; } } while (i < 5); System.out.println(i = + i + and j = + j); / Line 9 */

  1. i = 6 and j = 5

  2. i = 5 and j = 5

  3. i = 6 and j = 6

  4. i = 5 and j = 6

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

The prefix and postfix unary operators have a higher order of evaluation than the relational operators. So on line 4 the variable i is incremented and the variable j is decremented before the greater than comparison is made. As the loop executes the comparison on line 4 will be: if(i > j) if(2 > 9) if(3 > 8) if(4 > 7) if(5 > 6) at this point i is not less than 5, therefore the loop terminates and line 9 outputs the values of i and j as 5 and 6 respectively. The continue statement never gets to execute because i never reaches a value that is greater than j.