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
,f2
andf3
. andf1
andf3
returns. - In
f2
,f3
is called andf2
is called again withX=1
as the condition goes to else part. - In the new
f2
,f3
is called andf1
is called end the calls end there.