Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Loops and Iteration
What will be the output of this Ruby code?
3.times { |i| puts i * 2 }
A0 2 4
B1 2 3
C2 4 6
D0 1 2
Step-by-Step Solution
Solution:
  1. Step 1: Understand the iteration count and block variable

    The 3.times runs the block 3 times with i values 0, 1, 2.
  2. Step 2: Calculate the output for each iteration

    Each iteration prints i * 2: 0*2=0, 1*2=2, 2*2=4.
  3. Final Answer:

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

    Multiply index by 2 = 0, 2, 4 [OK]
Quick Trick: Multiply block index by 2 to get output [OK]
Common Mistakes:
  • Assuming index starts at 1
  • Printing index without multiplication
  • Confusing output order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes