Bird
0
0

Given this code, what will be printed?

hard📝 Application Q9 of 15
Ruby - Blocks, Procs, and Lambdas
Given this code, what will be printed?
def test
  p = Proc.new { |x| return x * 2 }
  result = p.call(4)
  puts "After Proc call"
  result
end
puts test
AOnly 8
B"After Proc call" and then 8
C8 and then "After Proc call"
DOnly "After Proc call"
Step-by-Step Solution
Solution:
  1. Step 1: Analyze Proc return effect

    The return inside the Proc causes the entire method to return immediately with 8 (4 * 2).
  2. Step 2: Check if code after Proc call runs

    The line puts "After Proc call" is never executed because the method has already returned.
  3. Final Answer:

    Only 8 -> Option A
  4. Quick Check:

    Proc return exits method immediately = A [OK]
Quick Trick: Proc return exits method before later code runs [OK]
Common Mistakes:
  • Expecting "After Proc call" to print
  • Confusing Proc and lambda return
  • Assuming method continues after Proc return

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes