Bird
0
0

Given this code, what will be the output?

hard📝 Application Q15 of 15
Ruby - Blocks, Procs, and Lambdas

Given this code, what will be the output?

pairs = [[1, 2], [3, 4], [5, 6]]
pairs.each do |a, b|
  puts a + b
end
A[1, 2] [3, 4] [5, 6]
B3 7 11
CError: wrong number of block parameters
D1 3 5
Step-by-Step Solution
Solution:
  1. Step 1: Understand multiple block parameters

    The block parameters a and b receive each inner array's two elements separately.
  2. Step 2: Calculate sum for each pair

    For each pair, the code prints the sum: 1+2=3, 3+4=7, 5+6=11.
  3. Final Answer:

    3 7 11 -> Option B
  4. Quick Check:

    Sum of pairs = 3,7,11 [OK]
Quick Trick: Use multiple block parameters to unpack arrays inside each [OK]
Common Mistakes:
  • Expecting array printed instead of sum
  • Thinking block parameters can't unpack arrays
  • Assuming syntax error with two parameters

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes