Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q5 of 15
Ruby - Arrays
What will be the output of this Ruby code?
arr = [5, 10, 15, 20, 25]
slice = arr[2...5]
puts slice.inspect
A[15, 20, 25]
B[15, 20]
C[10, 15, 20]
D[10, 15, 20, 25]
Step-by-Step Solution
Solution:
  1. Step 1: Understand the range 2...5

    The three-dot range excludes the end index 5, so it includes indices 2, 3, and 4.
  2. Step 2: Extract elements at indices 2, 3, and 4

    These elements are 15, 20, and 25 respectively.
  3. Final Answer:

    [15, 20, 25] -> Option A
  4. Quick Check:

    Three-dot range excludes end index, so arr[2...5] = [15, 20, 25] [OK]
Quick Trick: Three-dot range excludes the last index [OK]
Common Mistakes:
  • Expecting only two elements due to ...
  • Using .. which includes index 5 (out of range)
  • Confusing indices with values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes