ProgrammingVery hardInfosys DSE · Wipro Elite
What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std)
What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std)
struct T {
T() { cout << "d"; }
T(const T&) { cout << "c"; }
T& operator=(const T&) { cout << "a"; return *this; }
};
int main() { T x; T y = x; T z; z = y; T w(z); }- A. dcdaa
- B. dcdac
- C. ddcac
- D. dcdcc
Correct answer: B. dcdac
T y = x and T w(z) are copy-constructions (c); z = y is copy assignment (a); x and z use the default constructor (d): d c d a c.
More OOP with C++ and Java questions
- What is printed? (no move constructor is declared) (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct T {…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct C { static int n; int id; C() : id(++n)…
- What is the result? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct A { void f(int) { cout << "Ai"; }…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct A { virtual int g(int x = 10) { return…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct V { V(int x) { cout << "V" << x; } V()…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct A { A() { cout << "A"; } }; struct B {…