Interview Prep12 min read

Top C++ Interview Questions with Answers (2026)

C++ interviews test whether you understand what the machine is actually doing - how memory is laid out, when objects are created and destroyed, and how pointers and references differ. Service-company placement tests lean on OOP and the STL; systems and product roles push on memory management and modern C++. Here are the questions that come up again and again, grouped by area, each with a concise answer.

A laptop showing a C++ code editor with pointer arrows, memory blocks and angle-bracket templates, representing systems programming

Pointers, references & memory

Pointer vs reference?

A pointer is a variable holding an address; it can be null, reassigned, and does pointer arithmetic. A reference is an alias for an existing object; it must be initialised, can't be null or rebound. Prefer references for function parameters unless you need reseating or nullability.

Stack vs heap?

Stack memory is automatic - local variables allocated and freed as functions enter and exit, fast but limited in size. Heap memory is allocated manually with new (or a container) and lives until you free it, larger but slower and your responsibility to manage.

What is a memory leak and a dangling pointer?

A memory leak is heap memory you allocated but never freed, so it's lost until the process ends. A dangling pointer points to memory that has already been freed - dereferencing it is undefined behaviour. RAII and smart pointers prevent both.

What is RAII?

Resource Acquisition Is Initialisation: tie a resource's lifetime to an object's scope, so acquiring it happens in the constructor and releasing it in the destructor. When the object goes out of scope the resource is freed automatically - even if an exception is thrown. It's the foundation of safe modern C++.

OOP in C++

What are the four pillars of OOP in C++?

Encapsulation (bundling data with methods and hiding internals via access specifiers), inheritance (deriving a class from a base), polymorphism (virtual functions dispatching at runtime), and abstraction (exposing behaviour through interfaces/abstract classes). Give a one-line example of each.

How do virtual functions work?

A class with virtual functions gets a vtable - a table of function pointers - and each object holds a hidden vptr to it. A call through a base pointer or reference looks up the actual object's function at runtime, enabling runtime polymorphism.

Why does a base class need a virtual destructor?

So that deleting a derived object through a base-class pointer calls the derived destructor first. Without a virtual destructor that's undefined behaviour and the derived part leaks. Any class meant to be a polymorphic base should have a virtual destructor.

What is the Rule of Three/Five?

If a class manages a resource and you define any of the destructor, copy constructor, or copy assignment, you almost certainly need all three (Rule of Three). Modern C++ adds the move constructor and move assignment (Rule of Five). The 'Rule of Zero' says: prefer types that need none of them by using smart pointers and containers.

Function overloading vs overriding?

Overloading is multiple functions with the same name but different parameters, resolved at compile time. Overriding is a derived class redefining a base class's virtual function with the same signature, resolved at runtime.

The STL

vector vs list vs deque?

vector is a dynamic array - contiguous memory, O(1) random access, cheap push_back, costly inserts in the middle. list is a doubly linked list - O(1) insert/erase anywhere given an iterator but no random access. deque allows O(1) push/pop at both ends. Default to vector.

map vs unordered_map?

map is a balanced binary search tree - keys kept sorted, O(log n) operations. unordered_map is a hash table - average O(1) operations but no ordering. Use map when you need sorted iteration, unordered_map when you just need fast lookups.

What are iterators?

Iterators are objects that generalise pointers to traverse a container. They come in categories (input, output, forward, bidirectional, random-access) and let STL algorithms like sort and find work uniformly across containers.

What is iterator invalidation?

Modifying a container can invalidate iterators pointing into it. For example, a vector's reallocation on growth invalidates all iterators; erasing from a vector invalidates iterators at and after the erase point. Using an invalidated iterator is undefined behaviour.

Modern C++

unique_ptr vs shared_ptr?

unique_ptr is a smart pointer with sole ownership - it can't be copied, only moved, and frees its object automatically. shared_ptr allows shared ownership via reference counting and frees the object when the last owner goes away. Prefer unique_ptr; reach for shared_ptr only when ownership is genuinely shared.

What are move semantics and rvalue references?

An rvalue reference (T&&) binds to temporaries, letting a move constructor steal a temporary's resources (like a heap buffer) instead of copying them. This makes returning and passing large objects cheap. std::move casts an lvalue to an rvalue so it can be moved from.

What does const do, and what is a const member function?

const marks something as not modifiable - a const variable, a pointer to const data, or a const reference parameter that promises not to change the argument. A const member function promises not to modify the object's state and can be called on const objects.

What is the difference between C and C++?

C is procedural; C++ adds object-oriented and generic programming (classes, templates), the STL, references, function/operator overloading, exceptions, and RAII. C++ is largely a superset of C but encourages higher-level abstractions with zero-overhead where possible.

How to actually get ready

Reading these answers builds recognition, but interviews test recall under pressure. Practise retrieving, not re-reading: take timed MCQ rounds on pointers/memory and OOP first, because that's where C++ interviewers dig deepest, then read the explanation for anything you miss and revisit weak spots the next day.

C++ interviews rarely stop at the language. Expect data-structures and algorithms questions solved in C++, plus operating-system and DBMS basics in campus placements, so interleave those so you're ready when the interview shifts from 'what is a vtable' to 'now code this on a whiteboard'.

The bottom line

Now go test yourself

C++ interviews reward understanding over memorisation. If you can reason about where memory lives, explain object lifetimes and virtual dispatch, and pick the right STL container, you'll handle the follow-ups that trip up people who only skimmed a question bank.

The fastest way to find out whether you truly understand C++ - rather than just recognise the terms - is to be tested on it. Start a C++ quiz, answer from memory, and let the explanations catch every gap before an interviewer does.

FAQs

Frequently asked questions

What topics should freshers focus on for a C++ interview?

Pointers and references, stack versus heap and memory management, OOP (virtual functions, the Rule of Three/Five), the STL containers, and modern C++ (smart pointers, move semantics, RAII). Add data structures and algorithms for placement rounds.

Is the STL important for C++ interviews?

Very. Be ready to compare vector, list and deque, explain map versus unordered_map, and discuss iterators and iterator invalidation. Interviewers often ask which container you'd pick for a given problem and why.

Do I need modern C++ for fresher interviews?

Increasingly, yes. Smart pointers (unique_ptr, shared_ptr), move semantics and rvalue references, RAII and const-correctness now come up even in fresher interviews, so don't limit yourself to legacy C++.

What is the difference between a pointer and a reference in C++?

A pointer holds an address, can be null, reassigned, and does arithmetic. A reference is an alias that must be initialised and can't be null or rebound. Use references for cleaner parameter passing and pointers when you need nullability or reseating.

Why is a virtual destructor important?

Deleting a derived object through a base-class pointer without a virtual destructor is undefined behaviour and leaks the derived part. Any polymorphic base class - one with virtual functions - should declare a virtual destructor.

Are these C++ questions enough to clear placement tests?

They cover the language portion well. Most placement tests also include aptitude, logical reasoning, data structures and sometimes DBMS or OS, so pair C++ practice with those sections for full coverage.

How many C++ interview questions should I practise?

Quality beats quantity. Master the core questions above - memory, OOP, the STL and modern C++ - until you can answer each from memory, then drill them as timed MCQs and review every miss.

Related quizzes

Put it into practice

Keep reading

Related articles

Browse all articles →

Test yourself in two minutes

Six adaptive questions, every answer explained by an AI tutor. Free.

▶ Start an AI quiz