Java OCA OCP Practice Question 742

Question

What will the following program print?

class Test{ //from  w  ww.  j a  v a 2s .c o m
   public static void main (String args []){ 
      int var = 20, i=0; 
      do{ 
         while (true){ 
         if ( i++ > var) break; 
          } 
       }while (i<var--); 
      System.out.println (var); 
    } 
} 

Select 1 option

  • A. 19
  • B. 20
  • C. 2 1
  • D. 22
  • E. It will enter an infinite loop.


Correct Option is  : A

Note

When the first iteration of outer do-while loop starts, var is 20.

The inner loop executes till i becomes 2 1.

The condition for outer do-while is checked, while( 22 < 20 ), [i is 22 because of the last i++>var check], thereby making var 19.

As the condition is false, the outer loop also ends.

19 is printed.




PreviousNext

Related