What is the return value of executing the recursive Python function `mystery(4)`?

def mystery(n):
if n <= 1:
return 1
return n * mystery(n - 1)

Answer and explanation

Correct answer: A. 24

  1. Step 1: Trace the recursive calls: `mystery(4) = 4 * mystery(3)`.
  2. Step 2: `mystery(3) = 3 mystery(2)`, `mystery(2) = 2 mystery(1)`, and `mystery(1) = 1` (base case). Unwinding gives: (factorial function ).
Answer
24.
Why not the others: 10 is the triangular sum (); 16 is ; 12 is .

Practise more Algorithms and Python Programming.

Free questions with an explanation for every one, and full timed EmSAT Achieve (UAE) mock tests.