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
Question
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
Solution
The correct answer is Infinite loop.
Here's why:
The function fun(n) is a recursive function, which means it calls itself within its definition.
When fun(45) is called, n is not greater than 100, so it goes to the return fun(fun(n+11)) line. This means it will call fun(56) (because 45+11 is 56).
Again, 56 is not greater than 100, so it will call fun(67), and this process will continue.
The value of n will keep increasing by 11 in each recursive call, but it will never reach a value greater than 100 because each time the function is called, it's called with n+11, and then the function is called again with that result.
So, the function will keep calling itself indefinitely, resulting in an infinite loop.
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))Options5074Infinite loop
Choose the correct answerWhat will be the output of the following program?def fun(): try: print(1) finally: print(2)fun()Select all the correct statements given below.Options211221
What will be the output of the following Python code?def fun(): x=15 print(x)x=12fun()Options: Pick one correct answer from belowError151215 12
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 test(i,j): if(i==0): return j else: return test(i-1,i+j)print(test(4,7))OptionsInfinite loop 713 17
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.