ProgrammingVery hardInfosys DSE · Wipro Elite

What is printed? (also assume <typeinfo>) (C++17; assume <iostream>, <string>, <vector> and using namespace std)

What is printed? (also assume <typeinfo>) (C++17; assume <iostream>, <string>, <vector> and using namespace std)
struct A { virtual ~A() {} };
struct B : A {};
struct C : A {};
int main() {
    A* p = new B;
    cout << (dynamic_cast<C*>(p) == nullptr)
         << (dynamic_cast<B*>(p) != nullptr)
         << (typeid(*p) == typeid(B));
}
  1. A. 110
  2. B. 111
  3. C. 101
  4. D. 011

Correct answer: B. 111

The cross-cast to C* fails (null), the cast to B* succeeds, and typeid(*p) uses the dynamic type for a polymorphic class: all three true.

More OOP with C++ and Java questions

Exams that ask this topic