Bird
0
0

Given this agent code snippet, what will be printed?

medium📝 Predict Output Q4 of 15
Agentic AI - Future of AI Agents
Given this agent code snippet, what will be printed? class Agent: def __init__(self): self.state = 0 def perceive(self): self.state += 1 def act(self): print(self.state) agent = Agent() for _ in range(3): agent.perceive() agent.act()
A1 1 1
B0 1 2
C1 2 3
D3 3 3
Step-by-Step Solution
Solution:
  1. Step 1: Trace the loop iterations

    Each loop calls perceive() which increments state by 1, then act() prints the state.
  2. Step 2: Calculate printed values

    After first iteration, state=1 printed; second iteration, state=2; third iteration, state=3.
  3. Final Answer:

    1 2 3 -> Option C
  4. Quick Check:

    State increments before print = 1,2,3 [OK]
Quick Trick: State increments before print each loop iteration [OK]
Common Mistakes:
  • Printing state before increment
  • Assuming state resets each loop
  • Confusing loop count with state value

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Agentic AI Quizzes