0
0
Rubyprogramming~20 mins

String slicing and indexing in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ruby String Slicing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of negative index slicing
What is the output of this Ruby code?
Ruby
str = "programming"
puts str[-4, 3]
A"amm"
B"min"
C"mmi"
D"ing"
Attempts:
2 left
💡 Hint
Remember that negative index counts from the end, and the second number is length.
Predict Output
intermediate
2:00remaining
Output of range slicing
What will this Ruby code print?
Ruby
text = "hello world"
puts text[2..5]
A"llo "
B"llo w"
C"lo w"
D"ello"
Attempts:
2 left
💡 Hint
Range includes both start and end indices.
🔧 Debug
advanced
2:00remaining
Identify the error in slicing
What error does this Ruby code raise?
Ruby
word = "example"
puts word[3, -2]
Anil output (no error)
BArgumentError
CTypeError
DIndexError
Attempts:
2 left
💡 Hint
Negative length in slice returns nil instead of error.
Predict Output
advanced
2:00remaining
Output of slice with range and negative indices
What is the output of this Ruby code?
Ruby
s = "abcdefg"
puts s[-5..-3]
A"cdef"
B"cde"
C"def"
D"bcd"
Attempts:
2 left
💡 Hint
Negative indices count from the end, inclusive range.
🧠 Conceptual
expert
2:00remaining
Number of characters extracted by slice
How many characters does this Ruby slice extract?
Ruby
str = "university"
result = str[1, 4]
A6
B5
C3
D4
Attempts:
2 left
💡 Hint
The second argument is the length of the slice.