What will be the output of the following C++ code after appending a character sequence?

#include <iostream>
#include <string>
using namespace std;
int main() {
    string s = "abc";
    s += "d";
    cout << s.length();
    return 0;
}
                        

Options

  • A. 3
  • B. 4
  • C. 5
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

B. 4

Detailed Explanation

Correct Answer: Option B — 4

Explanation: The += operator appends d to the string abc, producing abcd. The length() member function returns the number of characters, which is 4.