ProgrammingVery hardProduct-company placement · C++

Which line fails to compile?

Which line fails to compile?
string a = "a" + "b";            // L1
string b = string("a") + "b";    // L2
string c = "a" + string("b");    // L3
string d = "a" "b";              // L4
  1. A. L1 and L3
  2. B. L1 only
  3. C. L1 and L4
  4. D. L4 only

Correct answer: B. L1 only

"a" + "b" tries to add two const char* pointers, which is not allowed. As soon as one operand is a std::string, its operator+ is used (L2, L3). Adjacent string literals (L4) are joined by the compiler.

More OOP with C++ and Java questions

Exams that ask this topic