ProgrammingHardProduct-company placement · C++

What is printed?

What is printed?
int main() {
    try { throw "oops"; }
    catch (string& s)      { cout << "S"; }
    catch (const char* s)  { cout << s; }
}
  1. A. S
  2. B. Soops
  3. C. oops
  4. D. Nothing; std::terminate is called

Correct answer: C. oops

A string literal is thrown as a const char*. Handlers do not apply user-defined conversions, so catch (string&) does not match, and the const char* handler prints it.

More OOP with C++ and Java questions

Exams that ask this topic