ProgrammingVery hardProduct-company placement · C++

What is printed?

What is printed?
struct A { };          struct B : A { };
struct P { virtual ~P() { } };  struct Q : P { };
int main() {
    B b; Q q;
    A& ra = b; P& rp = q;
    cout << (typeid(ra) == typeid(B)) << (typeid(rp) == typeid(Q));
}
  1. A. 11
  2. B. 10
  3. C. 01
  4. D. 00

Correct answer: C. 01

typeid on a reference gives the DYNAMIC type only for polymorphic classes (those with a virtual function). A has none, so typeid(ra) is just typeid(A); P has a virtual destructor, so typeid(rp) is Q.

More OOP with C++ and Java questions

Exams that ask this topic