ProgrammingVery hardProduct company OA
What is printed?
What is printed?
int i = 0, j = 5, k = 0;
int r = (i++ || j++) && (k++, i + j > 1) ? i++ : (j += 2, k);
printf("%d %d %d %d", r, i, j, k);- A. 0 1 8 1
- B. 1 2 6 1
- C. 1 2 5 1
- D. 0 2 6 1
Correct answer: B. 1 2 6 1
||, &&, the comma operator and ?: all introduce sequence points, so this is fully defined. i++ yields 0 (i = 1), so j++ is evaluated: 5 is true (j = 6). The right side of && runs: k becomes 1 and i + j = 7 > 1. ?: has lower precedence than &&, so the whole && expression is the condition; it is true, r = i++ = 1 and i ends as 2. The third operand (j += 2, k) is never evaluated. Output 1 2 6 1.
More C Programming questions
- What is printed? c const char w[] = {"heap", "stack", "queue", "trie"}; const char p = w; printf("%c", ++p); printf("%c", ++p)…
- Assume 8-bit unsigned char. What is printed? c unsigned int count = 0; for (unsigned char i = 250; i != 4; i += 3) count++; printf("%u"…
- Assume 32-bit unsigned int and 64-bit unsigned long long. What is printed? c unsigned int a = 0x80000001u; unsigned int b = a 2; int c =…
- Assume a 64-bit LP64 ABI (4-byte int, 8-byte pointers and double, natural alignment). What is printed? c char s[] = "a0bc"; char p = s…
- What is printed? c int inc(int x) { return x + 1; } int dbl(int x) { return x 2; } int sqr(int x) { return x x; } int apply(int…
- What is printed? c int s = 0; for (int i = 0; i < 6; i++) { switch (i % 4) { case 0: s += 1; case 1: s += 2; break; case 2: continue…