Java OCA OCP Practice Question 1560

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 Label1; 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  : E

Note

This is a forward loop that contains a labelled break statement.

A labelled break breaks out of the loop that is marked with the given label.

Therefore, a labelled break is used to break out from deeply nested loops to the outer loops.




PreviousNext

Related