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?
count = 1
until count > 3 do
  puts count
  count += 1
end
A1 2 3 4
B1 2 3
C2 3 4
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the loop condition and variable

    The loop runs until count > 3. Starting from 1, it prints and increments count.
  2. Step 2: Trace each iteration

    Prints 1, increments to 2; prints 2, increments to 3; prints 3, increments to 4; now count > 3, loop stops.
  3. Final Answer:

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

    Prints 1 to 3 before stopping [OK]
Quick Trick: Until runs while condition false, stops when count > 3 [OK]
Common Mistakes:
  • Including 4 in output by mistake
  • Thinking loop runs while condition true
  • Forgetting to increment count

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes