Java OCA OCP Practice Question 804

Question

In the following code what will be the output if 0 (integer value zero) is passed to m()?

public class Main{ 
   public void m (int x){ 
      loop : for  (int i = 1; i < 5; i++){ 
         for  (int j = 1; j < 5; j++){ 
            System .out.println (i); 
            if  (x == 0)  {  continue loop;   } 
            System .out.println (j); 
          } /*from  ww  w  .  j  a va  2s.c o  m*/
       } 
    } 
} 

Select 1 option

  • A. The program will not compile.
  • B. It will print 1 2 3 4
  • C. It will print 1 1 2 3 4
  • D. It will print 1 1 2 2 3 3 4 4
  • E. Produces no output


Correct Option is  : B

Note

When x is 0, the statement continue loop; is executed.

loop: is for the outer loop.

Only one iteration is performed for the inner loop.

The inner loop prints the value of i only once and then next iteration of outer loop starts.

'j ' is never printed. So, it prints 1 2 3 4.




PreviousNext

Related