Bird
0
0

Given arr = [2, 4, 6, 8, 10, 12], write a Ruby expression to get every element from index 1 to 4 inclusive, then reverse that slice.

hard📝 Application Q8 of 15
Ruby - Arrays
Given arr = [2, 4, 6, 8, 10, 12], write a Ruby expression to get every element from index 1 to 4 inclusive, then reverse that slice.
Aarr[0..4].reverse
Barr[1...4].reverse
Carr[1..5].reverse
Darr[1..4].reverse
Step-by-Step Solution
Solution:
  1. Step 1: Identify the slice indices

    Indices 1 to 4 inclusive means elements at indices 1, 2, 3, and 4.
  2. Step 2: Slice and reverse the array

    Use arr[1..4] to get the slice, then call .reverse to reverse it.
  3. Final Answer:

    arr[1..4].reverse -> Option D
  4. Quick Check:

    Use .. for inclusive range and .reverse to reverse slice [OK]
Quick Trick: Use .. for inclusive range and .reverse to reverse slice [OK]
Common Mistakes:
MISTAKES
  • Using ... which excludes index 4
  • Including wrong indices in slice
  • Forgetting to call .reverse

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes