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

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

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

Solution

The output of the given Python code will be 15. 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 equals 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 2 and 12.
  6. The function is called again with arguments 1 and 14.
  7. The function is called one more time with arguments 0 and 15.
  8. Now, since p is 0, the function returns q, which is 15.
  9. Therefore, the output of the code is 15.

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

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

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 p(q):    q = q + [5] r = [1, 2, 3, 4]p(r)print(len(r))Options51An exception is thrown4

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.