Bird
0
0

Which Ruby code snippet correctly starts two threads that print numbers?

easy📝 Syntax Q12 of 15
Ruby - Concurrent Programming

Which Ruby code snippet correctly starts two threads that print numbers?

Option A:
threads = [Thread.new { puts 1 }, Thread.new { puts 2 }]
threads.each(&:join)

Option B:
Thread.new { puts 1 }
Thread.new { puts 2 }

Option C:
threads = Thread.new { puts 1 }
threads = Thread.new { puts 2 }
threads.join

Option D:
Thread.start { puts 1 }
Thread.start { puts 2 }
ACreates two threads and waits for both to finish
BCreates two threads but does not wait for them
CCreates one thread twice, only waits for last
DUses undefined Thread.start method
Step-by-Step Solution
Solution:
  1. Step 1: Analyze thread creation and joining

    Creates two threads and waits for both to finish. Creates two threads and calls join on both, ensuring both finish.
  2. Final Answer:

    Creates two threads and waits for both to finish -> Option A
  3. Quick Check:

    Thread.new + join = wait for threads [OK]
Quick Trick: Use join to wait for all threads to finish [OK]
Common Mistakes:
  • Not calling join and missing thread completion
  • Overwriting thread variables losing references
  • Using non-existent Thread.start method

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes