ProgrammingHardTCS NQT · Product company OA

What is printed?

What is printed?
#define MAX(a, b) a > b ? a : b
int x = 3, y = 7;
printf("%d", 2 * MAX(x, y));
  1. A. 7
  2. B. 6
  3. C. 3
  4. D. 14

Correct answer: A. 7

The expansion is 2 * x > y ? x : y. Multiplication binds tightest, so the condition is (2 * 3) > 7, which is false, and the result is y = 7, not twice it. Parenthesising the whole body, ((a) > (b) ? (a) : (b)), gives the intended 14.

More C Programming questions

Exams that ask this topic