ProgrammingModerateGATE CS · TCS NQT
What is printed? (C++, assume <iostream> and using namespace std)
What is printed? (C++, assume <iostream> and using namespace std)
struct A {
A() { cout << "c"; }
A(const A&) { cout << "k"; }
~A() { cout << "d"; }
};
void f(A a) { }
int main() { A x; f(x); }- A. ckdd
- B. cdd
- C. ckd
- D. cckdd
Correct answer: A. ckdd
x is built (c); passing it by value invokes the copy constructor (k); the parameter is destroyed at the end of f (d) and x at the end of main (d).
More OOP with C++ and Java questions
- What is printed? java class P { static String w() { return "P"; } } class Q extends P { static String w() { return "Q"; } } // in main: P…
- What is printed? (C++, assume <iostream> and using namespace std) cpp struct B { virtual void f(int x = 1) { cout << "B" << x; } }; struct…
- What is printed? (C++, assume <iostream> and using namespace std) cpp struct B { virtual void f() { cout << "B"; } }; struct D : B { void…
- What is printed? java class A { int x = 1; } class B extends A { int x = 2; } // in main: A a = new B(); System.out.print(a.x + " " + ((B)…
- What is printed? java class P { P() { init(); } void init() { } } class C extends P { int v = 5; void init() { System.out.print(v); } } //…
- What is printed? (C++, assume <iostream> and using namespace std) cpp struct M { M() { cout << "M"; } }; struct B { B() { cout << "B"; }…