Bird
0
0

Which of the following is the correct syntax for a for loop in Ruby to iterate over an array ["a", "b", "c"]?

easy📝 Syntax Q12 of 15
Ruby - Loops and Iteration
Which of the following is the correct syntax for a for loop in Ruby to iterate over an array ["a", "b", "c"]?
Afor x to ["a", "b", "c"] puts x end
Bfor (x : ["a", "b", "c"]) puts x end
Cfor x in ["a", "b", "c"] do puts x end
Dfor x in ["a", "b", "c"] puts x
Step-by-Step Solution
Solution:
  1. Step 1: Check the correct Ruby for loop syntax

    Ruby uses for variable in collection do ... end or without do.
  2. Step 2: Validate each option

    for x in ["a", "b", "c"] do puts x end uses correct syntax with for x in array do ... end. for (x : ["a", "b", "c"]) puts x end and C use invalid syntax. for x in ["a", "b", "c"] puts x misses end to close the loop.
  3. Final Answer:

    for x in ["a", "b", "c"] do puts x end -> Option C
  4. Quick Check:

    Correct Ruby for loop uses 'for x in collection do ... end' [OK]
Quick Trick: Remember: for x in collection do ... end is correct Ruby syntax [OK]
Common Mistakes:
  • Using colon or 'to' instead of 'in'
  • Omitting the 'end' keyword
  • Adding parentheses incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes