Constructor Questions with Answers

10 practice questions on Constructor (C++) with answers and step-by-step solutions. Try 10 samples below, then practise all 10 free in the AptiNest app.

Sample questions

Question 1Medium

If no constructor is declared, the compiler assumes the class to have a default constructor with no arguments.

  1. True
  2. False
  3. None of these
  4. Can't say

Answer: A. True

Explanation

A Default constructor is that will either have no parameters, or all the parameters have default values. If no constructors are available for a class, the compiler implicitly creates a default parameter less constructor without a constructor initialize and a null body.

Step-by-step solution

  1. If a C++ class has no constructor, the compiler generates a default constructor.
  2. The generated default constructor takes no arguments.
  3. So the statement is true.

Why the other options are wrong

  • B. False is wrong; the compiler provides the default constructor.
  • C. The statement is clear and true, so None of these is wrong.
  • D. The rule is fixed by the language, so Can't say is wrong.

Tip: No constructor written means the compiler writes a no-argument one.

Question 2Medium

If the copy constructor receives its arguments by value, the copy constructor would

  1. call one-argument constructor of the class
  2. work without any problem
  3. call itself recursively
  4. call zero-argument constructor

Answer: C. call itself recursively

Explanation

This is because in order to pass by value, you need to COPY the object.
Therefore, you are passing in a copy of the object, in the definition of how to copy it.
Effectively what really happens if this trap is not there, is your copy constructor will call your copy constructor to create a copy, which will call your copy constructor to create a copy, which will call your copy constructor to create a copy,

Step-by-step solution

  1. A copy constructor receiving its argument by value must copy that argument first.
  2. Making that copy means calling the copy constructor again, which needs another copy first.
  3. This repeats without end, so the copy constructor calls itself recursively.
  4. The answer is call itself recursively.

Why the other options are wrong

  • A. The one-argument constructor is not what gets called; the copy constructor calls itself.
  • B. Passing by value causes infinite recursion, so it does not work without problems.
  • D. The zero-argument constructor is not called in this case.

Tip: A copy constructor must take its argument by reference.

Question 3Medium

Which of the following are NOT provided by the compiler by default...?

  1. Zero-argument Constructor
  2. Destructor
  3. Copy Constructor
  4. Copy Destructor

Answer: D. Copy Destructor

Explanation

Compiler provide default constructor, copy constructor and destructor but in c++ we can not write copy destructor.
A destructor takes no arguments and has no return type.
Its address cannot be taken. Destructors cannot be declared const, volatile, const volatile or static.
A destructor can be declared virtual or pure virtual.

Step-by-step solution

  1. If a class declares no constructors or destructor, the compiler provides a zero-argument constructor, a copy constructor and a destructor.
  2. A copy destructor is not a C++ concept, since a class has only one destructor.
  3. So the compiler does not provide a copy destructor by default.
  4. The answer is Copy Destructor.

Why the other options are wrong

  • A. The compiler does provide a zero-argument constructor by default.
  • B. The compiler does provide a destructor by default.
  • C. The compiler does provide a copy constructor by default.

Tip: Remember the default members: zero-argument constructor, copy constructor, assignment operator and destructor.

Question 4Medium

Which of the following statement is correct, whenever an object goes out of scope...?

  1. The default constructor of the object is called.
  2. The parameterized destructor is called.
  3. The default destructor of the object is called.
  4. None of the above.

Answer: C. The default destructor of the object is called.

Explanation

A destructor is a special member function of a class that is executed whenever an object of it's class goes out of scope or whenever the delete expression is applied to a pointer to the object of that class.

Step-by-step solution

  1. When an object goes out of scope, its destructor is called automatically.
  2. A class with no destructor written has an implicit default destructor, and that is called.
  3. C++ destructors take no parameters, so there is no parameterized destructor.
  4. So the answer is The default destructor of the object is called.

Why the other options are wrong

  • A. The constructor runs when the object is created, not when it goes out of scope.
  • B. C++ destructors cannot take parameters, so there is no parameterized destructor.
  • D. Option C is correct, so none of the above is wrong.

Tip: Going out of scope calls the destructor, never the constructor.

Question 5Medium

Which of the following statement is correct...?

  1. A destructor has the same name as the class in which it is present.
  2. A destructor has a different name than the class in which it is present.
  3. A destructor always returns an integer.
  4. A destructor can be overloaded.

Answer: A. A destructor has the same name as the class in which it is present.

Explanation

A destructor will have exact same name as the class prefixed with a tilde (~) and it can neither return a value nor can it take any parameters.
Destructor can be very useful for releasing resources before coming out of the program like closing files, releasing memories etc.

Step-by-step solution

  1. A destructor has the same name as the class with a tilde (~) prefix.
  2. It cannot return any value or take parameters.
  3. So it must have the same name as the class.
  4. This matches option A exactly.
  5. Final answer: A destructor has the same name as the class in which it is present.

Why the other options are wrong

  • B. Destructor name is not different but same as class with ~ prefix.
  • C. Destructor does not return any value.
  • D. Destructor cannot be overloaded as it has no parameters or return type.

Question 6Medium

Which of the following statement is correct about constructors...?

  1. A constructor has a return type.
  2. A constructor cannot contain a function call.
  3. A constructor has no return type.
  4. A constructor has a void return type.

Answer: C. A constructor has no return type.

Explanation

constructor never return any value.
It does not have any return type at all, not even void.
constructor is that the runtime uses constructor method to determine how much space is needed to store an object instance in memory.
After this space is allocated, the constructor is automatically called as an internal part of the instantiation and initialization process (if you don't provide values for attributes it will use default values for each type).
When the constructor exits, the runtime returns the newly-created instance.

Step-by-step solution

  1. A constructor is a special member function with the same name as the class.
  2. Constructors are not allowed a return type, not even void.
  3. So a constructor has no return type.
  4. The answer is: A constructor has no return type.

Why the other options are wrong

  • A. A constructor has no return type at all.
  • B. A constructor may contain function calls; there is no such restriction.
  • D. A void return type is not allowed for constructors either.

Tip: Constructors never have a return type.

Question 7Medium

Which of the following gets called when an object is being created?

  1. Constructor
  2. virtual function
  3. Destructor
  4. Main

Answer: A. Constructor

Explanation

The process of creating and deleting objects in C++ is not a trivial task.
Every time an instance of a class is created the constructor method is called.
The constructor has the same name as the class and it doesn't return any type. To call a constructor, you use the class name together with parameters surrounded by braces or parentheses.

Step-by-step solution

  1. Constructor gets called when an object is being created.
  2. It initializes the object's state.
  3. The constructor has the same name as the class and no return type.

Why the other options are wrong

  • B. A virtual function can be overridden but not automatically called during object creation.
  • C. A destructor is called when an object goes out of scope or is explicitly deleted.
  • D. Main is the entry point of a program, not related to object creation.

Tip: Remember: Constructors are called during object creation.

Question 8Medium

Which of the following cannot be declared as virtual...?

  1. Constructor
  2. Destructor
  3. Data Members
  4. Both A and C

Answer: D. Both A and C

Explanation

A virtual call is a mechanism to get work done given partial information.
In particular, "virtual" allows us to call a function knowing only any interfaces and not the exact type of the object.
To create an object you need complete information.
In particular, you need to know the exact type of what you want to create. Consequently, a "call to a constructor" cannot be virtual.
But we can declare destructor as virtual, It's even more important for an interface.
Any user of your class will probably hold a pointer to the interface, not a pointer to the concrete implementation.
When they come to delete it, if the destructor is non-virtual, they will call the interface's destructor (or the compiler-provided default, if you didn't specify one), not the derived class's destructor.
Instant memory leak.

Step-by-step solution

  1. A virtual call works from the interface alone, but a constructor needs the exact type, so it cannot be virtual.
  2. Data members are variables, not functions, so they cannot be declared virtual.
  3. A destructor can be virtual, and base classes usually make it virtual.
  4. Constructors (A) and data members (C) cannot be virtual, so the correct answer is option D, Both A and C.

Why the other options are wrong

  • A. A constructor cannot be virtual, but data members cannot be either, so this option is incomplete.
  • B. A destructor can be virtual and often should be in a base class.
  • C. Data members cannot be virtual, but the constructor cannot be either, so this option is incomplete.

Tip: Only non-constructor member functions can be virtual; make destructors virtual in base classes.

Question 9Medium

To ensure that every object in the array receives a destructor call, always delete memory allocated as an array with operator ____ .

  1. destructor
  2. delete
  3. delete[]
  4. kill[]

Answer: C. delete[]

Explanation

In the C++ programming language, the delete operator calls the destructor of the given argument, and returns memory allocated by new back to the heap.
delete[] - Deallocates the memory block pointed by array (if not null), releasing the storage space previously allocated to it by a call to operator new[] and rendering that pointer location invalid.

Step-by-step solution

  1. Memory allocated with new[] is an array and must be freed with the array form of delete.
  2. delete[] calls the destructor for every element before freeing the memory.
  3. Plain delete on an array runs only one destructor and is undefined behaviour.
  4. So the blank is filled by delete[].

Why the other options are wrong

  • A. Destructor is a member function name, not an operator.
  • B. Delete frees only one object and skips the other elements' destructors.
  • D. kill[] is not a C++ operator.

Tip: new[] goes with delete[], always.

Question 10Medium

Destructor calls are made in which order of the corresponding constructor calls...?

  1. Reverse order
  2. Forward order
  3. Depends on how the object is constructed
  4. Depends on how many objects are constructed

Answer: A. Reverse order

Explanation

The destructor calling sequence always goes from the derived to base, like popping the stack. This allows derived classes to clean up resources allocated by your base class.
In this case, the compiler knows how to construct this sequence, because it knows the exact type of the objects x and y statically.

Step-by-step solution

  1. Objects are constructed in one order, such as base first and then derived.
  2. Destructors run in the opposite order, so the last object built is destroyed first.
  3. So destructor calls go in reverse order of the constructor calls, which is option A.

Why the other options are wrong

  • B. Forward order would destroy the first object built first, which breaks the reverse rule.
  • C. The order is always reverse; it does not depend on how the object is built.
  • D. The count of objects does not change the reverse order rule.

Tip: Destructors run in reverse order of construction, like popping a stack.

Practise all 10 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 C++ topics · All topics