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));
}- A. 110
- B. 111
- C. 101
- 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
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct G { const char n; G(const char s)…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct A { virtual void f() { cout << "A"; }…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp void f(bool) { cout << "b"; } void f(string) {…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp template <class T> void f(T) { cout << "T"; }…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct A { static int n; static void p() {…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp int main() { vector<int> a(3, 1), b{3, 1}…