Bird
0
0

What does the expression arr[1..3] do when arr = [10, 20, 30, 40, 50]?

easy📝 Conceptual Q11 of 15
Ruby - Arrays
What does the expression arr[1..3] do when arr = [10, 20, 30, 40, 50]?
AReturns a new array with elements at indexes 1, 2, and 3
BReturns a new array with elements at indexes 1 and 3 only
CReturns a new array with elements at indexes 1 and 2 only
DReturns a single element at index 3
Step-by-Step Solution
Solution:
  1. Step 1: Understand the range operator '..'

    The range 1..3 includes both 1 and 3, so it covers indexes 1, 2, and 3.
  2. Step 2: Apply slicing to the array

    Slicing arr[1..3] returns elements at indexes 1, 2, and 3, which are 20, 30, and 40.
  3. Final Answer:

    Returns a new array with elements at indexes 1, 2, and 3 -> Option A
  4. Quick Check:

    Range '..' includes end index = elements 20, 30, 40 [OK]
Quick Trick: Remember '..' includes the end index in slicing [OK]
Common Mistakes:
  • Thinking '..' excludes the end index
  • Confusing indexes with values
  • Assuming slicing changes the original array

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes