GATE Computer ScienceHardGATE CS (PYQ pattern)
void r(char *s) { if (s[0] == 0 || s[1] == 0) { printf("%s", s); return; } printf("%c%c", s[1], s[0]); r(s + 2); printf("%c", s[0]); } What does r("abcdefg") print?
- A. badcfegace
- B. badcfegeca
- C. badcfeg
- D. badcfegcea
Correct answer: B. badcfegeca
Going down the pairs are printed swapped: ba, dc, fe; the base case prints g; coming back each level prints its first character, from the deepest: e, c, a.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- int t(int n) { return n == 0 ? 0 : n % 3 + 10 t(n / 3); } What is t(97) + t(t(7))?
- int g(int a, int b) { return b == 0 ? a : g(b, a % b); } How many calls to g (including the first) does g(610, 987) make, and how many…
- int w(int n) { if (n < 0) return 0; if (n == 0) return 1; return w(n - 1) + w(n - 3) + w(n - 4); } What does w(10) return, and how many…
- int x = 0; void q(int n) { if (n > 0) { x += n; q(n - 1); printf("%d ", x); if (n > 2) q(n - 2); } } What does q(4) print?
- Integers 1, 2, 3, 4, 5 are pushed on an initially empty stack in this order; pops may be interleaved with pushes at any time. How many of…
- The integers 1 to 6 are pushed in this order onto a stack that can hold at most 2 elements at any moment; pops may be interleaved with…