ProgrammingVery hardProduct company OA
What is printed?
What is printed?
int i = 1, j = 2, k = 3;
int r = (i > j) ? i++ : j > k ? j++ : (k++, i + j + k);
printf("%d %d %d %d", i, j, k, r);- A. 1 3 3 2
- B. 1 2 4 6
- C. 1 2 3 6
- D. 1 2 4 7
Correct answer: D. 1 2 4 7
?: groups right to left, so the else-branch is the whole second conditional. Both tests fail, so only (k++, i + j + k) runs; the comma operator finishes k++ before the sum, giving 1 + 2 + 4 = 7. Only the chosen branch is evaluated, so i and j are untouched. '6' uses the old k.
More C Programming questions
- What is printed by r("ABCDE", 0)? c void r(const char s, int d) { if (!s) return; if (d % 2) putchar(s); r(s + 1, d + 1); if (d % 2 == 0)…
- What is printed? c int m[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; int (r)[3] = m + 1; printf("%d %d %d", r[1][0], (r)[2], (r - 1) +…
- Which of these statements about C's dynamic-memory functions are correct? (i) realloc(NULL, n) behaves like malloc(n). (ii) If realloc…
- Why is the line p = realloc(p, 2 n sizeof p); considered unsafe?
- Given char s[] = "ab"; and the declarations below, for which of p1 to p4 does the statement p = 'x'; compile? c typedef char PC; const PC…
- Assume 32-bit unsigned int. What is printed? c unsigned int u = 1u << 31; printf("%u %u", u >> 31, (u >> 30) | (u >> 28 & 1));