Java OCA OCP Practice Question 241

Question

Given:

3. public class Main {
4.   public static void main(String[] args) {
5.     foreach:/*w w w  .j av  a2 s.co  m*/
6.     for(int j=0; j<5; j++) {
7.       for(int k=0; k< 3; k++) {
8.         System.out.print(" " + j);
9.         if(j==3 && k==1) break foreach;
10.         if(j==0 || j==2) break;
11.       }
12.     }
13.   }
14. }

What is the result?

  • A. 0 1 2 3
  • B. 1 1 1 3 3
  • C. 0 1 1 1 2 3 3
  • D. 1 1 1 3 3 4 4 4
  • E. 0 1 1 1 2 3 3 4 4 4
  • F. Compilation fails


C is correct.

Note

A break breaks out of the current innermost loop and carries on.

A labeled break breaks out of and terminates the labeled loops.




PreviousNext

Related