Home / Java Programming / Java.lang Class :: Discussion

Discussion :: Java.lang Class

  1. 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 */ 

  2. A.
    i = 6 and j = 5
    B.
    i = 5 and j = 5
    C.
    i = 6 and j = 6
    D.
    i = 5 and j = 6

    View Answer

    Workspace

    Answer : Option D

    Explanation :

    This question is not testing your knowledge of the continue statement. It is testing your knowledge of the order of evaluation of operands. Basically 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.


Be The First To Comment