Constructor and destructor interview questions, with answers
Constructors get more interview questions than their size suggests, because they are where object initialisation rules meet inheritance rules and the language's small print — a default constructor that disappears, a super() call you never wrote, a return type that turns the constructor into a method. Destructors add the C++ half: when they run, and why the base class's should be virtual.
These are the questions as they get asked, with the rule and the code. Then take the free OOP diagnostic — ten questions over all fifteen OOP topics, and a readiness figure with the arithmetic shown.
The questions, with answers
1.What is a constructor, and how is it different from a method?
A constructor is the special routine that runs when an object is created, to put it into a valid initial state. It has the class's name, it has no return type — not even void — and it cannot be called on an existing object; new is the only thing that invokes it. Write a return type by mistake and Java quietly treats the declaration as an ordinary method with the same name as the class, which is a favourite trick question. Other differences: constructors are not inherited, cannot be abstract, static or final, and can be chained with this(...) and super(...). A method does work on an object that already exists; a constructor is what makes it exist.
2.What is a default constructor, and when does the compiler stop providing one?
A default constructor is a no-argument constructor. If a class declares no constructor at all, the compiler generates one that does nothing beyond calling the superclass's no-argument constructor. The moment you declare any constructor — say, one that takes an id — the generated one disappears, and new Thing() stops compiling unless you add a no-argument constructor yourself. That is the source of a common breakage: adding a parameterised constructor to a class that other code, or a framework such as JPA or Jackson, creates through new Thing(). The same rule holds in C++ and C#; in C++ the implicit default constructor also stops being generated the same way.
3.What is constructor overloading, and how does this() chain constructors?
A class can declare several constructors with different parameter lists, and the call picks one by the arguments — the same overload resolution as methods. To avoid repeating initialisation, one constructor can delegate to another with this(...), which must be the first statement. A no-argument constructor that calls this(100.0) runs the parameterised constructor completely first, then continues with its own body; so if both print something, the parameterised message appears before the no-argument one. Only one of this(...) or super(...) may appear, and only first, because the object must be initialised exactly once before any other code touches it. In C++ the same feature is called a delegating constructor (C++11).
class Account { double balance; Account() { this(100.0); System.out.println("no-arg done, balance=" + balance); } // delegates first Account(double b) { balance = b; System.out.println("opening balance set"); } } // new Account() prints: opening balance set / no-arg done, balance=100.04.Why does a subclass constructor call super(), and when does the implicit call fail to compile?
The base part of an object has to be initialised before the derived part, so every subclass constructor begins by calling a superclass constructor — if you do not write super(...), the compiler inserts super() with no arguments. That implicit call fails when the superclass has no no-argument constructor: if A declares only A(int x), then class B extends A with a plain B() will not compile until B() explicitly calls super(some int). The follow-up: constructors run base-first down the chain, so creating a C that extends B that extends A prints A, then B, then C. Fields with initialisers and instance initialiser blocks run after the super call and before the rest of the constructor body.
5.What is a copy constructor, and does Java have one?
A constructor that takes another instance of the same class and initialises the new object from it. C++ generates one automatically — a member-wise copy that is wrong for classes owning raw pointers, which is why the rule of three says a class with a destructor also needs its own copy constructor and copy assignment. Java has no automatic copy constructor and no shallow-copy syntax beyond clone(); you write one by hand, Point(Point other) { this.x = other.x; this.y = other.y; }, and it is usually preferred to clone() because it is explicit and works with final fields. The interview point is deep versus shallow: copying a reference to a mutable field shares that object between the copies, so a proper copy constructor copies mutable members too.
6.In C++, what is the difference between a member initialiser list and assignment in the constructor body?
With an initialiser list, Point(int a) : x(a) {}, the member is constructed directly with the value. With assignment in the body, Point(int a) { x = a; }, the member is first default-constructed and then assigned — two steps instead of one. For an int the difference is invisible; for a class-type member it costs a default construction plus an assignment, and for three kinds of member the body form does not compile at all: const members, reference members, and members of a class type that has no default constructor. Those must be initialised in the list. Members are initialised in the order they are declared in the class, not the order they appear in the list, which is a second trick question.
7.What is a destructor, and what does Java have instead?
In C++ a destructor, written ~ClassName(), runs automatically when an object's lifetime ends — a stack object going out of scope, or delete on a heap object — and it is where resources are released. Because it runs deterministically, C++ builds RAII on it: acquire in the constructor, release in the destructor, and a lock or file is cleaned up even when an exception unwinds the stack. Java has garbage collection instead: memory is reclaimed by the collector, and Object.finalize() was never a reliable place for cleanup — it runs at an unknown time or never, and it is deprecated. For non-memory resources Java uses try-with-resources and the AutoCloseable interface, which gives the deterministic release that a destructor provides in C++.
8.What are private constructors used for, and can a constructor be static, final or abstract?
A private constructor stops other classes from calling new: that is how a singleton controls its single instance, how a utility class of static methods prevents instantiation, and how a static factory method forces callers through a named entry point that can cache, validate or return a subtype. It does not stop nested classes or code inside the class itself. Constructors cannot be static (they initialise an instance), final (they are not inherited, so there is nothing to prevent overriding) or abstract (they must have a body). In C++, constructors likewise cannot be virtual, which is another favourite: dispatch on an object's type needs the object to exist, and the constructor is what is creating it.
How the diagnostic asks it
One question from the OOP bank, exactly as a sitting would show it. The bank has 5 on constructors & destructors and 60 across OOP.
Which statement about constructors is correct?
- 1A constructor must always be called explicitly by the programmer after creating an object.
- 2A class can have at most one constructor.
- 3A constructor is a special method automatically invoked when an object is created, and it has the same name as the class with no return type.correct
- 4Constructors are inherited and overridden by subclasses exactly like normal methods.
A constructor initializes an object's state and runs automatically at creation time; it shares the class name and has no return type (not even void). It never has to be called explicitly after creation, because object creation invokes it; a class may declare several constructors (constructor overloading), not at most one; and constructors are not inherited or overridden like normal methods, although a subclass constructor can invoke a parent's via super().
Measure it
Reading answers tells you what’s true. A diagnostic tells you what you get wrong.
10 OOP questions across its topics, easy to hard, about fifteen minutes. You get a readiness figure with the arithmetic shown, the topics you missed named, and a practice set sized for today. Free: 1 diagnostic a month and 15 problems a day. No card.