🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Strings in C++: all 10 questions

Quiz: Strings in C++ · Question 9 of 10

What does this print? (code)

What does this print?

#include <iostream>
#include <string>
using namespace std;
int main() {
    string s = "C++";
    s.insert(0, "Learn ");
    s.erase(s.size() - 1);
    cout << s << s.size();
    return 0;
}
  1. A.Learn C++9
  2. B.Learn C+8
  3. C.Learn C7
  4. D.Learn C++8
Answer & explanation

Correct answer: Learn C+8

After insert the string is "Learn C++" (9 characters). erase(8) removes everything from index 8, the last '+', leaving "Learn C+" with size 8.

Can you get the rest right without peeking?

Take the timed Quiz: Strings in C++ quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →