Bird
0
0

Identify the error in this Ruby code:

medium📝 Debug Q6 of 15
Ruby - String Operations
Identify the error in this Ruby code:
str = "code"
str[5] = 'r'
puts str
AIndex 5 is out of bounds for the string
BStrings cannot be modified using index assignment
CThe character 'r' is invalid in strings
DThe string must be frozen before modification
Step-by-Step Solution
Solution:
  1. Step 1: Check string length

    The string "code" has length 4, with indices 0 to 3.
  2. Step 2: Analyze index used

    Index 5 is beyond the string's length, causing an error when assigning.
  3. Step 3: Confirm error cause

    Assigning to an out-of-bounds index raises an IndexError.
  4. Final Answer:

    Index 5 is out of bounds for the string -> Option A
  5. Quick Check:

    Index must be within string length [OK]
Quick Trick: Index must be valid within string length [OK]
Common Mistakes:
MISTAKES
  • Using invalid indices
  • Thinking strings cannot be modified by index
  • Assuming characters must be frozen

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes