LearnFrenzy
Sign in Take a free test

Move Semantics · Exercise 2 of 4

Move Semantics Predict the Output

  • 20 questions
  • Page 2 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Read each C++ program and choose what it prints. Every program was compiled and run on a 64-bit system, so the answers are the real output.

Question 11 Medium

What is the output of the following C++ program on a 64-bit system?

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

struct Box {
    std::string v;
    explicit Box(std::string s) : v(std::move(s)) {}
    Box(const Box& o) : v(o.v) { std::cout << "copy "; }
    Box(Box&& o) noexcept : v(std::move(o.v)) { std::cout << "move "; }
};

Box upper(Box b) {
    for (char& c : b.v) c = static_cast<char>(c - 32);
    return b;                        // a parameter: implicitly moved, never copied
}

int main() {
    Box a("cpp");
    Box r = upper(a);
    std::cout << r.v << ' ' << a.v << '\n';
    return 0;
}
A copy move CPP cpp
B copy copy CPP cpp
C move move CPP
D copy CPP cpp
Question page & discussion
Question 12 Medium

What is the output of the following C++ program on a 64-bit system?

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

struct S {
    int v;
    explicit S(int x) : v(x) {}
    S(S&& o) noexcept : v(o.v) { std::cout << "mc "; }
    S& operator=(S&& o) noexcept { v = o.v; std::cout << "ma "; return *this; }
};

int main() {
    S a(1), b(2);
    std::swap(a, b);
    std::cout << "| " << a.v << b.v << '\n';
    return 0;
}
A mc ma ma | 21
B ma ma ma | 21
C mc mc mc | 21
D | 21
Question page & discussion
Question 13 Easy

What is the output of the following C++ program on a 64-bit system?

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

int main() {
    std::vector<int> v{1, 2, 3};
    std::map<std::string, int> m{{"a", 1}, {"b", 2}};
    auto v2 = std::move(v);
    auto m2 = std::move(m);
    v = {9};                                 // reuse after assigning
    std::cout << v2.size() << m2.size() << v.size() << m.empty() << '\n';
    return 0;
}
A 3211
B 3201
C 3210
D 0011
Question page & discussion
Question 14 Medium

What is the output of the following C++ program on a 64-bit system?

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

struct Job {
    std::unique_ptr<int> data;
    explicit Job(int v) : data(std::make_unique<int>(v)) {}
};

int main() {
    std::vector<Job> jobs;
    Job j(5);
    jobs.push_back(std::move(j));            // copy would not compile
    jobs.emplace_back(8);
    auto taken = std::move(jobs.front());
    std::cout << jobs.size() << ' ' << (jobs[0].data == nullptr) << ' ' << *taken.data << ' ' << *jobs[1].data << '\n';
    return 0;
}
A 2 1 5 8
B 1 0 5 8
C 2 0 5 8
D 2 1 8 5
Question page & discussion
Question 15 Hard

What is the output of the following C++ program on a 64-bit system?

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

class Builder {
    std::string s_;
public:
    Builder& add(const std::string& x) & { s_ += x; return *this; }
    std::string build() const & { std::cout << "[copy] "; return s_; }      // object stays usable
    std::string build() && { std::cout << "[steal] "; return std::move(s_); } // object is a temporary
};

int main() {
    Builder b;
    b.add("ab").add("cd");
    std::string x = b.build();
    std::string y = std::move(b).build();
    std::cout << x << ' ' << y << '\n';
    return 0;
}
A [copy] [steal] abcd abcd
B [copy] [copy] abcd abcd
C [steal] [steal] abcd
D [copy] [steal] abcd
Question page & discussion
Question 16 Hard

What is the output of the following C++ program on a 64-bit system?

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

struct T {
    static inline int c = 0, m = 0, d = 0;
    T() = default;
    T(const T&) { ++c; }
    T(T&&) noexcept { ++m; }
    ~T() { ++d; }
};

T make() { return T(); }

int main() {
    {
        std::vector<T> v;
        v.reserve(4);
        T a;
        v.push_back(a);            // copy
        v.push_back(std::move(a)); // move
        v.push_back(make());       // move from the temporary
        v.emplace_back();          // in place
    }
    std::cout << T::c << ' ' << T::m << ' ' << T::d << '\n';
    return 0;
}
A 1 2 6
B 1 2 4
C 2 2 6
D 1 3 6
Question page & discussion
Question 17 Hard

What is the output of the following C++ program on a 64-bit system?

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

std::string make() { return "tmp"; }

int main() {
    std::string s = "x";
    auto&& a = s;          // forwarding reference: becomes std::string&
    auto&& b = make();     // becomes std::string&& (extends the temporary's lifetime)
    a += "y";
    b += "!";
    std::cout << std::is_lvalue_reference_v<decltype(a)> << std::is_rvalue_reference_v<decltype(b)> << ' ' << s << ' ' << b << '\n';
    return 0;
}
A 11 xy tmp!
B 10 xy tmp!
C 11 x tmp!
D 01 xy tmp
Question page & discussion
Question 18 Medium

What is the output of the following C++ program on a 64-bit system?

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

struct Image {
    std::string name;
    std::unique_ptr<int[]> pixels;
    Image(std::string n, int size) : name(std::move(n)), pixels(std::make_unique<int[]>(size)) {}
};

int main() {
    Image a("logo", 100);
    a.pixels[0] = 255;
    Image b = std::move(a);
    std::cout << b.name << ' ' << b.pixels[0] << ' ' << (a.pixels == nullptr) << ' ' << a.name.size() << '\n';
    return 0;
}
A logo 255 1 0
B logo 255 0 4
C logo 0 1 0
D Compilation error
Question page & discussion
Question 19 Hard

What is the output of the following C++ program on a 64-bit system?

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

struct Fast { Fast() = default; Fast(const Fast&) { std::cout << "Fc "; } Fast(Fast&&) noexcept { std::cout << "Fm "; } };
struct Slow { Slow() = default; Slow(const Slow&) { std::cout << "Sc "; } Slow(Slow&&) { std::cout << "Sm "; } };

int main() {
    Fast f; Slow s;
    Fast f2 = std::move_if_noexcept(f);
    Slow s2 = std::move_if_noexcept(s);
    std::cout << std::is_nothrow_move_constructible_v<Fast> << std::is_nothrow_move_constructible_v<Slow> << '\n';
    return 0;
}
A Fm Sc 10
B Fm Sm 10
C Fc Sc 00
D Fm Sm 11
Question page & discussion
Question 20 Medium

What is the output of the following C++ program on a 64-bit system?

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

void f(std::string&)        { std::cout << "&  "; }
void f(const std::string&)  { std::cout << "c& "; }
void f(std::string&&)       { std::cout << "&& "; }

int main() {
    std::string s = "a";
    const std::string cs = "b";
    f(s); f(cs); f(s + "x"); f(std::string("t")); f(std::move(cs));
    std::cout << '\n';
    return 0;
}
A & c& && && c&
B & c& c& && &&
C & & && && &&
D c& c& && && c&
Question page & discussion
Up next · Exercise 3Find the Error10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links