0
0
Rubyprogramming~20 mins

Array slicing and ranges in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ruby Array Slicing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Ruby code using range slicing?
Consider the following Ruby code snippet. What will be printed?
Ruby
arr = [10, 20, 30, 40, 50]
puts arr[1..3].inspect
A[20, 30, 40, 50]
B[20, 30, 40]
C[10, 20, 30]
D[30, 40, 50]
Attempts:
2 left
💡 Hint
Remember that the range 1..3 includes elements at index 1, 2, and 3.
Predict Output
intermediate
2:00remaining
What does this code output when slicing with an exclusive range?
Look at this Ruby code. What will it print?
Ruby
arr = ['a', 'b', 'c', 'd', 'e']
puts arr[0...3].inspect
A["b", "c", "d"]
B["a", "b", "c", "d"]
C["a", "b", "c"]
D["a", "b"]
Attempts:
2 left
💡 Hint
The three dots in the range exclude the last index.
Predict Output
advanced
2:00remaining
What is the output of this code using negative indices in slicing?
Analyze this Ruby code and determine its output.
Ruby
arr = [1, 2, 3, 4, 5, 6]
puts arr[-4..-2].inspect
A[3, 4, 5]
B[4, 5, 6]
C[2, 3, 4]
D[3, 4]
Attempts:
2 left
💡 Hint
Negative indices count from the end: -1 is last element.
Predict Output
advanced
2:00remaining
What error does this Ruby code raise when slicing with an invalid range?
What happens when you run this Ruby code?
Ruby
arr = [7, 8, 9]
puts arr[5..7].inspect
AArgumentError
Bnil
CIndexError
D[]
Attempts:
2 left
💡 Hint
Check what Ruby returns when slicing beyond array length.
🧠 Conceptual
expert
2:00remaining
How many elements are in the resulting array after this slicing?
Given this Ruby code, how many elements does the resulting array have?
Ruby
arr = (1..10).to_a
result = arr[2..8]
puts result.size
A7
B6
C8
D9
Attempts:
2 left
💡 Hint
Remember that the range 2..8 includes both ends.