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
Question
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
Solution
The correct answer is 17.
Here's the step-by-step explanation:
- The function
testis called with the arguments4and7. - Since
iis not0, the function calls itself with the argumentsi-1(which is3) andi+j(which is11). - The function is now called with
3and11. Again, sinceiis not0, the function calls itself withi-1(which is2) andi+j(which is14). - The function is now called with
2and14. Again, sinceiis not0, the function calls itself withi-1(which is1) andi+j(which is16). - The function is now called with
1and16. Again, sinceiis not0, the function calls itself withi-1(which is0) andi+j(which is17). - Finally, the function is called with
0and17. Sinceiis0, the function returnsj, which is17.
So, the output of the code is 17.
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))Options513Infinite loop15
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 fun(n): if (n > 100): return n - 5 return fun(fun(n+11)); print(fun(45))Options7410050Infinite loop
What will be the output of the following code?for i in range(1,5): if i == 3: continue else: print(i,end = " ")Options: Pick one correct answer from below1 2 3 41 2 41 2 3 4 51 2
Select the correct answerWhat will be the output of the following Python code?def example(k = 1, l = 2): k = k + l l = l + 1 print(k, l)example(l = 1, k = 2)OptionsAn exception is thrown because of conflicting values3 33 21 2
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.