Functions · Class 11 · CBSE · Science Computer Science
What is the output of the following Python code? def mystery(n): if n <= 1: return 1 return n mystery(n - 2) print(mystery(5))
Practice question from Functions (Class 11 · CBSE · Science Computer Science), with the correct answer and a step-by-step solution.
hard · MCQ · Q-319499
What is the output of the following Python code? def mystery(n): if n <= 1: return 1 return n * mystery(n - 2)
print(mystery(5))
- A.5
- B.8
- C.15Correct answer
- D.25
Solution
The function is recursive. mystery(5) computes 5 * mystery(3), then 3 * mystery(1); since mystery(1) returns 1, the final result is 5 * 3 * 1 = 15.
More from Functions
- What is the output of the following Python code? def add(a, b=2): return a + b print(add(3))
- What is the output of the following Python code? x = 10 def change(): global x x = x + 5 return x print(change(), x)
- Explain the difference between formal parameters and actual parameters in Python functions. Give a suitable example using a function call.
- What is the output of the following Python code? def product(a, b): return a b print(product(b=3, a=4))
Practise more Computer Science
Sign up free for adaptive practice on this chapter, with instant feedback and hints on every question.
Start practising freeFree to start · No credit card needed