ProgrammingHardInfosys · GATE CS
What is printed? (C++, assume <iostream> and using namespace std)
What is printed? (C++, assume <iostream> and using namespace std)
struct B {
B() { f(); }
virtual void f() { cout << "B"; }
};
struct D : B { void f() override { cout << "D"; } };
int main() { D d; }- A. D
- B. B
- C. BD
- D. DB
Correct answer: B. B
While B's constructor runs, the object is only a B, so the virtual call resolves to B::f.
More OOP with C++ and Java questions
- A Java method has this structure. What happens when it is compiled? java try { reader.read(); // throws IOException } catch (Exception e)…
- What is printed? (C++, assume <iostream> and using namespace std) cpp struct B { virtual void f() = 0; }; void B::f() { cout << "B"; }…
- What is printed? java String s = "a"; s.concat("b"); s += "c"; System.out.print(s);
- What is printed? (C++, assume <iostream> and using namespace std) cpp struct B { void f(int) { cout << "Bi"; } }; struct D : B { void…
- What is printed? java class A { static { System.out.print("S"); } { System.out.print("I"); } A() { System.out.print("C"); } } // in main…
- What is printed? (C++, assume <iostream> and using namespace std) cpp class B { public: void call() { f(); } private: virtual void f() {…