C++ Constructors & Destructors Quiz
40 in-depth questions covering C++ constructors and destructors, including default/parameterized constructors, initializer lists, copy/move constructors, and RAII concepts — with 16 code examples to master object lifecycle management.
Question 1
What is the primary purpose of a constructor in C++?
Question 2
What is the key difference between a default constructor and a parameterized constructor?
Question 3
When is a destructor called in C++?
Question 4
What is the main advantage of using initializer lists over assignment in constructors?
Question 5
What is a copy constructor and when is it called?
#include <iostream>
#include <string>
class Person {
private:
std::string name;
int age;
public:
Person(const std::string& n, int a) : name(n), age(a) {
std::cout << "Constructor called for " << name << std::endl;
}
Person(const Person& other) : name(other.name), age(other.age) {
std::cout << "Copy constructor called for " << name << std::endl;
}
};
int main() {
Person p1("Alice", 30);
Person p2 = p1; // Copy constructor called
return 0;
}Question 6
What is the Rule of Three in C++?
Question 7
What is RAII and why is it important?
Question 8
What is a move constructor and how does it differ from a copy constructor?
#include <iostream>
#include <string>
#include <vector>
class Resource {
private:
std::vector<int> data;
public:
Resource(size_t size) : data(size, 42) {
std::cout << "Resource created with " << size << " elements" << std::endl;
}
// Copy constructor - expensive
Resource(const Resource& other) : data(other.data) {
std::cout << "Copy constructor called" << std::endl;
}
// Move constructor - efficient
Resource(Resource&& other) noexcept : data(std::move(other.data)) {
std::cout << "Move constructor called" << std::endl;
}
};
Resource createResource() {
return Resource(1000);
}
int main() {
Resource res = createResource(); // Move constructor called
return 0;
}Question 9
What happens if you don't provide a default constructor for a class?
Question 10
What is the purpose of the noexcept specifier on move constructors?
Question 11
What is constructor initialization order and why does it matter?
#include <iostream>
class A {
public:
A() { std::cout << "A constructed" << std::endl; }
};
class B {
public:
B() { std::cout << "B constructed" << std::endl; }
};
class Test {
private:
A a;
B b;
public:
Test() : b(), a() { // Initialization order is a, then b
std::cout << "Test constructed" << std::endl;
}
};
int main() {
Test t;
return 0;
}Question 12
What is the difference between delete and delete[] for destructors?
Question 13
What is a conversion constructor and when is it useful?
#include <iostream>
#include <string>
class StringWrapper {
private:
std::string data;
public:
StringWrapper(const std::string& s) : data(s) {} // Conversion constructor
void print() const {
std::cout << data << std::endl;
}
};
void processWrapper(StringWrapper sw) {
sw.print();
}
int main() {
std::string str = "Hello";
processWrapper(str); // Implicit conversion
return 0;
}Question 14
What is copy elision and why is it beneficial?
Question 15
What is the Rule of Zero in modern C++?
Question 16
What happens when a constructor throws an exception?
#include <iostream>
#include <stdexcept>
class Resource {
private:
int* data;
public:
Resource(int value) {
if(value < 0) {
throw std::invalid_argument("Negative value not allowed");
}
data = new int(value);
std::cout << "Resource created" << std::endl;
}
~Resource() {
delete data;
std::cout << "Resource destroyed" << std::endl;
}
};
int main() {
try {
Resource r(-1); // Exception thrown
} catch(const std::exception& e) {
std::cout << "Exception: " << e.what() << std::endl;
}
return 0;
}Question 17
What is the difference between std::move and copying in terms of object state?
Question 18
What is the purpose of explicit constructors?
#include <iostream>
class Integer {
private:
int value;
public:
explicit Integer(int v) : value(v) {} // Prevents implicit conversion
void print() const {
std::cout << value << std::endl;
}
};
void processInteger(Integer i) {
i.print();
}
int main() {
// processInteger(42); // ERROR: explicit constructor
processInteger(Integer(42)); // OK: explicit conversion
return 0;
}Question 19
What is the relationship between constructors and virtual functions?
Question 20
What is the most important consideration when implementing RAII for file handling?
#include <iostream>
#include <fstream>
#include <string>
class FileHandler {
private:
std::ifstream file;
public:
FileHandler(const std::string& filename) {
file.open(filename);
if(!file.is_open()) {
throw std::runtime_error("Failed to open file");
}
}
~FileHandler() {
if(file.is_open()) {
file.close();
}
}
std::string readLine() {
std::string line;
std::getline(file, line);
return line;
}
bool isOpen() const {
return file.is_open();
}
};
int main() {
try {
FileHandler handler("data.txt");
if(handler.isOpen()) {
std::cout << handler.readLine() << std::endl;
}
} catch(const std::exception& e) {
std::cout << "Error: " << e.what() << std::endl;
}
// File automatically closed here
return 0;
}Question 21
What is the key benefit of move constructors over copy constructors for expensive resources?
Question 22
What happens to member initialization order when using inheritance?
Question 23
What is the difference between = default and providing an empty constructor body?
#include <iostream>
class A {
public:
A() = default; // Compiler generates default initialization
};
class B {
public:
B() {} // User-defined, does nothing
};
class C {
private:
int x;
public:
C() = default; // x gets default initialization (0)
};
class D {
private:
int x;
public:
D() {} // x is uninitialized!
};
int main() {
D d;
// d.x is uninitialized - undefined behavior!
return 0;
}Question 24
What is the most common pitfall when implementing copy constructors?
Question 25
What is the relationship between smart pointers and RAII?
Question 26
What is the Rule of Five in C++11 and later?
Question 27
What is the most important reason to use initializer lists for const members?
#include <iostream>
class ConstExample {
private:
const int value;
public:
// ConstExample(int v) { value = v; } // ERROR: cannot assign to const
ConstExample(int v) : value(v) {} // OK: initialize const member
void print() const {
std::cout << value << std::endl;
}
};
int main() {
ConstExample ex(42);
ex.print();
return 0;
}Question 28
What is the difference between lvalues and rvalues in the context of move constructors?
Question 29
What happens when a destructor is declared as virtual?
#include <iostream>
class Base {
public:
virtual ~Base() {
std::cout << "Base destructor" << std::endl;
}
};
class Derived : public Base {
public:
~Derived() {
std::cout << "Derived destructor" << std::endl;
}
};
int main() {
Base* ptr = new Derived();
delete ptr; // Calls both destructors
return 0;
}Question 30
What is the most efficient way to initialize a member that is itself a class with a constructor?
Question 31
What is the key difference between std::unique_ptr and raw pointers in RAII?
Question 32
What is the most common mistake when implementing move constructors?
#include <iostream>
#include <vector>
class BadMove {
private:
std::vector<int> data;
public:
BadMove(size_t size) : data(size, 42) {}
// Bad move constructor - leaves source in invalid state
BadMove(BadMove&& other) {
data = std::move(other.data); // Assignment, not initialization
// other.data is now empty, but object is still valid
}
};
class GoodMove {
private:
std::vector<int> data;
public:
GoodMove(size_t size) : data(size, 42) {}
// Good move constructor - uses initializer list
GoodMove(GoodMove&& other) noexcept : data(std::move(other.data)) {}
};
int main() {
BadMove b1(100);
BadMove b2 = std::move(b1); // Less efficient due to assignment
GoodMove g1(100);
GoodMove g2 = std::move(g1); // More efficient
return 0;
}Question 33
What is the relationship between exceptions and RAII?
Question 34
What is the most important consideration when choosing between copy and move for a class member?
Question 35
What happens when you don't declare a destructor but use smart pointers?
Question 36
What is the key benefit of using delegating constructors?
#include <iostream>
class Point {
private:
double x, y;
public:
Point(double x_val, double y_val) : x(x_val), y(y_val) {
std::cout << "Point constructed" << std::endl;
}
Point() : Point(0.0, 0.0) {} // Delegate to main constructor
Point(double x_val) : Point(x_val, 0.0) {} // Delegate with default y
};
int main() {
Point p1; // (0, 0)
Point p2(5.0); // (5, 0)
Point p3(1, 2); // (1, 2)
return 0;
}Question 37
What is the most important rule for implementing destructors in inheritance hierarchies?
Question 38
What is the difference between std::move and std::forward?
Question 39
What is the most common performance issue with copy constructors?
Question 40
What is the fundamental principle that guides when to implement custom constructors and destructors?
