Bird
0
0

Identify the error in this code generated by an agent for a factorial function:

medium📝 Debug Q6 of 15
Agentic AI - Real-World Agent Applications

Identify the error in this code generated by an agent for a factorial function:

def factorial(n):
    if n == 0:
        return 0
    else:
        return n * factorial(n-1)
AMissing colon after function definition
BBase case returns 0 instead of 1
CIncorrect recursive call syntax
DFactorial function should not use recursion
Step-by-Step Solution
Solution:
  1. Step 1: Review factorial base case

    Factorial of 0 is 1, so base case should return 1, not 0.
  2. Step 2: Check other syntax elements

    Colons and recursion syntax are correct; recursion is valid here.
  3. Final Answer:

    Base case returns 0 instead of 1 -> Option B
  4. Quick Check:

    Factorial base case = return 1 [OK]
Quick Trick: Factorial 0! equals 1, not 0 [OK]
Common Mistakes:
  • Returning 0 for base case
  • Missing colons
  • Thinking recursion is invalid

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Agentic AI Quizzes