ProgrammingVery hardGATE CS · TCS Digital · Advanced
What is printed? (C++17, assume <iostream> and using namespace std)
What is printed? (C++17, assume <iostream> and using namespace std)
struct V { V() { cout << "V"; } };
struct A : virtual V { A() { cout << "A"; } };
struct B : virtual V { B() { cout << "B"; } };
struct C : A, B {
V m;
C() { cout << "C"; }
};
int main() { C c; }- A. VABC
- B. VAVBVC
- C. VABCV
- D. VABVC
Correct answer: D. VABVC
The virtual base V is built first and only once (by the most derived class), then A and B in declaration order, then C's own member m (another V), and finally C's body.
More OOP with C++ and Java questions
- What is printed? (C++17, assume <iostream> and using namespace std) cpp struct C { int v = 0; C& operator++() { ++v; return this; } C…
- What is printed? (C++17, assume <iostream> and using namespace std) cpp struct T { T() { cout << "c"; } ~T() { cout << "d"; } }; int…
- What is printed? java class A { A() { init(); } void init() { System.out.print("A"); } } class B extends A { int v = 5; B() { super()…
- What is printed? java Long l = 127L; Integer i = 127; System.out.print(l.equals(i) + " " + l.equals(127L) + " " + (l == 127) + " " +…
- What is printed? java static void f(long x) { System.out.print("L"); } static void f(Integer x) { System.out.print("I"); } static void…
- What is printed? java System.out.print(true ? 1 : 2.0); System.out.print(true ? 'a' : 0); int i = 0; System.out.print(true ? 'a' : i);