Knowee
Questions
Features
Study Tools

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

Question

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

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

Solution

The output of the given Python code will be 13. Here's the step-by-step explanation:

  1. The function demo(p, q) is defined, which takes two arguments p and q.
  2. If p is equal to 0, it returns q. Otherwise, it calls itself with arguments p-1 and p+q.
  3. The function is called with arguments 4 and 5.
  4. Since p is not 0, the function calls itself with arguments p-1 and p+q, i.e., 3 and 9.
  5. Again, since p is not 0, the function calls itself with arguments p-1 and p+q, i.e., 2 and 12.
  6. Again, since p is not 0, the function calls itself with arguments p-1 and p+q, i.e., 1 and 14.
  7. Again, since p is not 0, the function calls itself with arguments p-1 and p+q, i.e., 0 and 15.
  8. Now, since p is 0, it returns q, which is 15.
  9. However, this returned value is used in the previous call where p was 1 and q was 14. So, it subtracts 1 from the returned value and adds it to p, which gives 14.
  10. This process continues until the first call, where p was 4 and q was 5. So, the final output is 13.

This problem has been solved

Similar Questions

Select the 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))Options1005074Infinite loop

Select the correct answerWhat will be the output of the following Python code?def test(i,j): if(i==0): return j else: return test(i-1,i+j)print(test(4,7))OptionsInfinite loop 713 17

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

Select the correct answerWhat will be the output of the following Python code?def demo(a, b=3, c=6):  print('a is', a, 'and b is', b, 'and c is', c) demo(1, 5)demo(10, c = 15)demo(c = 30, a = 25)Optionsa is 5 and b is 1 and c is 10a is 10 and b is 3 and c is 15a is 3 and b is 25 and c is 30a is 1 and b is 5 and c is 6a is 10 and b is 3 and c is 15a is 25 and b is 3 and c is 30None of the mentioneda is 1 and b is 5 and c is 6a is 3 and b is 20 and c is 15a is 30 and b is 25 and c is 3

Select the correct answerWhat will be the output of the following Python code?def p(q):    q = q + [5] r = [1, 2, 3, 4]p(r)print(len(r))Options451An exception is thrown

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.