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
Solution
The output of the given Python code will be 15. Here's the step-by-step explanation:
- The function
demo(p, q)is defined, which takes two argumentspandq. - If
pequals 0, it returnsq. Otherwise, it calls itself with argumentsp-1andp+q. - The function is called with arguments 4 and 5.
- Since
pis not 0, the function calls itself with argumentsp-1andp+q, i.e., 3 and 9. - Again, since
pis not 0, the function calls itself with arguments 2 and 12. - The function is called again with arguments 1 and 14.
- The function is called one more time with arguments 0 and 15.
- Now, since
pis 0, the function returnsq, which is 15. - Therefore, the output of the code is 15.
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
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.