Bird
0
0

What is the output of this Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Loops and Iteration
What is the output of this Ruby code?
i = 1
while i <= 3 do
  puts i
  i += 1
end
A1 2 3
B1 2 3 4
C0 1 2 3
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Trace the loop iterations

    i starts at 1. Loop runs while i <= 3. It prints i then adds 1 each time.
  2. Step 2: List printed values

    Prints 1, then 2, then 3. When i becomes 4, condition fails and loop stops.
  3. Final Answer:

    1 2 3 -> Option A
  4. Quick Check:

    Prints 1 to 3 inclusive [OK]
Quick Trick: Count from start to condition limit inclusive [OK]
Common Mistakes:
  • Including 4 in output
  • Starting from 0 instead of 1
  • No output due to syntax errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes