Bird
0
0

What will this Ruby code print?

medium📝 Predict Output Q5 of 15
Ruby - Concurrent Programming
What will this Ruby code print?
t1 = Thread.new { sleep 0.2; puts "First" }
t2 = Thread.new { puts "Second" }
t1.join
AFirst
BSecond First
CSecond
DFirst Second
Step-by-Step Solution
Solution:
  1. Step 1: Analyze thread execution order

    Thread t2 prints "Second" immediately. Thread t1 sleeps 0.2 seconds before printing "First".
  2. Step 2: Understand join effect

    Main thread waits for t1 to finish but does not wait for t2. So "Second" prints first, then "First".
  3. Final Answer:

    Second\nFirst -> Option B
  4. Quick Check:

    Sleep delays first thread, so second prints first [OK]
Quick Trick: Sleep delays thread; join waits only for specified thread [OK]
Common Mistakes:
  • Assuming threads run in creation order
  • Ignoring sleep delay effect
  • Expecting join to wait for all threads

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes