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?
pid = fork do
  puts 'Child process'
end
puts 'Parent process'
Process.wait(pid)
AChild process\nParent process
BParent process\nChild process
CChild process only
DParent process only
Step-by-Step Solution
Solution:
  1. Step 1: Understand execution order after fork

    The parent prints 'Parent process' immediately after fork. The child prints 'Child process' inside the fork block.
  2. Step 2: Analyze Process.wait effect

    Process.wait(pid) makes parent wait for child to finish, but parent's puts runs before waiting.
  3. Final Answer:

    Parent process\nChild process -> Option B
  4. Quick Check:

    Output order = parent then child [OK]
Quick Trick: Parent prints before waiting; child prints after fork block [OK]
Common Mistakes:
  • Assuming child prints before parent
  • Thinking Process.wait delays parent's puts
  • Ignoring concurrent execution order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes