Skills101
← All practice quizzes

Quiz: Copy Constructor and Deep Copy

5 questions · new set on every retakeNo time limitPass: 60%+10 XP on first pass

When C++ copies an object, which constructor runs, and why does the default copy sometimes crash your program? Shallow vs deep copy, the copy constructor, the copy assignment operator, copy-and-swap and the rule of three.

Log in to take the quiz

Free account · see your score, answers and explanations, earn XP

  1. Question 1

    Which function runs for Student b = a; when b is being declared?

  2. Question 2

    What does this print?

    #include <iostream>
    using namespace std;
    struct T {
        T() {}
        T(const T &) { cout << "C"; }
        T &operator=(const T &) { cout << "A"; return *this; }
    };
    void f(T x) {}
    int main() {
        T a;
        T b = a;
        b = a;
        f(a);
        return 0;
    }
    
  3. Question 3

    What is a shallow copy?

  4. Question 4

    Why is a shallow copy dangerous for a class whose destructor does delete[] data?

  5. Question 5

    Why must the copy constructor take its parameter by reference?

🎯 Today’s placement challenge — 10 questions, no login. Can you score 10/10?Take today’s challenge →