LearnFrenzy
Sign in Take a free test

std::string · Predict the Output · Question 5

insert and erase on a string Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 5 Back to Exercise

What is the output of the following C++ program on a 64-bit system?

main.cpp
#include <iostream>
#include <string>

int main() {
    std::string s = "abcdef";
    s.insert(3, "XYZ");
    s.erase(1, 2);
    s.erase(s.size() - 2);
    std::cout << s << '\n';
    return 0;
}
  • A aXYZd Correct
  • B aXYZde
  • C abXYZd
  • D aXYde
Correct Answer: Option A

"abcXYZdef" → erase 2 chars at 1 → "aXYZdef" → erase from size − 2 to the end → "aXYZd".

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