ProgrammingHardProduct-company placement · C++

What is printed?

What is printed?
struct E { };
int main() {
    try { throw new E(); }
    catch (E&)  { cout << "ref"; }
    catch (E*)  { cout << "ptr"; }
}
  1. A. ptr
  2. B. ref
  3. C. refptr
  4. D. Compile-time error

Correct answer: A. ptr

throw new E() throws a POINTER (type E*), which only a handler for E* (or void*, or catch-all) can catch; E& expects an E object. The object is also leaked unless the handler deletes it.

More OOP with C++ and Java questions

Exams that ask this topic