Give the output for the following:-
int a[4]={2,4,6,8};
for(i=0;i<=1;i++)
{
s=a[i]+a[3-i];
System.out.println(s);
}
Answers
Answer:
Explanation:
but
When i ran this program on machine i shocked to get the answer 12,32,50.
i am writing here what concept i used.
If we do some operation in printf statement on same element then we have to use stack.
so in first printf statement:-
structure of stack is
++i (top of stack) => 6
i++ => 6
and after the execution of this value of i is 7 and i++ + ++i is 6+6 = 12
in second printf statement
structure of stack is
i++ <- (top of stack) = > 7
i++ =>8
++i => 10
i++ (lower index of stack) => 10
and after the execution of this value of i is 11 and i++ + ++i + i++ + i++is 10 + 10 + 8 + 7 = 35
in third printf statement
structure of stack is
i++ <- (top of stack) = > 11
++i =>13
i++ => 13
++i (lower index of stack) => 15
and after the execution of this value of i is 15 and ++i + i++ + ++i + i++ is 15 + 13 + 13 + 11 = 52
but When i ran this program on machine i shocked to get the answer 12,32,50.
i dont know what concept they used ..most probably side effect..
let me know if some have answer of this question with proper explanation.
thanks in advance ..!!!