Bird
0
0

What will be the output of this code snippet?

medium📝 Predict Output Q5 of 15
Agentic AI - Production Agent Architecture
What will be the output of this code snippet? class Agent: def __init__(self): self.state = 0 def act(self): self.state += 1 if self.state > 2: raise RuntimeError('Too high') def recover(self): self.state = 0 print('Reset') def run(self): try: self.act() except RuntimeError: self.recover() agent = Agent() for _ in range(3): agent.run()
AReset printed once
BReset
CReset printed twice
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Trace state changes in loop

    First run: state=1, no error; second run: state=2, no error; third run: state=3, error raised.
  2. Step 2: Check recover() calls

    Recover is called only on third run, printing 'Reset' once.
  3. Final Answer:

    Reset printed once -> Option A
  4. Quick Check:

    Recover called once = D [OK]
Quick Trick: Recover triggers only when state > 2 [OK]
Common Mistakes:
  • Assuming no output
  • Thinking reset prints multiple times
  • Ignoring state increment

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Agentic AI Quizzes