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?
fiber = Fiber.new do
  puts 'Hello'
  Fiber.yield
  puts 'World'
end
fiber.resume
fiber.resume
AHello
BWorld
CHello World
DHello World
Step-by-Step Solution
Solution:
  1. Step 1: Trace first resume call

    On first fiber.resume, it prints 'Hello' then hits Fiber.yield, pausing execution.
  2. Step 2: Trace second resume call

    Second fiber.resume resumes after yield, printing 'World'.
  3. Final Answer:

    Hello World -> Option D
  4. Quick Check:

    Fiber yield pauses, resume continues = Hello World [OK]
Quick Trick: Fiber.yield pauses; resume continues from yield point [OK]
Common Mistakes:
  • Expecting both prints on first resume
  • Ignoring Fiber.yield effect
  • Confusing output order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes