Knowee
Questions
Features
Study Tools

what is the output of following python code without running the code on python compiler? def test( i, j ):    if(j==1):       return i    else:       return test( i+1, j-1 )print(test( 4, 6 ))

Question

what is the output of following python code without running the code on python compiler? def test( i, j ):    if(j==1):       return i    else:       return test( i+1, j-1 )print(test( 4, 6 ))

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

Solution

The function test is a recursive function. Here's how it works:

  1. The function test takes two arguments, i and j.
  2. If j is equal to 1, it returns i.
  3. If j is not equal to 1, it calls itself with i+1 and j-1.

Let's break down the call test(4, 6):

  1. j is not 1, so it calls test(5, 5).
  2. Again, j is not 1, so it calls test(6, 4).
  3. Again, j is not 1, so it calls test(7, 3).
  4. Again, j is not 1, so it calls test(8, 2).
  5. Again, j is not 1, so it calls test(9, 1).
  6. Now, j is 1, so it returns i, which is 9.

So, the output of the code will be 9.

This problem has been solved

Similar Questions

swerWhat will be the output of the following Python code?def demo(p,q):  if(p == 0):    return q  else:    return demo(p-1,p+q)print(demo(4,5))

What will be the output of the following Python code?1.  def foo():2.     try:3.       print(1)4.    finally:5.        print(2)6.   7.  foo()

What will be printed by the following code?for i in range(5):    for j in range(5, 0, -1):        if i == j:            print(i, end=" ")

What will be the output of the following Python code?def foo(k): k = [1]q = [0]foo(q)print(q)

What will be the output of the following program in Python?print(0 or 1)

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.