GATE Computer ScienceHardGATE CS (PYQ pattern)
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?
- A. 10 10 10 10 12 12
- B. 4 7 9 10 9 10
- C. 10 10 10 11 11 14 14
- D. 10 10 10 10 12 13
Correct answer: C. 10 10 10 11 11 14 14
x is global, so each print shows the running total: q(4) first adds 4, 3, 2, 1 (x = 10) before any print, giving 10 10 10, then q(1) from q(3) adds 1 (11, printed twice on the way out), and q(2) from q(4) adds 2 and 1 (x = 14, printed twice).
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- 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…
- A circular queue stores items in an array of 8 slots (indices 0 to 7) and keeps two fields, front (index of the first item) and count…
- The infix expression a + b (c ^ d - e) ^ (f + g h) - i (with ^ right-associative and higher than and +) is converted to postfix with the…
- Evaluate the postfix expression 9 3 - 4 2 5 - 7 3 / + where / is integer division truncating toward zero.
- Ten people numbered 1 to 10 stand in a circle (a circular linked list in increasing order). Counting starts at person s, who is counted as…