ProgrammingVery hardProduct company OA
What is printed?
What is printed?
struct N { struct N *nx; } n[5];
for (int i = 0; i < 4; i++) n[i].nx = &n[i + 1];
n[4].nx = &n[2];
struct N *s = n, *f = n;
int steps = 0;
do { s = s->nx; f = f->nx->nx; steps++; } while (s != f);
printf("%d %td", steps, s - n);- A. 2 2
- B. 4 2
- C. 3 2
- D. 3 3
Correct answer: D. 3 3
The list is 0 → 1 → 2 → 3 → 4 → back to 2. Slow and fast pointers: after step 1 they are at 1 and 2, after step 2 at 2 and 4, after step 3 at 3 and 3, where they meet. The meeting point is inside the cycle but need not be its start (2).
More C Programming questions
- On a two's-complement machine with C99 division, what is printed? c printf("%d %d", -13 % 8, -13 & 7);
- Assume IEEE-754 single-precision float and 32-bit unsigned int. What is printed? c union { float f; unsigned u; } v = {1.0f}…
- What is printed? c #line 100 int a = LINE; int b = LINE; printf("%d %d", a, b);
- What is printed? c #define CAT(a, b) a##b printf("%d", CAT(1, 2) + CAT(0x, 1F));
- What is printed? c #define f(x) ((x) 2) int (f)(int x) { return x + 100; } int main(void) { printf("%d %d", f(1), (f)(1)); }
- What is printed? c if (strcmp("abc", "abc")) puts("same"); else puts("different");