Knowee
Questions
Features
Study Tools

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

What 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))
🧐 Not the exact question you are looking for?Go ask a question

Solution

The given Python code is a recursive function. Here's how it works:

  1. The function demo(x) is defined, which takes an integer x as an argument.
  2. Inside the function, there is an if condition that checks if x is greater than 100. If x is greater than 100, it returns x - 5.
  3. If x is not greater than 100, the function calls itself twice with the argument x+11.
  4. The print statement at the end calls the function with the argument 45.

Now, let's trace the execution of the code:

  1. demo(45) is called. Since 45 is not greater than 100, it calls demo(45+11) or demo(56).
  2. demo(56) is called. Since 56 is not greater than 100, it calls demo(56+11) or demo(67).
  3. demo(67) is called. Since 67 is not greater than 100, it calls demo(67+11) or demo(78).
  4. demo(78) is called. Since 78 is not greater than 100, it calls demo(78+11) or demo(89).
  5. demo(89) is called. Since 89 is not greater than 100, it calls demo(89+11) or demo(100).
  6. demo(100) is called. Since 100 is not greater than 100, it calls demo(100+11) or demo(111).
  7. Now, demo(111) is called. Since 111 is greater than 100, it returns 111 - 5 or 106.

So, the output of the Python code will be 106.

This problem has been solved

Similar Questions

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 snippet if x=1?x<<2Options1248

What will be the output for following code snippet?x=5x=y-=1print(y)810Syntax errorNo error but no output

What will be the output after the following statements?x = 5 if x > 15: print('yes') elif x == 15: print('equal') else: print('no')Options15equalnoyes

What will be the output of the following statement?print(17 // 10)Options: Pick one correct answer from below1.712None of the Above

1/3

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.