Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Basics and Runtime
What will be the output of this Ruby code?
count = 0
3.times do
  count += 1
end
print count
A3
B0
C1
DError
Step-by-Step Solution
Solution:
  1. Step 1: Understand the times loop

    The times method runs the block 3 times, increasing count by 1 each time.
  2. Step 2: Calculate final count

    Starting from 0, after 3 increments, count becomes 3.
  3. Final Answer:

    3 -> Option A
  4. Quick Check:

    0 + 1 + 1 + 1 = 3 [OK]
Quick Trick: times runs block that many times, increment count each time [OK]
Common Mistakes:
  • Thinking count stays 0
  • Confusing times with for loop syntax
  • Expecting error due to block

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes