Question-14
GATE-2023 (modified)
    Python programming
    recursion
  Consider the following program:
def main():
    f1()
    f2(2)
    f3()
    return 0
def f1():
    return 1
def f2(X):
    f3()
    if X == 1:
        return f1()
    else:
        return X * f2(X - 1)
def f3():
    return 5
main()Which one of the following options represents the activation tree corresponding to the main function?
- 
main ├── f1 ├── f2 │ ├── f3 │ └── f2 │ ├── f3 │ └── f1 └── f3
- 
main ├── f1 ├── f2 │ ├── f3 │ └── f1 └── f3
- 
main ├── f1 ├── f2 │ ├── f3 │ └── f1 └──
- 
main ├── f1 ├── f2 │ ├── f3 │ ├── f2 │ └── f1 └── f3
Answer
- 
main ├── f1 ├── f2 │ ├── f3 │ └── f2 │ ├── f3 │ └── f1 └── f3
- 
main ├── f1 ├── f2 │ ├── f3 │ └── f1 └── f3
- 
main ├── f1 ├── f2 │ ├── f3 │ └── f1 └──
- 
main ├── f1 ├── f2 │ ├── f3 │ ├── f2 │ └── f1 └── f3
Solution
- main calls f1,f2andf3. andf1andf3returns.
- In f2,f3is called andf2is called again withX=1as the condition goes to else part.
- In the new f2,f3is called andf1is called end the calls end there.
