Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q4 of 15
Ruby - Concurrent Programming
What will be the output of this Ruby code?
thread = Thread.new { 3.times { |i| puts i; sleep 0.1 } }
thread.join
puts "Done"
A0 1 2 Done
BDone 0 1 2
C0 1 2 Done
DDone
Step-by-Step Solution
Solution:
  1. Step 1: Understand thread execution and join

    The thread prints numbers 0, 1, 2 with pauses. The main thread waits for it to finish because of join.
  2. Step 2: Determine output order

    Numbers 0, 1, 2 print first, then "Done" prints after the thread finishes.
  3. Final Answer:

    0\n1\n2\nDone -> Option C
  4. Quick Check:

    Thread.join waits, so output order is numbers then Done [OK]
Quick Trick: join waits for thread, so output order is thread then main [OK]
Common Mistakes:
  • Assuming main prints before thread finishes
  • Ignoring sleep delays in thread
  • Confusing join with immediate continuation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes