Bird
0
0

What is the output of this Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - String Operations
What is the output of this Ruby code?
str = "cat"
str[1] = 'o'
puts str
Acat
Bc t
Ccot
DError
Step-by-Step Solution
Solution:
  1. Step 1: Understand string indexing and assignment

    str[1] = 'o' changes the character at index 1 (second character) from 'a' to 'o'.
  2. Step 2: Apply the change and print

    Original string "cat" becomes "cot" after the change, so puts prints "cot".
  3. Final Answer:

    cot -> Option C
  4. Quick Check:

    Changing index 1 to 'o' = cot [OK]
Quick Trick: Index 1 means second character in string [OK]
Common Mistakes:
  • Expecting original string unchanged
  • Confusing index positions starting at 1
  • Thinking it causes an error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes