Bird
0
0

How can you reverse the substring from index 2 to 5 in str = "developer" and replace it back into the string?

hard📝 Application Q9 of 15
Ruby - String Operations
How can you reverse the substring from index 2 to 5 in str = "developer" and replace it back into the string?
Astr[2..5] = str[5..2].reverse
Bstr[2, 5] = str[2, 5].reverse
Cstr[2..5] = str[5..2]
Dstr[2..5] = str[2..5].reverse
Step-by-Step Solution
Solution:
  1. Step 1: Identify substring from index 2 to 5

    str[2..5] extracts characters at indices 2,3,4,5.
  2. Step 2: Reverse the substring and assign back

    str[2..5] = str[2..5].reverse replaces that part with its reverse.
  3. Final Answer:

    str[2..5] = str[2..5].reverse -> Option D
  4. Quick Check:

    Range slice + reverse + assignment = substring reversed in place [OK]
Quick Trick: Use range slice and assign reversed substring [OK]
Common Mistakes:
  • Using incorrect range order
  • Trying to reverse with invalid range
  • Assigning reversed substring without slicing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes