ProgrammingHardGATE 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 P { P(const char* s) { cout << s; } };
struct Q {
P y{"y"};
P x{"x"};
Q() : x("X"), y("Y") { }
};
int main() { Q q; }- A. YX
- B. xyXY
- C. XY
- D. yxYX
Correct answer: A. YX
Members are initialised in declaration order (y, then x), regardless of the order in the mem-initialiser list; the mem-initialisers replace the default member initialisers, so 'Y' and 'X' (not 'y' or 'x') are printed.
More OOP with C++ and Java questions
- 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…
- What happens when this C++ program is compiled? cpp struct B { int x = 5; }; class D : B { }; int main() { D d; cout << d.x; }