C++ Interview Questions and Answers

Pointers, memory management, the STL, templates and modern C++.

Practise 10 random 3 peer-reviewed questions
C++ Interview Syllabus & Preparation Strategy

Whether you are preparing for entry-level C++ interview questions for freshers or senior software engineer interview questions addressing concurrency, scalability, and system architecture, this track provides peer-reviewed model answers with syntax walkthroughs, edge cases, and practical interview tips.

1 What is the difference between a pointer and a reference? Easy

A pointer is a variable that stores an address. It can be null, reassigned and used with pointer arithmetic. A reference is an alias for an existing object: it must be initialised when created, cannot be reseated, and is never null.

int a = 1, b = 2;
int* p = &a;    // p can point elsewhere
*p = 10;        // writes a
p = nullptr;    // allowed

int& r = a;     // r aliases a
r = b;          // assigns b's value into a; rebinding is impossible

Use references for parameters and return values when a value must exist; use pointers when "nothing" is a valid state, for optional output parameters, or for dynamic data structures. Prefer references by default for safety and clearer intent. const T& is the idiomatic way to pass large objects without copying. Note that references are usually implemented as pointers under the hood, and sizeof a reference gives the referenced type's size, not a pointer's.

2 How do stack and heap allocation differ in C++? Easy

The stack is a LIFO region managed automatically: allocation is a pointer bump, extremely fast, and memory is reclaimed when the scope ends. The heap is a large pool managed by new/delete or smart pointers; allocation is slower, can fragment, and you must free it.

void f() {
    int x = 5;                          // stack
    int* p = new int(5);                // heap
    std::unique_ptr<int> q =
        std::make_unique<int>(5);       // heap, RAII-managed
    delete p;                           // must free manually
}                                       // x and q cleaned automatically

Stack size is limited (often 1-8MB), so large arrays or deep recursion overflow it. Heap objects can outlive the scope that created them and support dynamic sizes.

Prefer stack allocation and RAII containers such as std::vector and std::string, and reach for the heap only when you need a lifetime beyond the scope, polymorphic ownership, or a runtime-determined size. This avoids leaks and fragmentation.

3 What do constructors and destructors do? Easy

A constructor runs when an object is created and initialises its members. A destructor runs when it is destroyed and releases resources. Constructors share the class name and have no return type; destructors are ~Class(), take no arguments and there is exactly one.

class Buffer {
public:
    explicit Buffer(std::size_t n)
        : data_(new char[n]), size_(n) {}

    ~Buffer() { delete[] data_; }

    Buffer(const Buffer&) = delete;
    Buffer& operator=(const Buffer&) = delete;

private:
    char* data_;
    std::size_t size_;
};

Prefer the member initialiser list over assignment in the constructor body. It initialises directly instead of default-constructing then assigning, and it is required for const and reference members. Initialisation order follows declaration order, not the order in the list, which can surprise you.

A destructor that reliably frees resources is what makes RAII work, so cleanup happens on scope exit and during stack unwinding from an exception.

Frequently Asked Questions About C++ Interviews

What do hiring managers evaluate in C++ technical rounds?

Technical interviewers look for foundational fluency, idiomatic syntax, clarity when communicating complex logic, and awareness of performance trade-offs (e.g. memory footprint, render performance, and network latency) in production environments.

What are the best interview tips for practicing C++ questions?

Use active recall: summarize each answer in your own words before revealing the model solution. Focus on explaining why a certain approach is chosen rather than just memorizing code syntax.