What will be the output of the following C++ code? #include<iostream>…

2023

What will be the output of the following C++ code? #include<iostream> #include<string> using namespace std; int main(int argc, char *argv[]) { char s1[6] = "Hello"; char s2[6] = "World"; char s3[12] = s1 + " " + s2; cout << s3; return 0; }

Answer: C. ErrorThe given C++ code will result in a compilation error. In C++, the + operator cannot be used directly with char arrays (C-style strings) for concatenation.…

  1. A.

    Hello

  2. B.

    World

  3. C.

    Error

  4. D.

    More than one of the above

  5. E.

    None of the above

Attempted by 407 students.

Show answer & explanation

Correct answer: C

The given C++ code will result in a compilation error. In C++, the + operator cannot be used directly with char arrays (C-style strings) for concatenation. The expression s1 + " " + s2 is invalid because the + operator is not defined for char arrays. Instead, string concatenation in C++ requires the use of std::string or functions like strcat(). Since the code attempts to use the + operator on char arrays, it will not compile. Therefore, the correct answer is 'Error / त्रुटि'.

Explore the full course: Bpsc

Loading lesson…