Bird
0
0

What will be the output when you enter this in IRB?

medium📝 Predict Output Q13 of 15
Ruby - Basics and Runtime
What will be the output when you enter this in IRB?
5.times { |i| puts i }
A5 times printed on one line
B1 2 3 4 5 (each on a new line)
C0 1 2 3 4 (each on a new line)
DError: undefined method 'times'
Step-by-Step Solution
Solution:
  1. Step 1: Understand the times method

    The times method runs the block 5 times, with i from 0 to 4.
  2. Step 2: Check what puts i does inside the block

    It prints each value of i on a new line, so output is 0,1,2,3,4 each on separate lines.
  3. Final Answer:

    0 1 2 3 4 (each on a new line) -> Option C
  4. Quick Check:

    5.times counts from 0 to 4 [OK]
Quick Trick: times starts counting from zero [OK]
Common Mistakes:
  • Thinking times starts at 1
  • Expecting output on one line
  • Assuming times is undefined in IRB

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes