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));
}- A. 11
- B. 10
- C. 01
- 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
- What is printed? cpp struct A { A(int x) { cout << x; } A() { cout << 0; } }; struct B : virtual A { B() : A(1) { cout << "B"; } }; struct…
- What is printed? cpp void f(long) { cout << "L"; } void f(double) { cout << "D"; } int main() { f(5L); f(2.5f); f(5); }
- What is printed? cpp struct B { int b; B(int x) : b(x) { } }; struct D : B { int d; D(int x, int y) : B(x), d(y) { } }; int main() { D…
- How many of these statements about C++ dynamic memory are correct? (i) Memory obtained with new[] must be released with delete[]. (ii)…
- What happens when this is compiled? cpp struct K { const int c; int& r; K(int& x) { c = 1; r = x; } };
- Which line fails to compile? cpp struct A { A(int) { } }; struct B { B(A) { } }; void f(B) { } int main() { f(A(1)); // L1 f(1); // L2 }