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));
}- A. 11
- B. 01
- C. 10
- 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
- What is printed (C++17)? cpp int main() { int x = 0; auto f = [x]() mutable { return ++x; }; f(); auto g = f; f(); cout << f() << g() <<…
- What is printed? cpp struct B { }; struct D : B { }; void f(B&) { cout << "B"; } void f(D&) { cout << "D"; } int main() { D d; B& b = d…
- What is printed? cpp struct T { int id; T(int i) : id(i) { cout << "+" << id; } ~T() { cout << "-" << id; } }; T g(1); void f() { static T…
- What is printed? cpp int main() { int a = 1, b = 2; int& r = a; r = b; r = 5; cout << a << b; }
- What is printed? cpp void g(int&) { cout << "L"; } void g(int&&) { cout << "R"; } template <class T> void fwd(T&& x) { g(x)…
- Which line fails to compile? cpp struct B { void hi() { } }; class D1 : B { }; // note: class struct D2 : B { }; // note: struct int…