Bird
0
0

Which of the following is the correct syntax to print numbers 0 to 4 using times in Ruby?

easy📝 Syntax Q12 of 15
Ruby - Loops and Iteration
Which of the following is the correct syntax to print numbers 0 to 4 using times in Ruby?
Atimes(5) { |i| puts i }
B5.times do |i| puts i end
C5.times { i in puts i }
D5.times puts i
Step-by-Step Solution
Solution:
  1. Step 1: Check Ruby times syntax

    The correct syntax is calling times on an integer with a block, either with do...end or curly braces.
  2. Step 2: Identify the correct block parameter usage

    5.times do |i| puts i end uses 5.times do |i| puts i end, which is valid Ruby syntax to print 0 to 4.
  3. Final Answer:

    5.times do |i| puts i end -> Option B
  4. Quick Check:

    Correct block syntax = 5.times do |i| puts i end [OK]
Quick Trick: Use integer.times do |i| ... end for correct syntax [OK]
Common Mistakes:
  • Using times(5) which is invalid
  • Incorrect block syntax like { i in ... }
  • Omitting block braces or do/end

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes