ProgrammingHardTCS NQT · Infosys pattern
What is printed?
What is printed?
int a = 5, b = 2, c;
c = a > b ? a++ : --b;
printf("%d %d %d", a, b, c);- A. 6 2 5
- B. 5 1 1
- C. 6 2 6
- D. 5 2 5
Correct answer: A. 6 2 5
The ternary operator fully sequences evaluation of its condition before evaluating EXACTLY ONE of the two branches (never both). a > b (5 > 2) is true, so only a++ runs: c gets a's OLD value, 5 (post-increment), and a becomes 6. b is never touched since its branch (--b) never executes. Output: 6 2 5.
More C Programming questions
- What is printed? c int a = 2; int b = (a++, a + 3, a 2); printf("%d %d", a, b);
- What is printed? c int a = 10; int b = a++ + a++; printf("%d", a);
- What is printed? c int x = 5; if (x = 10) printf("true %d", x); else printf("false %d", x);
- What is printed? c int a = 1, b = 0, c = 1; int result = a || b && c; printf("%d", result);
- What is printed? c int x = 5; int y = x++ + (x = 10); printf("%d", y);
- Assume a 64-bit LP64 ABI. What is printed? c int a = 5, b = 10, c = 15; int arr[3]; arr[0] = &c; arr[1] = &a; arr[2] = &b; int sum = 0…