GATE / CS ARCHIVE

GATE CSE 2023 / QUESTION 36

GATE CSE 2023 Q36

Programming in C · Recursion · Full GATE CSE 2023 paper

GATE 2023, Q36 2 marks · MCQ
Report issue

Recursion

Question 36, GATE 2023: main calls f1(), f2(2), f3() in that order. f1 returns 1; f3 returns 5. f2(X) first calls f3(), then returns f1() if X==1, otherwise returns X*f2(X−1). Describe its activation tree and identify the matching source option.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Main has three ordered children f1, f2(2), f3. The f2(2) activation first calls f3 and then recursively calls f2(1). That activation calls f3 and, because X=1, calls f1. Thus f2(2) has children f3, f2(1), and f2(1) has children f3, f1.

Teaching detail. An activation-tree node represents one invocation, not one distinct function name. The recursive f2 calls at arguments 2 and 1 therefore occupy different nodes. The ordered tree is main[f1, f2(2)[f3, f2(1)[f3,f1]], f3]. Returning a value does not create another activation, so multiplying by X adds no node. Likewise the final f3 called by main is a sibling of f2, not a descendant. This trace verifies both call nesting and left-to-right sibling order.

Original paper, page 35 · Answer key