Copy & Move Semantics Questions with Answers

18 practice questions on Copy & Move Semantics (C++) with answers and step-by-step solutions. Try 10 samples below, then practise all 18 free in the AptiNest app.

Sample questions

Question 1Easy

According to the Rule of Three, if a class needs a user-defined destructor, which other two members does it almost certainly need?

  1. Move constructor and move assignment operator
  2. Copy constructor and copy assignment operator
  3. Conversion operator and copy constructor
  4. Default constructor and move constructor

Answer: B. Copy constructor and copy assignment operator

Explanation

A custom destructor usually means the class manages a resource, so copying must also be customised. The Rule of Five adds the move constructor and move assignment.

Step-by-step solution

  1. The Rule of Three says a class that needs a custom destructor usually needs a custom copy constructor and copy assignment operator.
  2. A destructor usually means the class owns a resource, so copying must be handled too.
  3. So the two other members are the copy constructor and the copy assignment operator: option B.

Why the other options are wrong

  • A. Move members belong to the Rule of Five, not the Rule of Three.
  • C. A conversion operator is not part of the Rule of Three.
  • D. The default constructor and move constructor are not part of the Rule of Three.

Tip: Rule of Three: destructor, copy constructor and copy assignment go together.

Question 2Easy

What does std::move actually do?

  1. It casts its argument to an rvalue reference so that move operations can be selected
  2. It always calls the move constructor immediately
  3. It sets the source object to its default value
  4. It moves the object's bytes to a new memory location

Answer: A. It casts its argument to an rvalue reference so that move operations can be selected

Explanation

std::move is just a static_cast to T&&. The move happens only if a move constructor or move assignment then receives that rvalue.

Step-by-step solution

  1. Std::move is a cast of its argument to an rvalue reference.
  2. It does not move any memory by itself.
  3. The actual move happens only if a move constructor or move assignment receives the result.
  4. So the answer is option A.

Why the other options are wrong

  • B. It does not call the move constructor; it only casts the argument.
  • C. The source object is not reset by std::move itself; it is only the cast.
  • D. It does not copy bytes to a new location.

Tip: Std::move only casts; the move happens in the move constructor or move assignment that receives the cast value.

Question 3Easy

What is the output?

#include <iostream>
using namespace std;

struct A {
    A() {}
    A(const A&) { cout << "C"; }
    A& operator=(const A&) { cout << "="; return *this; }
};

int main() {
    A a;
    A b = a;
    A c;
    c = a;
    A d(b);
    return 0;
}
  1. CCC
  2. C=C
  3. ==C
  4. C==

Answer: B. C=C

Explanation

A b = a; and A d(b); create new objects, so they use the copy constructor. c = a; assigns to an existing object, so it uses copy assignment.

Step-by-step solution

  1. A b = a; creates a new object from a, so the copy constructor prints C.
  2. A c; is default constructed, and c = a; assigns to an existing object, so copy assignment prints =.
  3. A d(b); creates a new object from b, so the copy constructor prints C again.
  4. The output is C=C: option B.

Why the other options are wrong

  • A. c = a is an assignment to an existing object, so it prints = and not C.
  • C. The first new object is created by copying, so it prints C, not =.
  • D. The first output is C, not =, because b is created by copy construction.

Tip: A new object from another object uses the copy constructor; assigning to an existing object uses copy assignment.

Question 4Easy

What is the output?

#include <iostream>
using namespace std;

struct A {
    A() {}
    A(const A&) { cout << "copy "; }
};

void byValue(A) {}
void byRef(const A&) {}

int main() {
    A a;
    byValue(a);
    byRef(a);
    byValue(a);
    return 0;
}
  1. copy copy
  2. copy copy copy
  3. copy
  4. Nothing is printed

Answer: A. copy copy

Explanation

Passing an lvalue by value copy-constructs the parameter; passing by const reference makes no copy. Two by-value calls give two copies.

Step-by-step solution

  1. byValue(a) copies a into the parameter, so the copy constructor prints copy.
  2. byRef(a) binds a const reference, so no copy is made and nothing prints.
  3. The second byValue(a) copies again, printing copy.
  4. The output is copy copy: option A.

Why the other options are wrong

  • B. Only two copies are made, since the byRef call does not copy.
  • C. There are two by-value calls, so two copies are made, not one.
  • D. Passing by value calls the copy constructor, so text is printed.

Tip: Passing by value copies the object; passing by const reference does not.

Question 5Medium

A class holds a raw owning pointer char* buf allocated with new[] in its constructor and freed in its destructor, and relies on the compiler-generated copy constructor. What goes wrong when an object is copied?

  1. Both objects point to the same buffer, so it is deleted twice when they are destroyed
  2. The copy gets a new buffer but the contents are not copied
  3. The original object's pointer is set to nullptr
  4. The compiler refuses to generate a copy constructor for pointer members

Answer: A. Both objects point to the same buffer, so it is deleted twice when they are destroyed

Explanation

The implicit copy constructor copies the pointer value (a shallow copy). Two destructors then delete[] the same memory. The fix is a deep copy, or using std::string / std::vector / smart pointers.

Step-by-step solution

  1. Default copy copies the pointer value.
  2. Two objects now point to the same buffer.
  3. Both destructors delete[] it: double free.

Why the other options are wrong

  • B. No new buffer is created by default copy.
  • C. The original pointer is unchanged.
  • D. The compiler still generates a copy constructor.

Tip: Rule of three/five.

Question 6Medium

Why should a user-defined copy assignment operator handle self-assignment (a = a)?

  1. If it frees its own resource before copying, it would then read from memory it has just freed
  2. Self-assignment always causes a compilation error otherwise
  3. The compiler calls the destructor automatically before self-assignment
  4. Self-assignment would call the copy constructor recursively

Answer: A. If it frees its own resource before copying, it would then read from memory it has just freed

Explanation

A naive "delete old; copy from other" implementation destroys the data it is about to copy when other is *this. A self-check or the copy-and-swap idiom avoids this.

Step-by-step solution

  1. Copy assignment often frees the old resource first.
  2. If a = a, the source is the very object just freed.
  3. Then it copies from freed memory.
  4. A self-check or copy-and-swap avoids this.

Why the other options are wrong

  • B. It compiles fine; the problem is at runtime.
  • C. The compiler does not call destructors this way.
  • D. Assignment does not recurse into the copy constructor.

Tip: Check this != &other.

Question 7Medium

Which C++17 situation is guaranteed copy elision (no copy/move constructor is called or even required)?

  1. Throwing a named local exception object
  2. Passing an lvalue by value to a function
  3. Returning a named local variable (NRVO)
  4. Initialising an object from a prvalue of the same type, e.g. returning T() from a function returning T

Answer: D. Initialising an object from a prvalue of the same type, e.g. returning T() from a function returning T

Explanation

C++17 made prvalue elision mandatory, so T x = T(); works even for non-movable types. NRVO for named locals is still only permitted, not guaranteed.

Step-by-step solution

  1. From C++17, a prvalue of the same type used to initialise an object is not copied or moved.
  2. Returning T() from a function that returns T is one such case.
  3. Returning a named local variable is only allowed to be elided, not guaranteed.
  4. So the answer is option D.

Why the other options are wrong

  • A. Throwing a named exception object is not guaranteed to skip copying.
  • B. Passing an lvalue by value always makes a copy.
  • C. Returning a named local is only permitted to be elided, not guaranteed.

Tip: C++17 guarantees elision for prvalues; named objects only get optional elision.

Question 8Medium

What is the output?

#include <iostream>
using namespace std;

#include <vector>

struct A {
    A(int) { cout << "I"; }
    A(const A&) { cout << "C"; }
    A(A&&) noexcept { cout << "M"; }
};

int main() {
    vector<A> v;
    v.reserve(2);
    v.emplace_back(5);
    v.push_back(6);
    return 0;
}
  1. IMIM
  2. IMI
  3. II
  4. IIM

Answer: D. IIM

Explanation

emplace_back forwards 5 to the constructor and builds the element in place. push_back(6) first converts 6 to a temporary A (I) and then moves it into the vector (M).

Step-by-step solution

  1. reserve(2) allocates space but builds no elements.
  2. emplace_back(5) builds the element in place with A(int), printing I.
  3. push_back(6) first converts 6 to a temporary A, printing I, then moves it into the vector, printing M.
  4. The output is IIM: option D.

Why the other options are wrong

  • A. Emplace_back builds in place with one I, not with a move.
  • B. Push_back also moves the temporary into the vector, so M is printed after the second I.
  • C. The temporary is moved into the vector, so M is printed.

Tip: Emplace_back builds the element in place; push_back of a value first builds a temporary and then moves it.

Question 9Hard

What is the output?

#include <iostream>
using namespace std;

#include <utility>

struct S {
    S() {}
    S(S&&) noexcept { cout << "M"; }
    S& operator=(S&&) noexcept { cout << "A"; return *this; }
};

int main() {
    S a, b;
    std::swap(a, b);
    return 0;
}
  1. AAA
  2. MMM
  3. MAA
  4. MA

Answer: C. MAA

Explanation

The generic std::swap move-constructs a temporary from a (M), move-assigns b into a (A) and move-assigns the temporary into b (A).

Step-by-step solution

  1. std::swap move-constructs a temp from a: M.
  2. Then move-assigns b into a: A.
  3. Then move-assigns the temp into b: A.
  4. Output: MAA.

Why the other options are wrong

  • A. The first step is a construction, not an assignment.
  • B. Only one construction is needed; the rest are assignments.
  • D. Two assignments happen, not one.

Tip: swap = M A A.

Question 10Hard

What is the output when compiled as C++17?

#include <iostream>
using namespace std;

struct A {
    int id = 7;
    A() { cout << "D"; }
    A(const A&) { cout << "C"; }
    A(A&&) { cout << "M"; }
};

A make() { return A(); }

int main() {
    A x = make();
    cout << x.id;
    return 0;
}
  1. DC7
  2. DMM7
  3. D7
  4. DM7

Answer: C. D7

Explanation

Since C++17, initialising an object from a prvalue of the same type is guaranteed copy elision: A() materialises directly into x, so neither the copy nor the move constructor runs.

Step-by-step solution

  1. A() in make() is a prvalue.
  2. From C++17, initialising x from a prvalue of the same type is guaranteed elision.
  3. Only the default constructor runs (D), then id 7 prints.
  4. Output: D7.

Why the other options are wrong

  • A. No copy is made; elision is guaranteed in C++17.
  • B. No move is made either; the object is built directly in x.
  • D. A move is not needed because of guaranteed elision.

Tip: C++17: no copy or move for prvalue initialisation.

Practise all 18 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all C++ topics · All topics