Bird
0
0

What does the following Ruby for loop do?

easy📝 Conceptual Q11 of 15
Ruby - Loops and Iteration
What does the following Ruby for loop do?

for i in 1..3
puts i
end
APrints numbers 1, 2, and 3 each on a new line
BPrints numbers 1 to 2 only
CPrints numbers 0 to 3
DCauses a syntax error
Step-by-Step Solution
Solution:
  1. Step 1: Understand the range 1..3

    The range 1..3 includes the numbers 1, 2, and 3.
  2. Step 2: Loop through each number and print

    The loop prints each number on its own line using puts.
  3. Final Answer:

    Prints numbers 1, 2, and 3 each on a new line -> Option A
  4. Quick Check:

    for loop with 1..3 prints 1, 2, 3 [OK]
Quick Trick: Remember 1..3 includes 3, so loop prints 1, 2, 3 [OK]
Common Mistakes:
MISTAKES
  • Thinking 1..3 excludes 3
  • Confusing puts with print
  • Expecting syntax error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes