Bird
0
0

Given this code snippet, what will be printed?

medium📝 Predict Output Q4 of 15
Agentic AI - Production Agent Architecture
Given this code snippet, what will be printed?
from threading import Thread

class Agent:
    def __init__(self, id):
        self.id = id
    def run(self):
        print(f"Agent {self.id} running")

agents = [Agent(i) for i in range(3)]
threads = [Thread(target=agent.run) for agent in agents]
for t in threads:
    t.start()
for t in threads:
    t.join()
ANo output because threads are not started
BAgent 0 running\nAgent 1 running\nAgent 2 running (always in order)
CAgent 0 running\nAgent 1 running\nAgent 2 running (order may vary)
DError due to missing join() calls
Step-by-Step Solution
Solution:
  1. Step 1: Understand threading behavior

    Threads start and run concurrently, so print order can vary.
  2. Step 2: Analyze code

    Threads are started and joined properly, so all agents print their messages, but order is not guaranteed.
  3. Final Answer:

    Agent 0 running\nAgent 1 running\nAgent 2 running (order may vary) -> Option C
  4. Quick Check:

    Thread output order = Non-deterministic [OK]
Quick Trick: Thread prints may appear in any order [OK]
Common Mistakes:
  • Assuming print order is always sequential
  • Thinking threads don't start without join()
  • Expecting errors due to missing synchronization

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Agentic AI Quizzes