ProgrammingVery hardWipro Elite · TCS NQT Prime
What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std)
What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std)
struct A { A() { f(); } virtual void f() { cout << "A"; } };
struct B : A { B() { f(); } void f() override { cout << "B"; } };
int main() { B b; }- A. BB
- B. AA
- C. BA
- D. AB
Correct answer: D. AB
While A's constructor runs the object is only an A, so the virtual call resolves to A::f; B's constructor then calls B::f.
More OOP with C++ and Java questions
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct A { virtual ~A() { f(); } virtual void…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct T { T() { cout << "d"; } T(const T&) {…
- What is printed? (no move constructor is declared) (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct T {…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct C { static int n; int id; C() : id(++n)…
- What is the result? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct A { void f(int) { cout << "Ai"; }…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct A { virtual int g(int x = 10) { return…