Bird
0
0

Which of the following is the correct way to get the substring 'ell' from str = "hello" using slicing?

easy📝 Syntax Q12 of 15
Ruby - String Operations
Which of the following is the correct way to get the substring 'ell' from str = "hello" using slicing?
Astr[1,3]
Bstr[0,3]
Cstr[2..4]
Dstr[1...3]
Step-by-Step Solution
Solution:
  1. Step 1: Understand slicing syntax

    In Ruby, str[start, length] extracts a substring starting at start with length characters.
  2. Step 2: Extract 'ell' from 'hello'

    'ell' starts at index 1 and has length 3, so str[1,3] returns 'ell'.
  3. Final Answer:

    str[1,3] -> Option A
  4. Quick Check:

    Substring from index 1, length 3 = 'ell' [OK]
Quick Trick: Use [start, length] to slice substrings easily [OK]
Common Mistakes:
  • Using range with wrong end index
  • Confusing start and length parameters
  • Using negative indices incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes