GATE / CS ARCHIVE

SIMILAR QUESTIONS / PROGRAMMING IN C

Trace persistent static state through recursive calls

4 questions solved the same way, newest first. Back to Programming in C

How these are solved

Keep one storage cell per function-local static and a separate local argument per activation, trace descent, then unwind. Questions ask a return value or printed sequence;

2 marks · NAT
Report issue

Scope, Lifetime & Storage Classes

Question 46, GATE 2020: Find fun2(5) on the first invocation of these C functions:
int fun1(int n) {
 static int i=0;
 if(n>0) { ++i; fun1(n-1); }
 return i;
}
int fun2(int n) {
 static int i=0;
 if(n>0) { i=i+fun1(n); fun2(n-1); }
 return i;
}
Tap to enlarge
Answer & solution

Official answer: 55

Step through it (18 steps)

Worked explanation

Use different names u and v in the trace for fun1’s and fun2’s separate static variables. Static initialization occurs once, not on every recursive call. The successive outer fun1 calls add 5,4,3,2,1 to u, making return values 5,9,12,14,15. At the corresponding fun2 entries, v becomes 5,14,26,40,55. The final fun2(0) returns 55 without changing it. When earlier calls unwind, they all return the same current static v=55; ignored recursive return values are not added again. This is why neither the triangular sum 15 nor the sum 5+4+3+2+1 is the final answer.

Derivation and answer

The two static variables named i belong to different functions and persist across calls. Starting from their initial zeros, fun1(5) increments its i five times and returns 5; subsequent fun1(4), fun1(3), fun1(2), fun1(1) return 9, 12, 14, 15 respectively. fun2 adds those five results into its own static i, so it returns 5+9+12+14+15=55. The recursive return values ignored inside fun1/fun2 do not undo updates to static storage. This assumes the displayed call is the first invocation, as intended.

Original paper, page 14 · Answer key

2 marks · MCQ
Report issue

Scope, Lifetime & Storage Classes

Question 35, GATE 2016: Find the printed sequence for count(3), where:
void count(int n){static int d=1;printf("%d ",n);printf("%d ",d);d++;if(n>1)count(n−1);printf("%d ",d);}
A:3 1 2 2 1 3 4 4 4. B:3 1 2 1 1 1 2 2 2. C:3 1 2 2 1 3 4. D:3 1 2 1 1 1 2.
The printed main wrapper calls count(3); assume the usual printing declarations.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Step through it (7 steps)

The static d is shared across recursive calls. Entry pairs are(3, 1),(2, 2),(1, 3); after the third increment d=4. Each of the three returning frames then prints 4. The complete sequence is 3 1 2 2 1 3 4 4 4, A. The snippet assumes normal printf declarations and acceptance of its void-main wrapper.

There is one static storage cell for d, not one copy per recursion frame. Trace entry: count(3) prints 3,1 and increments to 2; count(2) prints 2,2 and increments to 3; count(1) prints 1,3 and increments to 4. The base call skips recursion but still executes its final print. Returning callers also execute that final print and see the same shared 4. No decrement or new initialization happens during unwinding, which explains all three trailing 4s.

Original paper, page 14 · Answer key

2 marks · MCQ
Report issue

Recursive function with shared static state

Scope, Lifetime & Storage Classes

Question 42, GATE 2007: What is f(5) for the following C function?
int f(int n) {
    static int r=0;
    if(n<=0) return 1;
    if(n>3) { r=n; return f(n-2)+2; }
    return f(n-1)+r;
}
Options:
(A) 5
(B) 7
(C) 9
(D) 18
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

Step through it (11 steps)

1. Approach. The static r is one persistent shared object, not a fresh zero-initialized local for each recursive call. Record the single assignment r=5 before unwinding the recursion.

2. Derivation and interpretation. The static variable r is shared by all activations. In f(5), r becomes 5, and the return expression is f(3)+2. Calls f(3), f(2), and f(1) each recurse with n−1 and add the unchanged r=5. The base f(0)=1. Hence f(1)=6, f(2)=11, f(3)=16, and f(5)=18 (D). No smaller activation changes r, so operand evaluation order does not affect this particular call.

Original paper, page 10

2 marks · MCQ
Report issue

Recursion with a persistent static counter

Scope, Lifetime & Storage Classes

Question 31, GATE 2004: For the first invocation of this C function, what does f(1) return?
int f(int n) {
  static int i=1;
  if(n>=5) return n;
  n=n+i; i++;
  return f(n);
}
Options:
(A) 5
(B) 6
(C) 7
(D) 8
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

Step through it (6 steps)

1. On the first call, n=1,i=1; the update yields n=2,i=2. The next call yields n=4,i=3, then n=7,i=4.

2. Since7≥5, that call returns7 and the result propagates back, C. This assumes f(1) is the first call, since static i persists across calls.

3. Teaching check: Static storage is initialized once, not once per recursive activation. The entry states (n,i) are (1,1),(2,2),(4,3),(7,4). Replacing static i with an ordinary local initialized to 1 would produce a different trace and return 5.

Original paper, page 4