ProgrammingHardHCL · Tech Mahindra pattern
What is printed?
What is printed?
int x = 10;
int y = x > 5 ? x > 8 ? 1 : 2 : 3;
printf("%d", y);- A. 3
- B. 2
- C. Compile-time error: ambiguous nested ternary
- D. 1
Correct answer: D. 1
Nested ternaries associate from the right: x > 5 ? (x > 8 ? 1 : 2) : 3. x > 5 (10>5) is true, so evaluate the inner ternary: x > 8 (10>8) is also true, giving 1. The outer else-branch (3) is never reached.
More C Programming questions
- What is printed? c int i = 0; int arr[5] = {10, 20, 30, 40, 50}; while (i < 5) printf("%d ", arr[i++]); printf("%d", i);
- What is printed? c int a = 5; int b = (a > 3) ? (a = 10) : (a = 20); printf("%d %d", a, b);
- What is printed? c int x = 2, y = 3, z = 4; int result = x + y z - x / y; printf("%d", result);
- Assume a 64-bit LP64 ABI. What is printed? c int arr[5] = {1, 2, 3, 4, 5}; int start = arr; int end = arr + 5; int sum = 0; for (int p =…
- Assume a 64-bit LP64 ABI. What is printed? c int x = 42; void vp = &x; int ip = (int )vp; printf("%d", ip);
- Assume a 64-bit LP64 ABI. What is printed? c int a[3] = {1, 2, 3}; int p = &a[1]; printf("%d %d", p[-1], p[1]);