Adobe Interview Question
Testing / Quality AssurancesCountry: India
Interview Type: Written Test
Take Q2 as example:
int i=-3, j=2, k=0, m;
Step 1:
variable i, j, k, m are declared as an integer type and variable
i, j, k are initialized to -3, 2, 0 respectively.
Step 2:
m = ++i || ++j && ++k;
here (++j && ++k;) this code will not get executed because ++i has non-zero value.becomes
m = -2 || ++j && ++k; becomes m = TRUE || ++j && ++k;
The part (++j && ++k) will not be executed. Hence this statement becomes TRUE. So it returns '1'(one). Hencem=1.
Step 3:
printf("%d, %d, %d, %d\n", i, j, k, m);
In the previous step the value of variable 'i' only incremented by '1'(one). The variable j,k are not incremented.Hence the output is "-2, 2, 0, 1"
suppose i write the code
int main()
{
int i=-1, j=2, k=0, m;
m = ++i && ++j || ++k;
printf ("%d %d %d %d", i,j,k,m);
return 0;
}
the output shows:
0 2 1 1
shouldnt it show
0 2 0 0
how does the operation takes place in this?
suppose i write the code
int main()
{
int i=-1, j=2, k=0, m;
m = ++i && ++j || ++k;
printf ("%d %d %d %d", i,j,k,m);
return 0;
}
the output shows:
0 2 1 1
shouldnt it show
0 2 0 0
how does the operation takes place in this?
In C & in C++, the expression will only evaluated till we can't come across the answer from left to right.
For eg, in expr. c = a || b if a is true, then c will be true irrespective of b, so b will not be evaluated. but in expr. c = a && b, both needs to be evaluated.
All values (+ve or -ve) are treated as true expect ZERO.
and i think, u know about post/pre operator.
Ans:
- Hmmm January 29, 2012Q1: -2 3 01
Q2: -2 2 01
Q3: -2 3 1 1