What is the final value of j in the below code?
int main()
{
int i = 0, j = 0;
if (i && (j = i + 10))
//do something
;
}
Answer: D
Correct answer: 0
&& short-circuits: since i is 0 the whole condition is already false, so the right operand (j = i + 10) is never evaluated and the assignment never happens. j keeps its initial value, 0. This guarantee is what makes idioms such as if (p && p->next) safe.
Please log in or register to report this question.