Bird
0
0

The following agent code has a bug:

medium📝 Debug Q14 of 15
Agentic AI - Future of AI Agents
The following agent code has a bug:
class Agent:
    def __init__(self):
        self.state = 0
    def perceive(self, input):
        self.state = input
    def act(self):
        return self.state * 2

agent = Agent()
agent.perceive(3)
agent.perceive(2)
output = agent.act()

What is the bug and how to fix it?
ABug: perceive overwrites state; fix by adding input to state.
BBug: act returns wrong value; fix by returning state + 2.
CBug: __init__ missing; fix by adding __init__ method.
DBug: perceive missing; fix by adding perceive method.
Step-by-Step Solution
Solution:
  1. Step 1: Identify the bug in perceive method

    perceive sets state = input, overwriting previous state instead of accumulating.
  2. Step 2: Fix by accumulating inputs

    Change perceive to add input to state: self.state += input.
  3. Final Answer:

    Bug: perceive overwrites state; fix by adding input to state. -> Option A
  4. Quick Check:

    Accumulate inputs in perceive [OK]
Quick Trick: Check if state accumulates or overwrites inputs [OK]
Common Mistakes:
  • Changing act method instead of perceive
  • Adding missing methods not needed here
  • Ignoring state update logic

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Agentic AI Quizzes