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?
num = 2
until num > 5 do
  puts num
  num += 2
end
A2 4 6
B2 4 6 8
C2 4
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Understand the loop condition

    The loop runs until num > 5 becomes true.
  2. Step 2: Trace the loop

    Initial num = 2 (<=5), prints 2, increments to 4.
    num = 4 (<=5), prints 4, increments to 6.
    num = 6 (>5), loop stops.
  3. Final Answer:

    2 4 -> Option C
  4. Quick Check:

    Loop stops once condition is true, so 6 is not printed [OK]
Quick Trick: Loop stops before condition is true, last value not printed [OK]
Common Mistakes:
MISTAKES
  • Including the value that breaks the condition
  • Misunderstanding loop exit condition
  • Assuming inclusive output beyond condition

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes