Bird
0
0

Identify the error in this agent code:

medium📝 Debug Q6 of 15
Agentic AI - Future of AI Agents
Identify the error in this agent code:
class Agent:
    def __init__(self):
        self.state = 0
    def act(self):
        state += 1
        return state

agent = Agent()
print(agent.act())
AIncorrect class name
BMissing self when updating state variable
Cact method missing return statement
DIndentation error in class
Step-by-Step Solution
Solution:
  1. Step 1: Check variable usage in act method

    Inside act, state is updated without self., so it refers to undefined local variable.
  2. Step 2: Identify correct usage

    Should use self.state += 1 to update instance variable.
  3. Final Answer:

    Missing self when updating state variable -> Option B
  4. Quick Check:

    Instance variables need self prefix [OK]
Quick Trick: Use self.variable to access instance variables [OK]
Common Mistakes:
  • Updating variable without self inside methods
  • Assuming local variable exists automatically
  • Ignoring error messages about undefined variables

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Agentic AI Quizzes