Bird
0
0

What will this Ruby code print?

medium📝 Predict Output Q5 of 15
Ruby - Concurrent Programming
What will this Ruby code print?
pid = fork do
  puts 'Child'
  exit
end
puts 'Parent'
Process.wait(pid)
AParent\nChild
BChild\nParent
CParent only
DChild only
Step-by-Step Solution
Solution:
  1. Step 1: Analyze child process code

    The child prints 'Child' then calls exit to end immediately.
  2. Step 2: Analyze parent process code

    The parent prints 'Parent' immediately after fork, then waits for child to finish.
  3. Final Answer:

    Parent\nChild -> Option A
  4. Quick Check:

    Parent prints first, then child prints after wait [OK]
Quick Trick: Child exits after printing; parent prints after waiting [OK]
Common Mistakes:
  • Assuming child prints before parent
  • Ignoring exit stops child process
  • Thinking wait delays parent's puts

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes