Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q5 of 15
Ruby - Basics and Runtime
What will be the output of this Ruby code?
arr = [1, 2, 3]
arr.each do |num|
  puts num * 2
end
A1 2 3
B2 4 6
C[2, 4, 6]
DError: undefined method 'each'
Step-by-Step Solution
Solution:
  1. Step 1: Understand each method on array

    each runs the block for each element, here multiplying by 2 and printing.
  2. Step 2: Output for each element

    1*2=2, 2*2=4, 3*2=6 printed on separate lines.
  3. Final Answer:

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

    each with puts prints each doubled number [OK]
Quick Trick: each prints elements one by one, not as array [OK]
Common Mistakes:
MISTAKES
  • Expecting array output instead of printed lines
  • Confusing each with map
  • Thinking each is undefined for arrays

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes