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?
result = []
3.upto(5) { |i| result << i * 2 }
puts result.join(",")
A3,4,5
B6,8,10
C3,6,9
D5,4,3
Step-by-Step Solution
Solution:
  1. Step 1: Trace the loop from 3 upto 5

    The loop variable i takes values 3, 4, and 5.
  2. Step 2: Calculate and store values

    Each i is multiplied by 2 and added to result: 6, 8, 10.
  3. Final Answer:

    6,8,10 -> Option B
  4. Quick Check:

    Loop multiplies each number by 2 = B [OK]
Quick Trick: Multiply inside block to transform values [OK]
Common Mistakes:
  • Confusing loop range
  • Not multiplying inside block
  • Printing array incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes