Object Lifecycle: Creation, Copying, Destruction Quiz Quiz

Explore key concepts of the object lifecycle, including creation, copying, and destruction. This quiz helps you understand constructors, destructors, copy semantics, and lifecycle behaviors important for reliable and maintainable code.

  1. Constructor Identification

    Which of the following is called when a new object is created from a class without passing any arguments?

    1. Destructor
    2. Default constructor
    3. Move assignment operator
    4. Copy constructor

    Explanation: The default constructor is called when an object is created without any arguments and without copying from another object. The copy constructor is used when creating an object as a copy of another. The destructor is called when the object is destroyed, not created. The move assignment operator is used for assigning temporary resources, not initializing an object.

  2. The Role of the Copy Constructor

    When is the copy constructor invoked in an object’s lifecycle, such as in the statement 'MyClass b = a;'?

    1. When assigning one object to another after both are created
    2. When explicitly destroying an object
    3. When creating a new object as a copy of an existing object
    4. When a new object is initialized without parameters

    Explanation: The copy constructor is used when a new object is created as a copy of an existing object, for example, 'MyClass b = a;'. Assignment after objects have been initialized uses the copy assignment operator, not the copy constructor. Destruction involves the destructor, not the copy constructor. Creating an object without parameters triggers the default constructor.

  3. Object Destruction Timing

    At what point in an object's lifecycle is the destructor typically called?

    1. During object copying
    2. Before the default constructor is called
    3. Immediately after object creation
    4. When an object goes out of scope or is explicitly deleted

    Explanation: The destructor is automatically called when an object goes out of scope or is explicitly deleted, ensuring any resources are properly released. It is not called immediately after creation—that is when constructors run. During object copying, either the copy or move constructor is triggered. A destructor is not called before construction.

  4. Shallow Copy vs Deep Copy

    What risk is associated with only implementing a shallow copy in classes that manage dynamic resources?

    1. Memory leaks due to double allocation
    2. Destructors not being called at all
    3. Resource conflicts due to multiple objects sharing the same resource pointer
    4. Objects being constructed incorrectly

    Explanation: A shallow copy leads to multiple objects sharing a single resource, like a pointer, which can cause resource conflicts when one object modifies or deletes the resource used by another. Memory leaks can occur if resources aren’t deallocated, but shallow copying more commonly leads to double deletion or dangling pointers. Construction errors occur during construction, not copying. Destructors are still called, but they might operate on already-freed memory.

  5. Order of Constructor and Destructor Calls

    In a program with local objects, what is the correct order in which destructors are called when exiting a scope?

    1. In the order determined by object size
    2. In reverse order of object creation
    3. In alphabetical order of variable names
    4. In the order objects were created

    Explanation: Destructors of local objects are called in the reverse order to their creation, following the Last-In-First-Out (LIFO) principle. The order is not based on variable names or object sizes. Calling destructors in creation order would risk destroying prerequisites needed by objects still in use.