Bird
0
0

What will this Ruby code print?

medium📝 Predict Output Q5 of 15
Ruby - Basics and Runtime
What will this Ruby code print?
array = [1, 2, 3]
result = array.map { |n| n * 2 }
puts result.inspect
A[2, 4, 6]
B[1, 2, 3]
C[3, 4, 5]
DError: undefined method map
Step-by-Step Solution
Solution:
  1. Step 1: Understand map method on array

    map applies the block to each element, multiplying each by 2.
  2. Step 2: Check the resulting array

    Each element doubled: 1*2=2, 2*2=4, 3*2=6, so result is [2, 4, 6].
  3. Final Answer:

    [2, 4, 6] -> Option A
  4. Quick Check:

    map doubles elements = [2,4,6] [OK]
Quick Trick: map transforms each element in an array [OK]
Common Mistakes:
MISTAKES
  • Confusing map with each (which returns original array)
  • Expecting original array without changes
  • Thinking map is undefined for arrays

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes