ProgrammingHardProduct-company placement · C++
What is printed?
What is printed?
int main() {
unique_ptr<int> p = make_unique<int>(7);
unique_ptr<int> q = std::move(p);
cout << (p == nullptr) << *q;
}- A. 17
- B. 07
- C. Compile-time error: unique_ptr cannot be moved
- D. 1 then undefined behaviour
Correct answer: A. 17
unique_ptr cannot be copied but can be moved; moving transfers ownership and the source is guaranteed to become null. q now owns the int 7.
More OOP with C++ and Java questions
- What is printed? cpp int main() { auto a = makeshared<int>(5); auto b = a; { auto c = a; cout << a.usecount(); } cout << a.usecount()…
- What is printed? cpp int main() { weakptr<int> w; { auto s = makeshared<int>(1); w = s; cout << w.expired(); } cout << w.expired(); }
- What is printed? cpp struct P { int a, b; P() : P(1) { cout << "x"; } P(int a) : P(a, 2) { cout << "y"; } P(int a, int b) : a(a), b(b) {…
- Which lines fail to compile? cpp struct F { explicit operator bool() const { return true; } }; int main() { F f; if (f) cout << "Y"; // L1…
- What is printed? cpp struct M { operator int() const { return 7; } }; int main() { M m; cout << m + 1; }
- What is printed? cpp // C++17 struct Counter { int n = 0; int operator()() { return ++n; } }; int main() { Counter c; c(); c(); Counter d…