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; }
}- A. S
- B. Soops
- C. oops
- 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
- Which line fails to compile? cpp struct A { A(int) { } }; struct B { explicit B(int) { } }; A f() { return 5; } // L1 B g() { return 5; }…
- Which line fails to compile? cpp int main() { int a[3] = {1, 2, 3}, b[3]; b = a; // L1 array<int, 3> c{1, 2, 3}, d; d = c; // L2 }
- What is printed? cpp template <sizet N> sizet len(int (&)[N]) { return N; } int main() { int a[7]; cout << len(a); // L1 int p = a; cout…
- What is printed? cpp struct S { void f() & { cout << "L"; } void f() && { cout << "R"; } }; int main() { S s; s.f(); S().f()…
- What is printed? cpp int main() { int x = 0; auto f = [x]() mutable { return ++x; }; f(); f(); cout << f() << x; }
- What is printed? cpp int main() { std::function<int()> f; try { cout << f(); } catch (const badfunctioncall&) { cout << "E"; } }