Constructors & Destructors Questions with Answers

20 practice questions on Constructors & Destructors (OOP Concepts) with answers and step-by-step solutions. Try 10 samples below, then practise all 20 free in the AptiNest app.

Sample questions

Question 1Easy

Which language feature runs automatically when an object is about to be destroyed in C++?

  1. Static initialiser
  2. Copy constructor
  3. Destructor
  4. Finalizer queue

Answer: C. Destructor

Explanation

A destructor (~ClassName) runs when an object's lifetime ends, e.g. when it goes out of scope or is deleted.

Step-by-step solution

  1. C++ calls a special member function when an object's lifetime ends.
  2. This member is the destructor, named with a tilde such as ~ClassName.
  3. So the destructor runs automatically; the correct answer is option C.

Why the other options are wrong

  • A. A static initialiser runs at program start, not at destruction.
  • B. A copy constructor runs when an object is copied.
  • D. C++ has no finalizer queue; that is a feature of other languages.

Tip: Constructor creates, destructor cleans up.

Question 2Easy

If a Java class defines no constructor at all, the compiler:

  1. Makes the class abstract
  2. Reports a compile error
  3. Copies the parent's constructors
  4. Supplies a default no-argument constructor

Answer: D. Supplies a default no-argument constructor

Explanation

A default constructor is added only when the class declares none; once you write any constructor, it is not generated.

Step-by-step solution

  1. If a Java class has no constructor, javac adds one for it.
  2. The added constructor takes no arguments.
  3. So the compiler supplies a default no-argument constructor; the correct answer is option D.

Why the other options are wrong

  • A. The class is not made abstract.
  • B. This is not a compile error.
  • C. Constructors are not copied from the parent.

Tip: No constructor written = default no-arg constructor added.

Question 3Easy

Which statement about a constructor is correct?

  1. It must be declared static
  2. It has no return type, not even void
  3. It must be called explicitly after new
  4. It can be inherited and overridden

Answer: B. It has no return type, not even void

Explanation

A constructor initialises a new object; it has the class's name (in Java/C++) and no return type.

Step-by-step solution

  1. A constructor has the same name as the class.
  2. It is used to initialise an object.
  3. It cannot have a return type, not even void.
  4. So it has no return type, not even void.

Why the other options are wrong

  • A. Constructors are not static; they run on each new object.
  • C. A constructor runs automatically as part of new, so it is not called explicitly after it.
  • D. Constructors are not inherited or overridden in Java; a subclass calls the parent's through super.

Tip: Remember: constructors have no return type.

Question 4Medium

What is the output of this C++ program?

#include <iostream>
struct Engine {
    Engine() { std::cout << "E "; }
    ~Engine() { std::cout << "~E "; }
};
struct Car {
    Engine e;
    Car() { std::cout << "C "; }
    ~Car() { std::cout << "~C "; }
};
int main() {
    { Car c; }
    std::cout << "\n";
}
  1. E C ~E ~C
  2. E C ~C ~E
  3. C E ~E ~C
  4. C E ~C ~E

Answer: B. E C ~C ~E

Explanation

Member objects are constructed before the containing class's constructor body runs, and destroyed after its destructor body, in reverse order.

Step-by-step solution

  1. Creating a Car first constructs its member Engine e, printing "E ", before Car's own constructor body runs at all.
  2. Car's own constructor body then runs, printing "C ".
  3. When the block ends, Car's destructor body runs first, printing "~C ".
  4. Only after that does the member Engine get destroyed automatically, printing "~E " - giving the full sequence E C ~C ~E.

Why the other options are wrong

  • A. This swaps the construction order - a member object is always fully built before the containing class's own constructor body starts, never after.
  • C. This places Engine's construction after Car's own print, which never happens - members are always constructed before the enclosing constructor body runs.
  • D. Same ordering mistake as option C, additionally reversing the destruction order too.

Tip: Members: built first-in, destroyed last-out, relative to their owner's own constructor/destructor code.

Question 5Medium

What is the output of this C++ program?

#include <iostream>
struct T {
    T() {}
    T(const T&) { std::cout << "C"; }
    T& operator=(const T&) { std::cout << "A"; return *this; }
};
int main() {
    T a;
    T b = a;
    T c;
    c = a;
    T d(a);
    std::cout << "\n";
}
  1. AAC
  2. CAC
  3. CCC
  4. ACA

Answer: B. CAC

Explanation

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

Step-by-step solution

  1. T b = a starts a new object, so the copy constructor prints "C".
  2. c = a assigns to an existing object, so operator= prints "A".
  3. T d(a) is another copy construction and prints "C"; the output is "CAC", index 1.

Why the other options are wrong

  • A. The first and third lines are copy constructions, which print C, not A.
  • C. The middle line is an assignment, which calls operator= and prints A.
  • D. The first line is a copy construction, so it prints C, not A.

Tip: Starting a new object uses the copy constructor; assigning to an existing one uses operator=.

Question 6Medium

What is constructor chaining?

  1. Creating several objects in a loop and linking each new object to the previous one
  2. One constructor calling another of the same class or of the parent
  3. Returning this from setters
  4. Calling a destructor from a constructor

Answer: B. One constructor calling another of the same class or of the parent

Explanation

In Java this(...) calls another constructor of the same class and super(...) calls the parent's; both must be the first statement.

Step-by-step solution

  1. this(...) calls another constructor of the SAME class; super(...) calls the PARENT class's constructor.
  2. Whichever is used, it must be the very first statement inside the constructor.
  3. This lets one constructor delegate shared setup work to another, instead of repeating that logic.

Why the other options are wrong

  • A. Creating several objects in a loop and linking them is not chaining constructors at all - it's just object creation in a loop.
  • C. Returning 'this' from setters is method chaining (fluent calls), a completely different concept from constructor chaining.
  • D. Calling a destructor from inside a constructor isn't a real or safe pattern, and has nothing to do with chaining constructors.

Tip: this(...) or super(...) must be line one of the constructor - nothing can come before it.

Question 7Medium

What is the output of this Java program?

class A {
    A() { System.out.print("A "); }
}
class B extends A {
    B() { this(5); System.out.print("B "); }
    B(int n) { System.out.print("B" + n + " "); }
}
public class Main {
    public static void main(String[] args) { new B(); }
}
  1. A B5 B
  2. A B B5
  3. A A B5 B
  4. B5 B A

Answer: A. A B5 B

Explanation

B() delegates to B(int) via this(5). B(int) implicitly calls super() first, so A's constructor runs once, then B(int)'s body, then the rest of B().

Step-by-step solution

  1. New B() calls B(), which first calls this(5).
  2. B(int) runs; its implicit super() call runs A(), which prints A.
  3. B(int) then prints B5, and B() prints B; the output is A B5 B, the correct answer is option A.

Why the other options are wrong

  • B. The this(5) call runs before the rest of B(), so B5 comes before B.
  • C. A is printed only once, by the constructor of A.
  • D. A is printed first, from the super constructor, not last.

Tip: Constructor chains run from the top parent down, and this(...) runs first.

Question 8Medium

A class has a private constructor and a static method that returns instances. This is typically used to:

  1. Allow subclassing from any package
  2. Control instance creation, as in Singleton or factory methods
  3. Make all fields immutable
  4. Make the class abstract

Answer: B. Control instance creation, as in Singleton or factory methods

Explanation

A private constructor stops outside code from calling new; the class itself decides how and how many instances are created.

Step-by-step solution

  1. Private constructor stops external code from creating instances.
  2. Static method controls instance creation process.
  3. This pattern is used for Singleton or factory methods.
  4. It ensures only one instance is created or specific ones are made.
  5. Final answer: Control instance creation, as in Singleton or factory methods.

Why the other options are wrong

  • A. A private constructor blocks subclassing from outside, so it does not allow it.
  • C. Immutability comes from field design, not from a private constructor.
  • D. A private constructor does not make a class abstract; the two are separate features.

Tip: Use this pattern to restrict object creation and manage instances carefully.

Question 9Hard

A C++ class holds a raw pointer to heap memory and relies on the compiler-generated copy constructor. What problem arises?

  1. The class fails to compile
  2. The pointer is set to nullptr in the copy
  3. The default copy is always a deep copy, which wastes memory by duplicating every buffer
  4. Shallow copy: both objects share the buffer, leading to double deletion

Answer: D. Shallow copy: both objects share the buffer, leading to double deletion

Explanation

The default copy copies the pointer value only. Both destructors then delete the same memory, which is why the Rule of Three requires a user-defined copy constructor, assignment and destructor.

Step-by-step solution

  1. The compiler-generated copy constructor copies the raw pointer by value.
  2. Both objects end up pointing to the same heap memory.
  3. When both destructors run, they delete the same memory location twice.
  4. This leads to undefined behavior and potential program crash.
  5. Shallow copy: both objects share the buffer, leading to double deletion.

Why the other options are wrong

  • A. The class compiles fine; the problem appears at runtime when the buffer is freed.
  • B. The copy does not get nullptr; the raw pointer value is copied as it is.
  • C. The default copy is shallow, not deep, so it does not duplicate every buffer.

Tip: Always implement custom copy constructor and destructor for classes with raw pointers to avoid double deletion.

Question 10Hard

What is the output of this C++ program?

#include <iostream>
#include <memory>
struct R {
    int id;
    R(int i) : id(i) { std::cout << "+" << id; }
    ~R() { std::cout << "-" << id; }
};
int main() {
    auto a = std::make_unique<R>(1);
    {
        auto b = std::make_unique<R>(2);
        a = std::move(b);
    }
    std::cout << "|";
}
  1. +1+2|-1-2
  2. +1+2-2|-1
  3. +1+2-1|-2
  4. +1+2-1-2|

Answer: C. +1+2-1|-2

Explanation

Move-assigning into a destroys the R(1) it owned. b is then empty, so nothing is destroyed at the inner brace. R(2), now owned by a, is destroyed when main ends, after the "|".

Step-by-step solution

  1. A holds R(1): "+1" is printed.
  2. Inside the block b holds R(2): "+2" is printed; a = std::move(b) destroys the old R(1), printing "-1".
  3. The block ends; b is now empty, so nothing is printed; then "|" is printed.
  4. At the end of main a is destroyed, printing "-2"; the output is "+1+2-1|-2", index 2.

Why the other options are wrong

  • A. R(1) is destroyed when a is reassigned, not at the end; R(2) is not destroyed at the block end.
  • B. R(2) is still owned by a after the move, so it is not destroyed at the block end.
  • D. R(1) is destroyed on reassignment, before the bar, not after it.

Tip: Assigning a new unique_ptr destroys the old object straight away; a moved-from pointer is left empty.

Practise all 20 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 OOP Concepts topics · All topics