Examveda
Examveda

What all gets printed when the following program is compiled and run?
public class Test{
      public static void main(String args[]){
            int i=0, j=2;
            do{
                  i=++i;
                  j--;
            }while(j>0);
            System.out.println(i);
      }
}

A. 0

B. 1

C. 2

D. The program does not compile because of statement "i=++i;"

E. None of these

Answer: Option C


This Question Belongs to Java Program >> Flow Control

Join The Discussion

Comments ( 3 )

  1. Sai Simron
    Sai Simron :
    3 years ago

    In first iteration: i=1,j=1,condn:true
    2nd iteration :i=2,j=0,cond: false
    prints i=2

  2. Mounika Gangarapu
    Mounika Gangarapu :
    4 years ago

    when i = 0;
    a[i] = a[(a[i]+3)%a.length] //a.length =5;
    a[0] = a[(a[0]+3)%5];
    a[0] = a[(0+3)%5] ; // 3
    a[0] = a[3] = 1
    when i = 1;
    a[1]=a[(a[1]+3)%5];
    a[1]=a[(2+3)%5];
    a[1]=a[0];
    a[1]=1;
    Therefore a[1] is equal to 1

  3. Priy Shukla
    Priy Shukla :
    5 years ago

    1st iteration: i=1 and j=1
    2nd iteration:i=2 and j=0,
    condition false,
    Prints i=2.

Related Questions on Flow Control