ProgrammingVery hardProduct company OA
Assume 8-bit unsigned char. What is printed?
Assume 8-bit unsigned char. What is printed?
unsigned int count = 0;
for (unsigned char i = 250; i != 4; i += 3) count++;
printf("%u", count);- A. 174
- B. 88
- C. 2
- D. The loop never terminates
Correct answer: A. 174
i wraps modulo 256 (unsigned arithmetic after conversion back to unsigned char is well defined). We need the smallest k ≥ 0 with 250 + 3k ≡ 4 (mod 256), i.e. 3k ≡ 10. Because gcd(3, 256) = 1 the step visits every residue, so the loop does terminate: 3⁻¹ ≡ 171 (3·171 = 513), k ≡ 1710 ≡ 174. One lap is not enough: after the first wrap i takes 0, 3, 6, …, after the second 2, 5, 8, …, and only the third lap (1, 4) hits 4. 'Never terminates' would be right for a step such as i += 4, which only ever reaches values ≡ 2 (mod 4).
More C Programming questions
- 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…
- What is printed? c int calls; int f(int n) { calls++; return n < 3 ? n : f(n - 1) + f(n - 3); } int main(void) { int r = f(8); printf("%d…
- What is printed? c char s[] = "aaabccdddd"; char r = s, w = s; while (r) { char c = r; int n = 0; while (r == c) { r++; n++; } w++ = c; if…