ProgrammingHardIBM · Deloitte pattern
What is printed?
What is printed?
int a = 5;
int r = a > 3 ? 10 : 20 + 5;
printf("%d", r);- A. 15
- B. 10
- C. 25
- 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
- What is printed? c int a = 4; int r = ~a + 1 == -a; printf("%d", r);
- What is printed? c int a = 3, b = 4, c = 5; int r = a + b == c ? 1 : 0; printf("%d", r);
- What is printed? c int a = 5; int b = a++, c = ++a; printf("%d %d %d", a, b, c);
- What is printed? c int x = 2, y = 3, z = 4; int r = x + y == z || x y == z + 2; printf("%d", r);
- What is printed? c int a = 7; int r = a % 3 + 2 a / 7; printf("%d", r);
- What is printed? c int a = 5, b = 10; int r = a & b == 10; printf("%d", r);