What will be the output of the following C++ code?

#include <iostream>
using namespace std;
int main() {
    char s[] = "CAT";
    s[1] = 'X';
    cout << s;
    return 0;
}
                        

Options

  • A. CAT
  • B. CXT
  • C. AXT
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

B. CXT

Detailed Explanation

Correct Answer: Option B — CXT

Explanation: Array indexing starts at zero. s[1] refers to the second character, A. Replacing it with X changes CAT to CXT. The character array is modifiable because it is a non-const array.