Java OCA OCP Practice Question 693

Question

Consider the following method which is called with an argument of 7:

public void method1 (int i){ 
   int j =  (i*30 - 2)/100; 
    /*from ww  w. j  av a2  s  .  c o  m*/
   MyFlag  : for (;j<10; j++){ 
       boolean flag  = false; 
       while (!flag){ 
        if (Math .random ()>0.5) break MyFlag; 
        } 
    } 
    while (j>0){ 
     System.out.println (j--); 
     if (j == 4) break MyFlag; 
    } 
} 

What will it print?

Assume that Math.random() return a double between 0.0 and 1.0, not including 1.0.

Select 1 option

  • A. It will print 1 and 2
  • B. It will print 1 to N where N is a random number.
  • C. It will not compile.
  • D. It will throw an exception at runtime.


Correct Option is  : C

Note

Remember that a labeled break or continue statement must always exist inside the loop where the label is declared.

if (j == 4) break MyFlag; is a labelled break that is occurring in the second loop while the label MyFlag is declared for the first loop.




PreviousNext

Related