Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q4 of 15
Ruby - Loops and Iteration
What will be the output of this Ruby code?

for i in 1..3 do
puts i * 2
end
A3 6 9
B2 4 6
C1 2 3
DError: syntax error
Step-by-Step Solution
Solution:
  1. Step 1: Understand the loop iterations

    The loop runs with i = 1, 2, 3.
  2. Step 2: Calculate output for each iteration

    Each iteration prints i * 2: 2, 4, 6.
  3. Final Answer:

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

    Output = doubled numbers 2 4 6 [OK]
Quick Trick: Multiply inside loop to transform values [OK]
Common Mistakes:
MISTAKES
  • Printing i instead of i*2
  • Syntax errors from missing do/end
  • Confusing output order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes