ProgrammingVery hardProduct-company placement · C++
What is printed?
What is printed?
void f(int&) { cout << "L"; }
void f(int&&) { cout << "R"; }
int main() {
int a = 1;
int&& r = 5;
f(a); f(5); f(std::move(a)); f(r);
}- A. LRRR
- B. LRLL
- C. LLRR
- D. LRRL
Correct answer: D. LRRL
Lvalues bind to int&, rvalues (literals, std::move results) to int&&. A NAMED rvalue reference such as r is itself an lvalue expression, so f(r) calls the int& overload.
More OOP with C++ and Java questions
- What is printed? cpp struct A { A() { cout << "D"; } A(const A&) { cout << "C"; } A(A&&) noexcept { cout << "M"; } }; int main() {…
- What is printed? cpp int main() { uniqueptr<int> p = makeunique<int>(7); uniqueptr<int> q = std::move(p); cout << (p == nullptr) << q; }
- 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…