Bird
0
0

What will this Ruby code output?

medium📝 Predict Output Q4 of 15
Ruby - Concurrent Programming
What will this Ruby code output?
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 to 2 with pauses, then main waits for thread to finish because of join.
  2. Step 2: Determine output order

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

    0 1 2 Done -> Option A
  4. Quick Check:

    Thread.join waits for thread completion = Output order correct [OK]
Quick Trick: Thread.join waits for thread to finish before continuing [OK]
Common Mistakes:
  • Ignoring thread.join and printing 'Done' first
  • Expecting numbers and 'Done' on same line
  • Confusing sleep effect on output order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes