ProgrammingVery hardTCS NQT Digital · Accenture Advanced
What is printed? (no move constructor is declared) (C++17; assume <iostream>, <string>, <vector> and using namespace std)
What is printed? (no move constructor is declared) (C++17; assume <iostream>, <string>, <vector> and using namespace std)
struct T {
T() { cout << "d"; }
T(const T&) { cout << "c"; }
};
T g(T t) { return t; }
int main() { T x; T y = g(x); }- A. dc
- B. dccc
- C. dcc
- D. dd
Correct answer: C. dcc
x is default-built (d); the by-value parameter is copied (c); returning the parameter copies it into the result object, which is y itself in C++17 (c).
More OOP with C++ and Java questions
- 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 {…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct I { int v; I& operator++() { ++v…