Question 16
Back to Exercise
Given std::string s = "cat";, what does s[0] = 'b'; do?
- A Compile error – strings are immutable
- B Changes s to "bat" Correct
- C Creates a new string
- D Undefined behaviour
Correct Answer: Option B
Unlike Java or Python strings, std::string is mutable. (Modifying a string literal itself would be undefined behaviour.)
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!