Bird
0
0

What will this Ruby code print?

medium📝 Predict Output Q5 of 15
Ruby - String Operations
What will this Ruby code print?
str = "ruby"
str[2..3] = "on"
puts str
Aruony
BError: invalid range assignment
Cruonb
Druon
Step-by-Step Solution
Solution:
  1. Step 1: Understand range assignment in strings

    str[2..3] selects characters at index 2 and 3 ('b' and 'y').
  2. Step 2: Replace selected range with "on"

    Replacing 'by' with 'on' changes "ruby" to "ruon".
  3. Final Answer:

    ruon -> Option D
  4. Quick Check:

    Range replacement output = B [OK]
Quick Trick: Use str[start..end] = "new" to replace substring. [OK]
Common Mistakes:
  • Expecting original string unchanged.
  • Confusing index ranges or off-by-one errors.
  • Thinking it appends instead of replaces.

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes