Java OCA OCP Practice Question 375

Question

What will be the output if you run the following program?

public class Main{ 
   public static void main (String args []){ 
      int i; /*from   w w w . j  a v a2  s. c  o m*/
      int j; 
      for(i = 0, j = 0 ; j < 1 ; ++j , i++){ 
         System.out.println ( i + " " + j ); 
      } 
      System.out.println ( i + " " + j ); 
    } 
} 

Select 1 option

  • A. 0 0 will be printed twice.
  • B. 1 1 will be printed once.
  • C. 0 1 will be printed followed by 1 2.
  • D. 0 0 will be printed followed by 1 1.
  • E. It will print 0 0 and then 0 1.


Correct Option is  : D

Note

j will be less than 1 for only the first iteration.

So, first it will print 0, 0.

Next, i and j are incremented.

Because j is not less than 1 at the start of the loop,

the condition fails and it comes out of the loop.

Finally, it will print 1,1.




PreviousNext

Related