Java OCA OCP Practice Question 2909

Question

Given:

public class Main {
   public static void main(String[] args) {
      int x = 4;/*from   ww w  .  j  av  a  2  s. c om*/
      int y = 4;
      while ((x = jump(x)) < 8)
         do {
            System.out.print(x + " ");
         } while ((y = jump(y)) < 6);
   }

   static int jump(int x) {
      return ++x;
   }
}

What is the result?

  • A. 5 5 6 6
  • B. 5 5 6 7
  • C. 5 5 6 6 7
  • D. 5 6 5 6 7
  • E. Compilation fails due to a single error.
  • F. Compilation fails due to multiple errors.


B is correct.

Note

It's legal for while expressions to include method calls.

The do loop is nested inside the while loop.

In the first iteration of the while loop, the do loop is executed twice, and "y" is left with the value of 6.

In subsequent while iterations, the do loop executes only once because "y" just keeps getting bigger and bigger.




PreviousNext

Related