Which of the following function calls lead to infinite recursion in the below code?def fun(a): if(a==0): return 0 return a*fun(a-1)*fun(0)fun(9)fun(-3)None of these
Question
Which of the following function calls lead to infinite recursion in the below code?def fun(a): if(a==0): return 0 return a*fun(a-1)*fun(0)fun(9)fun(-3)None of these
Solution 1
To determine which function calls lead to infinite recursion in the given code, let's analyze the function step by step.
Here is the provided code:
def fun(a):
if a == 0:
return 0
return a * fun(a - 1) * fun(0)
Analysis of the Function
-
Base Case:
if a == 0: return 0If
ais 0, the function returns 0. This is the base case that stops the recursion. -
Recursive Case:
return a * fun(a - 1) * fun(0)If
ais not 0, the function calls itself twice:fun(a - 1): This is a recursive call withadecremented by 1.fun(0): This is a call withaset to 0, which will return 0 due to the base case.
Function Calls
-
fun(9)- The function will call
fun(8),fun(7), ..., down tofun(1), and finallyfun(0). - Each of these calls will eventually reach the base case
fun(0), which returns 0. - However, since
fun(0)returns 0, the multiplicationa * fun(a - 1) * fun(0)will always result in 0. - Therefore,
fun(9)will not lead to infinite recursion, but it will return 0.
- The function will call
-
fun(-3)- The function will call
fun(-4),fun(-5), ..., and continue indefinitely becauseawill never reach 0. - There is no base case for negative values, so this will lead to infinite recursion.
- The function will call
-
None of these- This option is incorrect because
fun(-3)does lead to infinite recursion.
- This option is incorrect because
Conclusion
The function call fun(-3) leads to infinite recursion because there is no base case to handle negative values, causing the function to call itself indefinitely with decreasing negative values.
So, the correct answer is:
fun(-3)
Solution 2
Para determinar cuál de las llamadas a la función lleva a una recursión infinita, analicemos el código paso a paso:
def fun(a):
if(a==0):
return 0
return a*fun(a-1)*fun(0)
-
fun(9):
- Llamada inicial:
fun(9) ano es 0, así que se evalúa9 * fun(8) * fun(0)fun(0)retorna 0, así que la expresión se convierte en9 * fun(8) * 0- Cualquier número multiplicado por 0 es 0, así que
fun(9)retorna 0 y no hay recursión infinita.
- Llamada inicial:
-
fun(-3):
- Llamada inicial:
fun(-3) ano es 0, así que se evalúa-3 * fun(-4) * fun(0)fun(0)retorna 0, así que la expresión se convierte en-3 * fun(-4) * 0- Cualquier número multiplicado por 0 es 0, así que
fun(-3)retorna 0 y no hay recursión infinita.
- Llamada inicial:
-
None of these:
- Como hemos visto, ninguna de las llamadas
fun(9)ofun(-3)lleva a una recursión infinita.
- Como hemos visto, ninguna de las llamadas
Por lo tanto, la respuesta correcta es:
None of these
Similar Questions
Explain recursion function in python with example
What is a recursive function?0 pointsA function that calls other function.A function which calls itself.Both A and BNone of the above
What is a recursive function?a.A function that performs arithmetic operations.b.A function that never terminates.c.A function that calls itself within its body.d.A function that only uses iteration.Clear my choice
In a recursive function, when is the recursive call executed?
Select the correct answer Observe the following Python code?def a(n): if n == 0: return 0 else: return n*a(n - 1)def b(n, tot): if n == 0: return tot else: return b(n-2, tot-2)Optionsb() is tail recursive but a() isn’t Both a() and b() are tail recursive Both a() and b() aren’t tail recursivea() is tail recursive but b() isn’t
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.