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 M {
const char* n;
M(const char* s) : n(s) { }
~M() { cout << n; }
};
struct B {
M m{"b"};
~B() { cout << "B"; }
};
struct D : B {
M x{"x"};
M y{"y"};
~D() { cout << "D"; }
};
int main() { D d; }- A. BbDyx
- B. DyxBb
- C. DxyBb
- D. DBbxy
Correct answer: B. DyxBb
Destruction runs in the reverse of construction: D's body, then D's members in reverse declaration order (y, x), then the base B (its body, then its member b).
More OOP with C++ and Java questions
- What is printed? (C++17, assume <iostream> and using namespace std) cpp struct P { P(const char s) { cout << s; } }; struct Q { P y{"y"}…
- What is printed? java String s1 = "hi"; String s2 = new String("hi"); System.out.print((s1 == s2) + " " + s1.equals(s2));
- What is printed? (C++, assume <iostream> and using namespace std) cpp struct C { static int n; C() { n++; } ~C() { n--; } }; int C::n = 0…
- Which statement about abstract classes in Java is correct?
- In C++, a Base pointer holds a Derived object created with new, and the Base destructor is NOT virtual. What happens on delete basePtr?
- What is printed? java static void f(int x) { System.out.print("int"); } static void f(long x) { System.out.print("long"); } static void…