ProgrammingHardTCS NQT · Infosys pattern

What is printed?

What is printed?
int a = 10;
int b = (a > 5) ? (a < 20 ? 1 : 2) : 3;
printf("%d", b);
  1. A. 1
  2. B. 3
  3. C. 2
  4. D. Compilation error: nested ternary requires explicit parentheses

Correct answer: A. 1

a>5 is true (10>5), so the outer ternary evaluates its 'then' branch: (a<20 ? 1 : 2). a<20 (10<20) is true, so this inner ternary yields 1. b = 1.

More C Programming questions

Exams that ask this topic