Bird
0
0

Which of these is the correct syntax to get the substring 'gram' from str = "programming"?

easy📝 Syntax Q3 of 15
Ruby - String Operations
Which of these is the correct syntax to get the substring 'gram' from str = "programming"?
Astr[3, 4]
Bstr[3..5]
Cstr[4..7]
Dstr[4, 3]
Step-by-Step Solution
Solution:
  1. Step 1: Identify substring 'gram' position

    'gram' starts at index 3: p(0)r(1)o(2)g(3)r(4)a(5)m(6)...
  2. Step 2: Use start index and length syntax

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

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

    Start index + length slicing = correct substring [OK]
Quick Trick: Use str[start, length] for substring extraction [OK]
Common Mistakes:
  • Using range with wrong end index
  • Confusing length with end index
  • Off-by-one errors in slicing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes