Bird
0
0

Identify the problem in this Ruby code snippet:

medium📝 Debug Q7 of 15
Ruby - Operators and Expressions
Identify the problem in this Ruby code snippet:
range = (1..5)
range.each do |n|
puts n
end
puts range[2]
ARange does not support indexing with []
BSyntax error in the block
CRange includes 6 which is incorrect
DNo problem, outputs numbers 1 to 5 and then 3
Step-by-Step Solution
Solution:
  1. Step 1: Check if range supports indexing

    Ruby ranges can be indexed like arrays, so range[2] returns the third element.
  2. Step 2: Understand the output

    The loop prints 1 to 5, and range[2] outputs 3 (the third number).
  3. Final Answer:

    No problem, outputs numbers 1 to 5 and then 3 -> Option D
  4. Quick Check:

    Ranges support indexing like arrays [OK]
Quick Trick: Ranges can be indexed like arrays starting at 0 [OK]
Common Mistakes:
MISTAKES
  • Thinking ranges cannot be indexed
  • Assuming syntax error in block
  • Confusing range boundaries

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes