ProgrammingVery hardMicrosoft/Google SDE MCQ · GATE CS

What is printed?

What is printed?
struct B { };            // no virtual functions
struct D : B { };
struct VB { virtual ~VB() { } };
struct VD : VB { };
int main() {
    B* p = new D;
    VB* q = new VD;
    cout << (typeid(*p) == typeid(D)) << (typeid(*q) == typeid(VD));
}
  1. A. 11
  2. B. 01
  3. C. 10
  4. D. 00

Correct answer: B. 01

typeid looks at the dynamic type only for polymorphic classes. B has no virtual functions, so typeid(*p) is the static type B (0). VB is polymorphic, so typeid(*q) is VD (1).

More OOP with C++ and Java questions

Exams that ask this topic