ProgrammingVery hardProduct-company placement · C++

What is printed?

What is printed?
struct B { virtual ~B() { } };
int main() {
    B* p = nullptr;
    try { cout << typeid(*p).name(); }
    catch (const bad_typeid&) { cout << "T"; }
}
  1. A. Undefined behaviour: a null dereference
  2. B. An empty line
  3. C. B
  4. D. T

Correct answer: D. T

For an expression of polymorphic type, typeid(*p) with a null pointer p is defined to throw std::bad_typeid instead of dereferencing it.

More OOP with C++ and Java questions

Exams that ask this topic