LearnFrenzy
Sign in Take a free test

std::string · std::string MCQ · Question 16

Can a std::string be modified in place? Answer, explanation & discussion

  • MCQ
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links