LearnFrenzy
Sign in Take a free test

Move Semantics · Exercise 2 of 4

Move Semantics Predict the Output

  • 20 questions
  • Page 1 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 1 Easy

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

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

struct W {
    W() = default;
    W(const W&) { std::cout << "C"; }
    W(W&&) noexcept { std::cout << "M"; }
};

W make() { return W(); }

int main() {
    W a;
    W b(a);
    W c(std::move(a));
    W d(make());
    W e(std::move(b));
    std::cout << '\n';
    return 0;
}
A CMM
B CMMM
C CCMM
D CMCM
Question page & discussion
Question 2 Easy

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

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

struct A {
    A() = default;
    A& operator=(const A&) { std::cout << "copy= "; return *this; }
    A& operator=(A&&) noexcept { std::cout << "move= "; return *this; }
};

int main() {
    A x, y;
    x = y;
    x = A();
    x = std::move(y);
    std::cout << '\n';
    return 0;
}
A copy= move= move=
B copy= copy= move=
C copy= move= copy=
D move= move= move=
Question page & discussion
Question 3 Hard

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

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

struct Tracked {
    Tracked() = default;
    Tracked(const Tracked&) { std::cout << "copy "; }
    Tracked(Tracked&&) noexcept { std::cout << "move "; }
};

struct Plain  { Tracked t; };
struct Logged { Tracked t; ~Logged() {} };     // only a destructor added

int main() {
    Plain p1;
    Plain p2 = std::move(p1);
    Logged l1;
    Logged l2 = std::move(l1);
    std::cout << '\n';
    return 0;
}
A move copy
B move move
C copy copy
D copy move
Question page & discussion
Question 4 Medium

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

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

struct Item {
    int id;
    explicit Item(int i) : id(i) {}
    Item(const Item& o) : id(o.id) { std::cout << "c" << id << ' '; }
    Item(Item&& o) : id(o.id) { std::cout << "m" << id << ' '; }    // may throw (not noexcept)
};

int main() {
    std::vector<Item> v;
    v.reserve(2);
    v.emplace_back(1);
    v.emplace_back(2);
    std::cout << "| ";
    v.emplace_back(3);              // capacity exceeded: existing elements must be transferred
    std::cout << '\n';
    return 0;
}
A | c1 c2
B | m1 m2
C | c1 c2 m3
D |
Question page & discussion
Question 5 Medium

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

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

int main() {
    std::vector<std::string> lines;
    std::string buf = "first line that is long enough";
    lines.push_back(std::move(buf));
    buf = "second";                  // assigning to a moved-from object is always fine
    lines.push_back(std::move(buf));
    buf.clear();
    buf += "third";
    lines.push_back(buf);
    std::cout << lines.size() << ' ' << lines[1] << ' ' << lines[2] << ' ' << buf << '\n';
    return 0;
}
A 3 second third third
B 3 second third
C 3 third third
D 2 second third
Question page & discussion
Question 6 Easy

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

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

int main() {
    int counter = 5;
    int old = std::exchange(counter, 0);
    std::string state = "idle";
    std::string prev = std::exchange(state, "running");
    std::cout << old << ' ' << counter << ' ' << prev << ' ' << state << '\n';
    return 0;
}
A 5 0 idle running
B 0 5 running idle
C 5 5 idle idle
D 0 0 running running
Question page & discussion
Question 7 Hard

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

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

void take(const std::string&) { std::cout << "L "; }
void take(std::string&&)      { std::cout << "R "; }

template <typename T> void fwd(T&& x) { take(std::forward<T>(x)); }
template <typename T> void mov(T&& x) { take(std::move(x)); }

int main() {
    std::string s = "hi";
    fwd(s); fwd(std::string("t"));
    mov(s); mov(std::string("t"));
    std::cout << '\n';
    return 0;
}
A L R R R
B L R L R
C R R R R
D L L L L
Question page & discussion
Question 8 Hard

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

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

class Holder {
    int* p_;
public:
    explicit Holder(int v) : p_(new int(v)) {}
    ~Holder() { delete p_; }
    Holder(const Holder&) = delete;
    Holder& operator=(Holder&& o) noexcept {
        if (this == &o) { std::cout << "[self] "; return *this; }
        delete p_;
        p_ = std::exchange(o.p_, nullptr);
        return *this;
    }
    int value() const { return p_ ? *p_ : -1; }
};

int main() {
    Holder a(7), b(9);
    a = std::move(a);
    b = std::move(a);
    std::cout << a.value() << ' ' << b.value() << '\n';
    return 0;
}
A [self] -1 7
B [self] 7 7
C -1 7
D [self] -1 9
Question page & discussion
Question 9 Medium

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

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

struct Person {
    std::string name;
    Person(std::string n) : name(std::move(n)) { std::cout << "ctor "; }
    Person(const Person& o) : name(o.name) { std::cout << "copy "; }
    Person(Person&& o) noexcept : name(std::move(o.name)) { std::cout << "move "; }
};

int main() {
    std::vector<Person> v;
    v.reserve(3);
    v.push_back(Person("Asha"));
    std::cout << "| ";
    v.emplace_back("Ravi");
    std::cout << "| ";
    Person p("Meena");
    v.push_back(p);
    std::cout << '\n';
    return 0;
}
A ctor move | ctor | ctor copy
B ctor | ctor | ctor copy
C ctor copy | ctor | ctor copy
D ctor move | ctor move | ctor move
Question page & discussion
Question 10 Medium

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

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

struct D {
    D() = default;
    D(const D&) { std::cout << "copy "; }
    D(D&&) noexcept { std::cout << "move "; }
};

D get_const() { const D d; return d; }

int main() {
    const D c;
    D a = std::move(c);
    D b = static_cast<const D&&>(D());
    std::cout << '\n';
    return 0;
}
A copy copy
B move move
C copy move
D move copy
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links