Bird
0
0

What does the expression arr[2..4] return when arr = [1, 2, 3, 4, 5, 6]?

easy📝 Conceptual Q1 of 15
Ruby - Arrays
What does the expression arr[2..4] return when arr = [1, 2, 3, 4, 5, 6]?
A[3, 4]
B[2, 3, 4]
C[3, 4, 5]
D[2, 3, 4, 5]
Step-by-Step Solution
Solution:
  1. Step 1: Understand the range 2..4

    The range 2..4 includes indices 2, 3, and 4.
  2. Step 2: Extract elements at indices 2, 3, and 4 from the array

    Elements at these indices are 3, 4, and 5 respectively.
  3. Final Answer:

    [3, 4, 5] -> Option C
  4. Quick Check:

    Range 2..4 extracts elements at indices 2 to 4 inclusive = [3, 4, 5] [OK]
Quick Trick: Range with two dots includes the end index [OK]
Common Mistakes:
  • Confusing .. with ... range operators
  • Starting index from 1 instead of 0
  • Expecting exclusive end index with ..

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes