Java OCA OCP Practice Question 833

Question

What will be the result of attempting to compile and run the following program?

public class Main{ 
   public static void main (String args []){ 
      int i = 0; 
      loop  :         // 1 
       { // w  w w. j a va  2 s. c om
         System .out.println ("Loop Lable line"); 
         try{ 
            for  (  ;  true ;  i++ ){ 
               if ( i >5) break loop;       // 2 
             } 
          } 
         catch (Exception e){ 
            System .out.println ("Exception in loop ."); 
          } 
         finally{ 
            System .out.println ("In Finally");      // 3 
          } 
       } 
    } 
} 

Select 1 option

  • A. Compilation error at line 1 as this is an invalid syntax for defining a label.
  • B. Compilation error at line 2 as 'loop' is not visible here.
  • C. No compilation error and line 3 will be executed.
  • D. No compilation error and line 3 will NOT be executed.
  • E. Only the line with the label Loop will be printed.


Correct Option is  : C

Note

A break without a label breaks the current loop and a break with a label tries to pass the control to the given label.

If the break is in a try block and the try block has a finally clause associated with it then it will be executed.




PreviousNext

Related