ProgrammingVery hardTCS NQT · Infosys
What is printed?
What is printed?
#include <stdio.h>
struct N { int v; struct N *nx; };
struct N *rev(struct N *h, struct N *prev) {
if (!h) return prev;
struct N *nx = h->nx;
h->nx = prev;
return rev(nx, h);
}
int main(void) {
struct N c = {3, 0}, b = {2, &c}, a = {1, &b};
struct N *h = rev(&a, 0);
for (; h; h = h->nx) printf("%d ", h->v);
}- A. 1 2 3
- B. 2 3 1
- C. 3 2 1
- D. 3 1 2
Correct answer: C. 3 2 1
The recursion reverses the links one node at a time and returns the old tail as the new head, so the list prints 3, 2, 1.
More C Programming questions
- What is printed? c #include <stdio.h> int main(void) { unsigned long long r = 1, b = 7; int e = 222; while (e) { if (e & 1) r = r b %…
- What is printed? c #include <stdio.h> int main(void) { unsigned h = 0; for (const char s = "abcdefgh"; s; s++) h = (h << 5) ^ (unsigned…
- What is printed? c #include <stdio.h> int pc(int x) { int c = 0; while (x) { c += x & 1; x >>= 1; } return c; } int cmp(const void a…
- What is printed? c #include <stdio.h> int C(int n) { if (n == 0) return 1; int s = 0; for (int i = 0; i < n; i++) s += C(i) C(n - 1 - i)…
- What is printed? Assume unsigned int is 32 bits. c #include <stdio.h> int main(void) { unsigned x = (unsigned)-5; printf("%u", x >> 31); }
- What is printed? c #include <stdio.h> int main(void) { int m[2][3] = {{1, 2, 3}, {4, 5, 6}}; int (p)[3] = m; p++; printf("%d", p); }