The thread prints numbers 0 to 4 with a short pause. The main thread waits for it to finish because of thread.join.
Step 2: Determine output order
The numbers 0 to 4 print first, each on its own line, then after the thread finishes, 'Done' prints.
Final Answer:
0 1 2 3 4 Done (each number on its own line) -> Option A
Quick Check:
Thread prints numbers before main prints 'Done' [OK]
Hint: join waits for thread to finish before continuing [OK]
Common Mistakes:
Assuming 'Done' prints before numbers
Thinking join blocks forever
Ignoring sleep delays in thread
4. Identify the error in this Ruby code snippet:
thread = Thread.new do
puts 'Start'
sleep 1
puts 'End'
end
puts 'Waiting'
thread.join
medium
A. The thread block is missing a closing end.
B. The main thread does not wait for the new thread to finish.
C. There is no error; the code runs correctly.
D. The sleep method cannot be used inside a thread.
Solution
Step 1: Check thread block syntax
The thread block is properly closed with end. Syntax is correct.
Step 2: Verify thread execution and join
The main thread prints 'Waiting' then calls thread.join to wait for the thread to finish. This is correct usage.
Final Answer:
There is no error; the code runs correctly. -> Option C
Quick Check:
Thread syntax and join usage are correct [OK]
Hint: Check block ends and join usage carefully [OK]
Common Mistakes:
Thinking sleep is invalid in threads
Assuming join is missing
Miscounting block ends
5. You want to create 3 threads that each print their thread number (1, 2, 3) and then print 'Finished' after all threads complete. Which code correctly achieves this?
hard
A. 3.times do |i|
Thread.new { puts i + 1 }
end
puts 'Finished'
B. threads = []
3.times do |i|
threads << Thread.new { puts i + 1 }
end
threads.each(&:join)
puts 'Finished'
C. threads = []
3.times do |i|
threads << Thread.new { puts i }
end
puts 'Finished'
threads.each(&:join)
D. threads = []
3.times do |i|
threads << Thread.new { puts i + 1 }
end
puts 'Finished'
threads.each(&:join)
Solution
Step 1: Create threads storing them in an array
threads = []
3.times do |i|
threads << Thread.new { puts i + 1 }
end
threads.each(&:join)
puts 'Finished' correctly creates 3 threads, each printing i + 1 (1, 2, 3), and stores them in threads.
Step 2: Wait for all threads to finish before printing 'Finished'
threads = []
3.times do |i|
threads << Thread.new { puts i + 1 }
end
threads.each(&:join)
puts 'Finished' calls threads.each(&:join) to wait for all threads, then prints 'Finished'. This ensures 'Finished' prints last.
Final Answer:
threads = []
3.times do |i|
threads << Thread.new { puts i + 1 }
end
threads.each(&:join)
puts 'Finished' -> Option B
Quick Check:
Store threads, join all, then print [OK]
Hint: Join all threads before printing final message [OK]
Common Mistakes:
Not storing threads to join later
Printing 'Finished' before threads complete
Using wrong thread index values
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.