LearnFrenzy
Sign in Take a free test

Move Semantics · Exercise 3 of 4

Move Semantics Find the Error

  • 10 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Each C++ program contains a move-semantics mistake that the compiler rejects. Choose the option that names the real problem.

Question 1 Easy

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>
#include <memory>

struct Account {
    std::unique_ptr<int> balance = std::make_unique<int>(500);
};

int main() {
    Account a;
    Account b = a;
    std::cout << *b.balance << '\n';
    return 0;
}
A make_unique needs new
B Account has a unique_ptr member, so its copy constructor is implicitly deleted; move it (Account b = std::move(a)) or write a deep copy
C balance must be static
D b must be a reference
Question page & discussion
Question 2 Easy

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>
#include <string>

void store(std::string&& s) { std::cout << "stored " << s << '\n'; }

int main() {
    std::string name = "Asha";
    store(name);
    return 0;
}
A store must return a value
B name is an lvalue and cannot bind to std::string&&; call store(std::move(name)) or add a const std::string& overload
C Strings cannot be moved
D store must be a template
Question page & discussion
Question 3 Medium

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>
#include <string>
#include <utility>

struct Note {
    std::string text;
    Note(std::string t) : text(std::move(t)) {}
    Note(Note&& o) noexcept : text(std::move(o.text)) {}
};

int main() {
    Note a("hello");
    Note b = a;
    std::cout << b.text << '\n';
    return 0;
}
A text must be public
B Declaring a move constructor implicitly deletes the copy constructor, so Note b = a fails; add Note(const Note&) = default;
C The move constructor must be const
D std::move is invalid here
Question page & discussion
Question 4 Medium

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>
#include <string>
#include <utility>

struct User {
    const std::string id;
    std::string name;
};

int main() {
    User a{"u1", "Asha"}, b{"u2", "Ravi"};
    a = std::move(b);
    std::cout << a.name << '\n';
    return 0;
}
A name must be const
B A const data member cannot be assigned, so User's copy and move assignment operators are implicitly deleted; drop the const or avoid assignment
C std::move cannot be used on structs
D User needs a constructor
Question page & discussion
Question 5 Hard

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>
#include <string>
#include <utility>

class Report {
    std::string text_ = "draft";
public:
    std::string release() && { return std::move(text_); }   // only for rvalues
};

int main() {
    Report r;
    std::string t = r.release();
    std::cout << t << '\n';
    return 0;
}
A release must be static
B release() is &&-qualified, so it can only be called on an rvalue; write std::move(r).release() to show r is being consumed
C text_ must be public
D Strings cannot be returned
Question page & discussion
Question 6 Medium

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>
#include <string>
#include <utility>
#include <vector>

std::vector<std::string> items;

template <typename T>
void add(T&& x) { items.push_back(std::forward(x)); }

int main() {
    add(std::string("pen"));
    std::cout << items.size() << '\n';
    return 0;
}
A push_back cannot take rvalues
B std::forward needs its template argument explicitly – std::forward<T>(x) – because T cannot be deduced from x
C T&& is not allowed
D items must be const
Question page & discussion
Question 7 Medium

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>
#include <thread>
#include <vector>

int main() {
    std::thread t([] { std::cout << "work\n"; });
    std::vector<std::thread> pool;
    pool.push_back(t);
    for (auto& th : pool) th.join();
    return 0;
}
A Lambdas cannot be threads
B std::thread is move-only (one thread of execution cannot have two owners); use pool.push_back(std::move(t))
C join must be called first
D vectors cannot hold threads
Question page & discussion
Question 8 Hard

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

struct Pinned {
    int v = 0;
    Pinned() = default;
    Pinned(const Pinned&) = delete;
    Pinned(Pinned&&) = delete;
};

Pinned make() {
    Pinned p;
    p.v = 42;
    return p;
}

int main() {
    Pinned x = make();
    std::cout << x.v << '\n';
    return 0;
}
A v must be private
B Returning a named local needs an accessible copy or move constructor (NRVO is optional), and both are deleted; only a prvalue return such as return Pinned{}; is guaranteed to need neither
C make must be inline
D Pinned needs a destructor
Question page & discussion
Question 9 Medium

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>
#include <utility>

struct Token {
    int id = 0;
    void operator=(Token&& o) noexcept { id = std::exchange(o.id, 0); }
};

int main() {
    Token a, b, c;
    c.id = 7;
    a = b = std::move(c);
    std::cout << a.id << '\n';
    return 0;
}
A std::exchange needs <algorithm>
B operator= returns void, so b = std::move(c) has no value to assign to a; return Token& (*this) as assignment operators conventionally do
C Tokens cannot be moved
D id must be const
Question page & discussion
Question 10 Hard

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <functional>
#include <iostream>
#include <memory>

int main() {
    auto p = std::make_unique<int>(5);
    auto task = [q = std::move(p)] { return *q; };
    std::function<int()> f = task;
    std::cout << f() << '\n';
    return 0;
}
A Init captures cannot move
B The lambda owns a unique_ptr, so it is move-only, but std::function requires a copyable callable; use std::move_only_function (C++23) or call task directly
C f must be auto
D *q is invalid
Question page & discussion
Up next · Exercise 4Move Semantics Programs10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links