Bird
0
0

What will be printed by this Ruby code?

medium📝 Predict Output Q4 of 15
Ruby - String Operations
What will be printed by this Ruby code?
text = "world"
text[2] = 'r'
puts text
Aworrd
Bword
Cworld
Dworod
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the code

    The string "world" has its character at index 2 replaced with 'r'.
  2. Step 2: Identify index 2 character

    Indexing starts at 0: w(0), o(1), r(2), l(3), d(4). The character at index 2 is 'r'. Replacing 'r' with 'r' keeps the string unchanged.
  3. Step 3: Confirm the output

    Since the character is replaced with the same letter, the string remains "world".
  4. Final Answer:

    world -> Option C
  5. Quick Check:

    Replacing a character with the same letter keeps string unchanged [OK]
Quick Trick: Replacing with same char keeps string unchanged [OK]
Common Mistakes:
  • Miscounting string indices
  • Assuming replacement deletes characters
  • Confusing index positions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes