Java OCA OCP Practice Question 797

Question

What is the output of the following?

12:  int v = 8; 
13:  loop: while (v > 7) { 
14:     v++; 
15:     do { 
16:        v--; 
17:     } while (v > 5); 
18:     break loop; 
19:  } 
20:  System.out.println(v); 
  • A. 5
  • B. 7
  • C. 8
  • D. The code does not compile.
  • E. The code compiles but throws an exception at runtime.


A.

Note

On line 12, v is first set to 8.

On line 13, the boolean condition is true because 8 > 7.

On line 13, v is incremented to 9.

Then the inner loop runs, decrementing v until it is no longer greater than 5.

On line 18, loop execution is completed because v is equal to 5.

The break statement says to skip to after the labeled loop, which is line 20.

Then v is printed as 5, making Option A correct.




PreviousNext

Related