Java OCA OCP Practice Question 3028

Question

Given this code inside a method:

public class Main {
   public static void main(String[] args) {
      int count = 0;
      outer: for (int x = 0; x < 5; x++) {
         middle: for (int y = 0; y < 5; y++) {
            if (y == 1)
               continue middle;
            if (y == 3)
               break middle;
            count++;//from   w w  w .  j a va 2 s  . c om
         }
         if (x > 2)
            continue outer;
         count = count + 10;
      }
      System.out.println("count: " + count);
   }
}

What is the result?

  • A. count: 33
  • B. count: 40
  • C. count: 45
  • D. count: 65
  • E. Compilation fails.
  • F. The code runs in an endless loop.


B is correct.

Note

The label, break, and continue statements are all legal.

A continue statement ends the current iteration of a loop, a break statement ends all iterations of a loop.




PreviousNext

Related