Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q5 of 15
Ruby - Loops and Iteration
What will be the output of this Ruby code?
count = 5
until count <= 0 do
  puts count
  count -= 2
end
A5 4 3 2 1 0
B5 3 1 -1
C5 3 1
DNo output (infinite loop)
Step-by-Step Solution
Solution:
  1. Step 1: Trace loop values

    count starts at 5; loop runs until count <= 0. Prints 5, then count becomes 3, prints 3, count becomes 1, prints 1, count becomes -1, condition count <= 0 is true, loop stops before printing -1.
  2. Step 2: Confirm output

    Printed values are 5, 3, 1 only.
  3. Final Answer:

    Output is 5, 3, 1 each on its own line -> Option C
  4. Quick Check:

    Until loop decrement output = C [OK]
Quick Trick: Until stops only when condition true exactly [OK]
Common Mistakes:
  • Assuming loop stops at negative count
  • Printing values after condition met
  • Misunderstanding loop exit condition

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes