ProgrammingVery hardProduct company OA
Which of these loops run their body exactly for i = 4, 3, 2, 1, 0 (once each) with an unsigned i?
Which of these loops run their body exactly for i = 4, 3, 2, 1, 0 (once each) with an unsigned i?
for (unsigned i = 5; i-- > 0; ) { /* (i) */ }
for (unsigned i = 4; i >= 0; i--) { /* (ii) */ }
for (unsigned i = 4; i > 0; i--) { /* (iii) */ }
for (unsigned i = 4; i + 1 > 0; i--) { /* (iv) */ }- A. (i) only
- B. (i) and (iv) only
- C. (ii) and (iv) only
- D. (i), (iii) and (iv) only
Correct answer: B. (i) and (iv) only
(i) tests the old value and decrements before the body, so the body sees 4 to 0. (iv) works because after 0, i wraps to UINT_MAX and i + 1 wraps to 0. (ii) never ends, since an unsigned value is always >= 0, and (iii) stops before 0.
More C Programming questions
- What is printed? c int x = -1; unsigned y = 1; printf("%d", (x < 0 ? x : y) < 0);
- What is printed? c int a[] = {1, 2, 3, 4, 5, 6}; int s = 0; for (int p = a; p < a + 6; p += 2) s += p p[1]; printf("%d", s);
- What is printed? c int cat(int n) { if (n <= 1) return 1; int s = 0; for (int i = 0; i < n; i++) s += cat(i) cat(n - 1 - i); return s; }…
- What is printed? c char comp[31] = {0}; int cnt = 0; for (int i = 2; i <= 30; i++) { if (comp[i]) continue; cnt++; for (int j = i i; j <=…
- What is printed? c int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int main(void) { int l = 1; for (int i = 1; i <= 10; i++) l = l…
- What is printed? c int isqrt(int n) { int lo = 0, hi = n, r = 0; while (lo <= hi) { int m = lo + (hi - lo) / 2; if ((long long)m m <= n) {…