Java OCA OCP Practice Question 651

Question

What will the following code print?

public class Main{ 
  public static void main (String [] args){ 
    int i = 0, j = 5; 
    lab1  : for ( ; ; i++){ 
      for ( ; ; --j)  if ( i >j ) break lab1; 
     } 
    System .out.println (" i = "+i+", j = "+j); 
   } 
} 

Select 1 option

  • A. i = 1, j = -1
  • B. i = 1, j = 4
  • C. i = 0, j = 4
  • D. i = 0, j = -1
  • E. It will not compile.


Correct Option is  : D

Note

The values of i and j in the inner most for loop change as follows:

i = 0 j = 5 
i = 0 j = 4 
i = 0 j = 3 
i = 0 j = 2 
i = 0 j = 1 
i = 0 j = 0 
i = 0 j = -1 

The final println prints i = 0, j = -1




PreviousNext

Related