ProgrammingVery hardProduct-company placement · C++
What is printed?
What is printed?
int main() {
const string s = "x";
string t = std::move(s);
cout << s << t;
}- A. x
- B. xx
- C. Compile-time error: cannot move a const object
- D. t only; s is now empty
Correct answer: B. xx
std::move(s) yields a const string&&, which cannot bind to the move constructor's string&& parameter, so the copy constructor is used. s is left untouched: moving from a const object silently copies.
More OOP with C++ and Java questions
- Which line fails to compile? cpp int main() { int x = 1; const int& r = x; auto a = r; // L1 a++; // L2 decltype(r) b = x; // L3 b++; //…
- Which line fails to compile? cpp struct A { virtual ~A() { } }; struct B { virtual ~B() { } }; struct C : A, B { }; int main() { A a = new…
- What does the C++ standard say about this code? cpp const int x = 5; int p = constcast<int>(&x); p = 6;
- For the call f(g(), h()) in C++17, which statement is correct?
- What happens when this is compiled? cpp struct P { int a; private: int b; }; int main() { P p{1, 2}; }
- Which line fails to compile? cpp struct A { int x = 1; }; struct B : protected A { }; struct C : B { int get() { return x; } }; int main()…