Bird
0
0

What will be printed when this Ruby code runs?

medium📝 Predict Output Q4 of 15
Ruby - Blocks, Procs, and Lambdas
What will be printed when this Ruby code runs?
def call_twice
  yield
  yield
end

call_twice { puts 'Hello' }
ANo output
BHello
CHello Hello
DError: no block given
Step-by-Step Solution
Solution:
  1. Step 1: Analyze method

    The method call_twice calls yield two times.
  2. Step 2: Block execution

    The block prints 'Hello' each time yield is called.
  3. Final Answer:

    Hello Hello -> Option C
  4. Quick Check:

    Yield calls block twice, prints twice [OK]
Quick Trick: Yield executes block each time it's called [OK]
Common Mistakes:
  • Assuming yield runs block only once
  • Expecting no output without explicit return
  • Thinking yield requires block_given? check always

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes