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))
Question
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))
Solution
The given Python code is a recursive function. Here's how it works:
- The function
demo(x)is defined, which takes an integerxas an argument. - Inside the function, there is an
ifcondition that checks ifxis greater than 100. Ifxis greater than 100, it returnsx - 5. - If
xis not greater than 100, the function calls itself twice with the argumentx+11. - The
printstatement at the end calls the function with the argument45.
Now, let's trace the execution of the code:
demo(45)is called. Since 45 is not greater than 100, it callsdemo(45+11)ordemo(56).demo(56)is called. Since 56 is not greater than 100, it callsdemo(56+11)ordemo(67).demo(67)is called. Since 67 is not greater than 100, it callsdemo(67+11)ordemo(78).demo(78)is called. Since 78 is not greater than 100, it callsdemo(78+11)ordemo(89).demo(89)is called. Since 89 is not greater than 100, it callsdemo(89+11)ordemo(100).demo(100)is called. Since 100 is not greater than 100, it callsdemo(100+11)ordemo(111).- Now,
demo(111)is called. Since 111 is greater than 100, it returns111 - 5or106.
So, the output of the Python code will be 106.
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
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 demo(p,q): if(p == 0): return q else: return demo(p-1,p+q)print(demo(4,5))Options13Infinite loop155
What will be the output of the following program?for x in range(10): if x == 6: print(x,end="")print(x
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
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.