Java OCA OCP Practice Question 1590

Question

What will the following code print?

void m (){
   int c = 0;
   Label1 : while  (c < 8){
       Label2: System.out.println (c);
       if  (c > 3) break Label2; else c++;
    }
}

Select 1 option

  • A. It will not compile.
  • B. It will throw an exception at runtime.
  • C. It will print numbers from 0 to 8
  • D. It will print numbers from 0 to 3
  • E. It will print numbers from 0 to 4


Correct Option is  : A

Note

Because break Label2; would be valid only when it is within the block of code under the scope of the label Label2.

In this case, the scope of Label2 extends only up till System.out.println (c); and break Label2; is out of the scope of the label.




PreviousNext

Related