Bird
0
0

What is the output of this Ruby code?

medium📝 Predict Output Q4 of 15
Ruby - Loops and Iteration
What is the output of this Ruby code?
numbers = [1, 2, 3]
numbers.each { |n| puts n * 2 }
A2 4 6
B[2, 4, 6]
C1 2 3
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Understand the block operation

    The block multiplies each element by 2 and prints it.
  2. Step 2: Trace the output line by line

    For 1, prints 2; for 2, prints 4; for 3, prints 6, each on a new line.
  3. Final Answer:

    2\n4\n6 -> Option A
  4. Quick Check:

    Block prints doubled values line by line [OK]
Quick Trick: puts prints each value on a new line [OK]
Common Mistakes:
  • Expecting array output instead of printed lines
  • Confusing puts with return value
  • Ignoring multiplication inside block

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes