Python Programming
June 17, 2025 at 05:29 PM
*Here are the Answers for the previous quizzes* ✅
*Q1. What is required for a recursive function to stop calling itself?*
✅ *Answer: C. A base case*
A base case is the condition under which the recursive function stops calling itself. Without it, the function would recurse infinitely and crash.
*Q2. What will be the output of this code?*
def mystery(n):
if n == 0:
return 0
else:
return n + mystery(n - 1)
print(mystery(3))
✅ *Answer: C. 6*
This computes the sum 3 + 2 + 1 + 0 = 6.
It works like this:
mystery(3) → 3 + mystery(2) → 3 + 2 + mystery(1) → 3 + 2 + 1 + mystery(0) → 0
*Q3. What is the main risk of writing a recursive function without a base case?*
✅ *Answer: C. It results in a RecursionError*
Without a base case, the function keeps calling itself until Python’s maximum recursion depth is exceeded, which throws a RecursionError.
*Q4. What does the following function compute?*
def compute(n):
if n <= 1:
return n
else:
return compute(n - 1) + compute(n - 2)
✅ *Answer: B. Fibonacci*
This is the classic recursive definition of the Fibonacci sequence:
F(n) = F(n-1) + F(n-2)
*Q5. Which of the following is true about recursive functions?*
✅ *Answer: C. They solve problems by breaking them into smaller subproblems*
Recursion breaks complex problems into simpler instances of the same problem, solving each recursively.
*React with ❤️ if you're ready for the next topic*
Python Coding Challenge: https://whatsapp.com/channel/0029VaiM08SDuMRaGKd9Wv0L/1661
❤️
🇮🇳
❤
👍
🇵🇸
😂
😮
🙏
😢
🇮🇱
296