ProgrammingHardProduct-company placement · C++
What is printed?
What is printed?
struct G { G() { cout << "G"; } ~G() { cout << "g"; } };
G g1;
int main() {
cout << "M";
static G s;
cout << "m";
}- A. MGGmgg
- B. GMGgmg
- C. GMGm
- D. GMGmgg
Correct answer: D. GMGmgg
The global g1 is constructed before main. The static local s is constructed when reached. At exit, objects with static storage are destroyed in reverse order of construction (s, then g1), so two g's follow m.
More OOP with C++ and Java questions
- Which lines fail to compile? cpp struct P { P(int) { } }; int main() { P a[3]; // L1 P b[2] = {1, 2}; // L2 vector<P> v(3); // L3…
- Which line fails to compile? cpp struct B { virtual B clone() { return new B; } virtual int id() { return 1; } }; struct D : B { D clone()…
- What is printed when compiled as C++17? cpp struct A { A() { cout << "D"; } A(const A&) { cout << "C"; } }; int main() { A a = A(A()); }
- What is printed? cpp struct B { B& operator=(int) { cout << "B"; return this; } }; struct D : B { }; int main() { D d; d = 5; }
- What is printed? cpp template <typename T> struct Box { static int count; Box() { count++; } }; template <typename T> int Box<T>::count =…
- What is printed? cpp struct N { int v; N(int x = 7) : v(x) { } }; void show(N n) { cout << n.v; } int main() { N a; N b = 3; show(4)…