Bird
0
0

What will the following Ruby code output?

medium📝 Predict Output Q4 of 15
Ruby - Basics and Runtime
What will the following Ruby code output?
arr = [1, 2, 3]
puts arr.map { |x| x * 2 }
A2 4 6
B246
C[2, 4, 6]
DError: map is not defined
Step-by-Step Solution
Solution:
  1. Step 1: Understand map and puts behavior

    arr.map returns a new array [2,4,6]. puts prints each element on a new line.
  2. Step 2: Predict output format

    puts on an array prints each element on its own line, so output is 2, then 4, then 6 on separate lines.
  3. Final Answer:

    2 4 6 -> Option A
  4. Quick Check:

    puts array = elements printed line by line [OK]
Quick Trick: puts array prints each element on a new line [OK]
Common Mistakes:
MISTAKES
  • Expecting array brackets in output
  • Thinking puts prints array as string
  • Assuming map is undefined

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes