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
Question
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
Solution
The output of the given Python code will be an Infinite loop.
Here's why:
The function demo(x) is a recursive function, which means it calls itself within its definition.
When you call demo(45), it checks if 45 is greater than 100. Since it's not, it calls demo(demo(45+11)).
This translates to demo(demo(56)), which again checks if 56 is greater than 100. Since it's not, it calls demo(demo(56+11)).
This process continues indefinitely because the input to the function demo(x) never becomes greater than 100, so the function keeps calling itself. This results in an infinite loop.
Similar Questions
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))
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 fun(n): if (n > 100): return n - 5 return fun(fun(n+11)); print(fun(45))Options7410050Infinite 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 snippet if x=1?x<<2Options1248
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.