Java OCA OCP Practice Question 1415

Question

What will the following code print when compiled and run?

public class Main{ 
    public static void main (String args []){ 
        int c = 0; 
        A : for (int i = 0; i < 2; i++){ 
            B : for (int j = 0; j < 2; j++){ 
                C: for (int k = 0; k < 3; k++){ 
                    c++; /*from   w  ww. ja va  2s . c  o m*/
                    if (k>j) break; 
                 } 
             } 
         } 
        System.out.println (c); 
     } 
} 

Select 1 option

  • A. 7
  • B. 8
  • C. 9
  • D. 10
  • E. 11


Correct Option is  : D

Note

The point to note here is that a break without any label breaks the innermost outer loop.

So in this case, whenever k>j , the C loop breaks.

You should run the program and follow it step by step to understand how it progresses.




PreviousNext

Related