Functions & Scope · Question 11 · Medium

This recursive function steps down by 2 on every call. What value is printed for f(7)?

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

print(f(7))
  1. 5040
  2. 105
  3. 35
  4. 15
Answer and explanation

Answer: B. 105

Explanation: f(7) = 7 × f(5) = 7 × 5 × f(3) = 7 × 5 × 3 × f(1) = 7 × 5 × 3 × 1 = 105. It is not a factorial because n drops by 2 each time.

Previous: The hit function tries to increase a module-level counter without declaring it… · Next: A function is passed to another function as an argument. What does twice print…

Practise functions & scope

Try a fresh set of functions & scope questions with an explanation after every answer, free and without signing up: Functions & Scope practice. Or sit a full placement mock test.

More functions & scope questions

More python practice

Comprehensions & Generators · Classes & Objects · Exceptions & Files · Built-ins & Standard Library · Variables, Types & Operators · Python Strings · Lists & Tuples · Dictionaries & Sets

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge