Bird
0
0

Given this code, what will be the output?

hard📝 Application Q9 of 15
Ruby - Blocks, Procs, and Lambdas
Given this code, what will be the output?
def test_lambda
  l = ->(x) { return x * 2 }
  l.call(5)
  return 10
end
puts test_lambda
ANothing printed
B10
CError: unexpected return
D5
Step-by-Step Solution
Solution:
  1. Step 1: Understand return behavior inside lambda

    Return inside a lambda returns from the lambda itself, not from the enclosing method.
  2. Step 2: Trace method execution

    l.call(5) returns 10 but method continues and returns 10 explicitly at the end.
  3. Final Answer:

    10 -> Option B
  4. Quick Check:

    Lambda return exits lambda only = 10 [OK]
Quick Trick: Return in lambda exits lambda, not method [OK]
Common Mistakes:
  • Thinking return exits the whole method
  • Expecting error from return inside lambda
  • Confusing proc return behavior with lambda

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes