ProgrammingHardIBM · Deloitte pattern

What is printed?

What is printed?
int a = 5;
int r = a > 3 ? 10 : 20 + 5;
printf("%d", r);
  1. A. 15
  2. B. 10
  3. C. 25
  4. D. 30

Correct answer: B. 10

The ternary operator ?: has lower precedence than +, but the + here is entirely inside the FALSE branch (20+5=25), which is simply never evaluated since a>3 is true; the whole expression groups as (a>3) ? 10 : (20+5), and since the condition is true, r=10.

More C Programming questions

Exams that ask this topic