Knowee
Questions
Features
Study Tools

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

🧐 Not the exact question you are looking for?Go ask a question

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

  1. Base Case:

    if a == 0:
        return 0
    

    If a is 0, the function returns 0. This is the base case that stops the recursion.

  2. Recursive Case:

    return a * fun(a - 1) * fun(0)
    

    If a is not 0, the function calls itself twice:

    • fun(a - 1): This is a recursive call with a decremented by 1.
    • fun(0): This is a call with a set to 0, which will return 0 due to the base case.

Function Calls

  1. fun(9)

    • The function will call fun(8), fun(7), ..., down to fun(1), and finally fun(0).
    • Each of these calls will eventually reach the base case fun(0), which returns 0.
    • However, since fun(0) returns 0, the multiplication a * fun(a - 1) * fun(0) will always result in 0.
    • Therefore, fun(9) will not lead to infinite recursion, but it will return 0.
  2. fun(-3)

    • The function will call fun(-4), fun(-5), ..., and continue indefinitely because a will never reach 0.
    • There is no base case for negative values, so this will lead to infinite recursion.
  3. None of these

    • This option is incorrect because fun(-3) does lead to infinite recursion.

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)

This problem has been solved

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)
  1. fun(9):

    • Llamada inicial: fun(9)
    • a no es 0, así que se evalúa 9 * fun(8) * fun(0)
    • fun(0) retorna 0, así que la expresión se convierte en 9 * fun(8) * 0
    • Cualquier número multiplicado por 0 es 0, así que fun(9) retorna 0 y no hay recursión infinita.
  2. fun(-3):

    • Llamada inicial: fun(-3)
    • a no 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.
  3. None of these:

    • Como hemos visto, ninguna de las llamadas fun(9) o fun(-3) lleva a una recursión infinita.

Por lo tanto, la respuesta correcta es:

None of these

This problem has been solved

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

1/3

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.