ProgrammingVery hardProduct company OA
What is printed?
What is printed?
struct N { int v; struct N *next; };
struct N n[5] = {{1, n + 1}, {2, n + 2}, {3, n + 3}, {4, n + 4}, {5, 0}};
struct N *h = n, *prev = 0;
for (int k = 0; k < 3 && h; k++) {
struct N *t = h->next; h->next = prev; prev = h; h = t;
}
n[0].next = h;
for (struct N *p = prev; p; p = p->next) printf("%d", p->v);- A. 321
- B. 32154
- C. 12345
- D. 32145
Correct answer: D. 32145
Three iterations of the standard reversal turn 1→2→3 into 3→2→1 with prev at node 3 and h left at node 4 (the untouched remainder). The old head, node 1, is now the tail of the reversed part, and n[0].next = h splices it to 4→5. Walking from prev prints 32145. Without that splice the list would end after 1 (output 321) and nodes 4, 5 would be unreachable – the usual bug in reverse-first-k.
More C Programming questions
- What is printed? c int lb(const int a, int n, int x) { int lo = 0, hi = n; while (lo < hi) { int mid = lo + (hi - lo) / 2; if (a[mid] < x)…
- What is printed? c int a = 7, b = 2; double x = a / b; double y = (double)a / b; double z = (double)(a / b); int w = (int)(-3.7) +…
- What is printed? c enum { RED = 2, GREEN, BLUE = GREEN + 4, N }; int a[] = { [RED] = 5, [BLUE] = 1, 9, [GREEN] = 6, 4 }; printf("%zu %d %d…
- Assume 8-bit char, 16-bit unsigned short and 32-bit int. What is printed? c int big = 300; unsigned char uc = big; unsigned short us = -1…
- What is printed? c int slot(int i) { static int box[3]; return &box[i % 3]; } int main(void) { slot(0) = 5; slot(4) = 7; int p = slot(3)…
- What is printed? c #define A 1 #define B #if defined(B) && A + C #define X 10 #else #define X 20 #endif #undef A #ifdef A #define Y 1…