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);
}
}
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
Join The Discussion
Comments ( 3 )
Related Questions on Flow Control
What is the purpose of the "if" statement in Java?
A. To execute a block of code conditionally
B. To create loops
C. To declare variables
D. To define methods
A. No output
B. Error
C. Hello
D. 5
In first iteration: i=1,j=1,condn:true
2nd iteration :i=2,j=0,cond: false
prints i=2
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
1st iteration: i=1 and j=1
2nd iteration:i=2 and j=0,
condition false,
Prints i=2.