Bird
0
0

Given n = 3, which code snippet correctly prints the numbers 1 to n using times?

hard📝 Application Q9 of 15
Ruby - Loops and Iteration
Given n = 3, which code snippet correctly prints the numbers 1 to n using times?
A(1..n).times { |i| puts i }
Bn.times { |i| puts i }
Cn.times { |i| puts i + 1 }
Dn.times { puts i + 1 }
Step-by-Step Solution
Solution:
  1. Step 1: Understand times block variable range

    Block variable i runs from 0 to n-1.
  2. Step 2: Adjust output to print 1 to n

    Adding 1 to i prints numbers 1 through n.
  3. Final Answer:

    n.times { |i| puts i + 1 } -> Option C
  4. Quick Check:

    Print 1 to n by adding 1 to i [OK]
Quick Trick: Add 1 to block variable to start from 1 [OK]
Common Mistakes:
MISTAKES
  • Printing i directly (starts at 0)
  • Using times on a range
  • Using undefined variable i

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes