Bird
0
0

Which expression extracts the substring 'cat' from str = "concatenate"?

easy📝 Conceptual Q2 of 15
Ruby - String Operations
Which expression extracts the substring 'cat' from str = "concatenate"?
Astr[-6, 3]
Bstr[3, 3]
Cstr[2..4]
Dstr[4, 3]
Step-by-Step Solution
Solution:
  1. Step 1: Locate 'cat' in 'concatenate'

    The substring 'cat' starts at index 3 (0-based): c(0)o(1)n(2)c(3)a(4)t(5)...
  2. Step 2: Use slicing with start index and length

    str[3, 3] extracts 3 characters starting at index 3, which is 'cat'.
  3. Final Answer:

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

    Start index + length slicing = correct substring [OK]
Quick Trick: Start index + length extracts substring [OK]
Common Mistakes:
  • Off-by-one error in start index
  • Using range that includes wrong characters
  • Confusing negative index meaning

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes