LearnFrenzy
Sign in Take a free test

OOPs · Topic 6 of 10

Object Lifecycle and Copying: questions with answers

Creation and destruction order, RAII and smart pointers, garbage collection and reference counting, try-with-resources and with, shallow vs deep copy, copy and move semantics, the rules of three and five.

Questions
45
Timed sets
3
Answers run or computed
32
Your accuracy
–

What this topic covers

12 subtopics
  • Shallow vs deep copy7
  • Copying objects6
  • Move semantics6
  • Garbage collection5
  • Object lifetime5
  • Smart pointers4
  • Copy semantics3
  • Deterministic cleanup3
  • Destruction order2
  • Reference counting2
  • Destructors1
  • RAII1
  • Beginner 15
  • Intermediate 15
  • Advanced 15

About this topic

Every object is created, used and eventually destroyed. C++ destroys objects deterministically – at the end of their scope or on delete – which makes RAII (resources released in destructors) possible. Java and Python free memory automatically (tracing garbage collection, reference counting), so other resources are released with try-with-resources and with. Copying raises the classic question: does the copy share inner objects (shallow) or duplicate them (deep)? Every order printed here is the real one.

Free preview

Sample questions

Try each one first, then open the answer for the explanation and why the other options are wrong.

Question 1 Destruction order Beginner Practice Code executed

What does this print?

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

struct Tracer {
    std::string n;
    Tracer(std::string s) : n(s) { std::cout << "+" << n << ' '; }
    ~Tracer() { std::cout << "-" << n << ' '; }
};

int main() {
    {
        Tracer a("a");
        Tracer b("b");
    }
    std::cout << "end\n";
}
+a -a +b -b end
+a +b -a -b end
+a +b -b -a end
+a +b end -b -a
Show answer and explanation
Correct answer C

Explanation

Local objects are destroyed when their block ends, in the reverse order of construction – b depended on nothing created after it, so it goes first.

Why the other options are wrong

  • ABoth live until the end of the block.
  • BDestruction reverses construction.
  • DThey die at the closing brace, before "end".
Question 2 Copy semantics Intermediate Practice Code executed

This class owns a raw pointer and relies on the default copy constructor. What goes wrong at run time?

C++ main.cpp
#include <iostream>

struct Buffer {
    int* data;
    Buffer() : data(new int(42)) {}
    ~Buffer() { delete data; }
};

int main() {
    Buffer a;
    Buffer b = a;
    std::cout << *b.data << '\n';
}
It leaks memory but runs correctly
It fails to compile
The default copy copies the pointer, so a and b both delete the same int – a double free
b.data is null
Show answer and explanation
Correct answer C

Explanation

A memberwise copy of a pointer is a shallow copy: two owners, one allocation. When both destructors run, the memory is freed twice (AddressSanitizer stops the program). Follow the rule of three/five, or own the memory with std::unique_ptr / std::vector.

Why the other options are wrong

  • AFreeing twice is undefined behaviour, not a leak.
  • BThe default copy compiles.
  • DIt points to the same int as a.
Question 3 Move semantics Advanced Practice Code executed

All five special members print. What does this print?

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

struct T {
    T()                          { std::cout << "ctor "; }
    T(const T&)                  { std::cout << "copy-ctor "; }
    T(T&&) noexcept              { std::cout << "move-ctor "; }
    T& operator=(const T&)       { std::cout << "copy-assign "; return *this; }
    T& operator=(T&&) noexcept   { std::cout << "move-assign "; return *this; }
};

int main() {
    T a;
    T b = std::move(a);
    T c;
    c = std::move(b);
    T d = c;
    c = d;
    std::cout << '\n';
}
ctor copy-ctor ctor copy-assign copy-ctor copy-assign
ctor move-assign ctor move-assign copy-ctor copy-assign
ctor move-ctor ctor move-assign copy-ctor copy-assign
ctor move-ctor ctor move-ctor copy-ctor copy-assign
Show answer and explanation
Correct answer C

Explanation

Creating a new object from another uses a constructor; changing an existing object uses an assignment operator. An rvalue (std::move) selects the move version, a named lvalue the copy version.

Why the other options are wrong

  • Astd::move enables the move overloads.
  • Bb is being created, so it is a constructor.
  • Dc already exists, so it is an assignment.
42 more questions in the timed setsSame format as the samples, with a timer, a question palette and a full review at the end.
Start practising

OOPs

All 10 topics

Topics, levels and full mocks
  1. 01 Classes, Objects and Constructors45 questions · 3 levels
  2. 02 Encapsulation and Access Modifiers45 questions · 3 levels
  3. 03 Inheritance45 questions · 3 levels
  4. 04 Polymorphism45 questions · 3 levels
  5. 05 Abstract Classes and Interfaces45 questions · 3 levels
  6. 06 Object Lifecycle and Copying45 questions · 3 levels
  7. 07 static, final, this and super45 questions · 3 levels
  8. 08 Exception Handling45 questions · 3 levels
  9. 09 SOLID and Class Relationships45 questions · 3 levels
  10. 10 Design Patterns45 questions · 3 levels

Search LearnFrenzy

Esc

Quick links