Adobe Interview Question Testing / Quality Assurances

• 0

Asked these three questions which are similar but not same.

Q1: Output ?
int main() {
int i=-3, j=2, k=0, m;
m = ++i && ++j || ++k;
printf ("%d %d %d %d", i,j,k,m);
return 0;
}

Q2: Output ?
int main() {
int i=-3, j=2, k=0, m;
m = ++i || ++j && ++k;
printf ("%d %d %d %d", i,j,k,m);
return 0;
}

Q3: Output ?
int main() {
int i=-3, j=2, k=0, m;
m = ++i && ++j && ++k;
printf ("%d %d %d %d", i,j,k,m);
return 0;
}

Country: India
Interview Type: Written Test

Comment hidden because of low score. Click to expand.
2
of 2 vote

Ans:
Q1: -2 3 01

Q2: -2 2 01

Q3: -2 3 1 1

Comment hidden because of low score. Click to expand.
0

Can you please how this operation works?

Comment hidden because of low score. Click to expand.
0

Can you please explain how this operation works?

Comment hidden because of low score. Click to expand.
0

Can you please explain how this operation/statement works?

Comment hidden because of low score. Click to expand.
0

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"

Comment hidden because of low score. Click to expand.
0

wow damn smart

Comment hidden because of low score. Click to expand.
0

The explained way is not clear...

Comment hidden because of low score. Click to expand.
0
of 0 vote

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?

Comment hidden because of low score. Click to expand.
0

It is due to the precedence rules, since && and || have same precedence, the compiler is seeing it as (++i && ++j) || ++k. This of course puts into question all the results posted above.

Comment hidden because of low score. Click to expand.
0
of 0 vote

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?

Comment hidden because of low score. Click to expand.
0

It goes like
++i = 0 = FALSE
so FALSE && ++j || ++k
this will not evaluate ++j as whatever be the value of j this will be false
and will directly go and evaluate k

Comment hidden because of low score. Click to expand.
0
of 0 vote

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.

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book walking you through every aspect of getting a job at a top tech company, while focuses on software engineering interviews.

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.