Knowee
Questions
Features
Study Tools

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))

Question

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))

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

Solution

The given Python code defines a recursive function demo(p, q). This function checks if p is equal to 0. If p is 0, it returns q. Otherwise, it calls itself with parameters p-1 and p+q.

Let's break down the execution of demo(4,5):

  1. demo(4,5) is called. Since p is not 0, it calls demo(3, 4+5) which is demo(3,9).
  2. demo(3,9) is called. Since p is not 0, it calls demo(2, 3+9) which is demo(2,12).
  3. demo(2,12) is called. Since p is not 0, it calls demo(1, 2+12) which is demo(1,14).
  4. demo(1,14) is called. Since p is not 0, it calls demo(0, 1+14) which is demo(0,15).
  5. demo(0,15) is called. Since p is 0, it returns q which is 15.

So, the output of the Python code will be 15.

This problem has been solved

Similar Questions

Select the correct answerWhat 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))Options13Infinite loop155

correct answerWhat will be the output of the following Python code?def demo(x):  if (x > 100):    return x - 5  return demo(demo(x+11)); print(demo(45))

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 ))

ct answerWhat will be the output of the following Python code?def p(num):  if num == 0:    return 0  elif num == 1:    return 1  else:    return p(num-1)+p(num-2)for k in range(0,4):  print(p(k),end=" ")

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.