ProgrammingVery hardProduct company OA

What is printed?

What is printed?
int a = 5, b = 10;
int r = a & b == 10;
printf("%d", r);
  1. A. 0, since (a & b) == 10 is computed first
  2. B. 15, treating & like addition
  3. C. Compilation error: ambiguous precedence
  4. D. 1, since == binds tighter than bitwise &, giving a & (b == 10)

Correct answer: D. 1, since == binds tighter than bitwise &, giving a & (b == 10)

A classic precedence trap: relational/equality operators bind TIGHTER than bitwise &. So a & b == 10 parses as a & (b == 10) = 5 & (10==10) = 5 & 1. In binary, 5 is 0101 and 1 is 0001; ANDing gives 0001 = 1. The naive but wrong reading (a & b) == 10 would instead give (5&10)==10 = 0==10 = 0.

More C Programming questions

Exams that ask this topic