💻Technical12 min

C++ Interview Questions

C++ remains a placement favourite because it exposes what higher-level languages hide — pointers, manual memory management, and how object orientation maps to memory. Fresher C++ rounds concentrate on a predictable set: pointers versus references, dynamic memory, virtual functions and vtables, and the Standard Template Library. Precision matters here more than in most languages.

Pointers vs References

The most common C++ opener. Know the three or four concrete differences.

  • A pointer holds an address, can be reassigned, and can be null; a reference is an alias that must be initialised and cannot be reseated.
  • Pointers support arithmetic; references do not.
  • References are safer and preferred for parameter passing; pointers are needed for dynamic memory and optional values.
  • 'A reference is a constant pointer that is automatically dereferenced' is a useful mental model.

Memory Management — new/delete and Leaks

C++ gives manual control, so interviewers test whether you use it safely.

  • new allocates on the heap and returns a pointer; delete frees it; use delete[] for arrays.
  • A memory leak happens when heap memory is never freed; a dangling pointer points to freed memory.
  • Stack memory is automatic and freed on scope exit; heap memory is manual.
  • Modern C++ prefers smart pointers (unique_ptr, shared_ptr) and RAII over raw new/delete.

Virtual Functions & Polymorphism

This is where C++ OOP questions get deep. Understand the vtable mechanism.

  • A virtual function enables run-time polymorphism — the correct override is chosen by the object's actual type.
  • The compiler builds a vtable (virtual table) per class and a vptr per object to dispatch calls.
  • A base class with virtual functions should have a virtual destructor, or deleting via a base pointer leaks the derived part.
  • A pure virtual function (= 0) makes the class abstract.

The STL — Containers & Algorithms

Fresher rounds expect familiarity with the main containers and their complexities.

  • vector — dynamic array, O(1) amortised push_back, O(1) random access.
  • map / set — balanced BST, O(log n) operations, ordered.
  • unordered_map / unordered_set — hash table, O(1) average operations, unordered.
  • Know iterators and common algorithms (sort, find, lower_bound) — sort is O(n log n).

OOP Specifics in C++

Expect a couple of C++-flavoured OOP questions beyond the general concepts.

  • Constructors and destructors; the difference between deep and shallow copy.
  • The copy constructor and the assignment operator — when the compiler-generated ones are unsafe (owning raw pointers).
  • Function overloading and operator overloading are compile-time polymorphism.
  • The Rule of Three (and modern Rule of Five) for classes managing resources.

Common Interview Questions & Answers

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

A pointer is a variable that stores a memory address; it can be null, reassigned to point elsewhere, and supports pointer arithmetic. A reference is an alias for an existing variable; it must be initialised when declared, cannot be made to refer to a different variable afterward, and cannot be null. References are therefore safer and preferred for passing parameters, while pointers are needed for dynamic allocation and cases where 'no value' must be representable.

The 'must be initialised and cannot be reseated' line is the crux — say it explicitly.

Q2. What is a virtual function and how does it work internally?

A virtual function allows run-time polymorphism: when called through a base-class pointer or reference, the version matching the object's actual derived type is invoked. Internally the compiler creates a virtual table (vtable) of function pointers for each class with virtual functions, and each object stores a hidden pointer (vptr) to its class's vtable, so the call is dispatched at run time via that table.

Explaining the vtable/vptr mechanism is what separates a memorised answer from a real one.

Q3. What is a memory leak and how do you prevent it?

A memory leak occurs when heap memory allocated with new is never released with delete, so the program's memory usage grows until it is exhausted. You prevent it by pairing every new with a delete, by following RAII so resources are freed in destructors, and — in modern C++ — by using smart pointers like unique_ptr and shared_ptr that free memory automatically when they go out of scope.

Mentioning RAII and smart pointers signals modern C++ knowledge, not just C-style habits.

Q4. Why should a base class destructor be virtual?

If you delete a derived object through a base-class pointer and the base destructor is not virtual, only the base part is destroyed — the derived destructor never runs, leaking its resources and causing undefined behaviour. Declaring the base destructor virtual ensures the full destructor chain runs from derived to base. Any class intended to be used polymorphically as a base should have a virtual destructor.

This is a favourite trap; volunteering it unprompted impresses interviewers.

Q5. What is the difference between map and unordered_map?

map is implemented as a balanced binary search tree, so it keeps keys sorted and offers O(log n) insertion, lookup and deletion. unordered_map is a hash table, so it does not maintain order but offers O(1) average-case operations, degrading to O(n) with many hash collisions. Choose map when you need ordering or range queries, and unordered_map when you only need fast key lookups.

State both the ordering difference and the complexity difference — interviewers want both.

Common Mistakes to Avoid

Saying a reference can be null or reassigned

Forgetting delete[] for array allocations, or omitting delete entirely

Not knowing the vtable/vptr mechanism behind virtual functions

Missing the virtual-destructor rule for polymorphic base classes

Confusing map (ordered, O(log n)) with unordered_map (hashed, O(1) average)

Expert Tips

Prepare the pointer-vs-reference answer until it is automatic — it opens most rounds

Learn the STL container complexities as a small table you can recall instantly

Mention RAII and smart pointers whenever memory comes up — it signals modern C++

Be ready to write a small class with constructor, destructor and copy constructor

Pre-Interview Checklist

6 items

Frequently Asked Questions

Is C or C++ asked more in placements?

C++ is more common because it covers both procedural and object-oriented questions, but C-style pointer and memory questions overlap heavily. Prepare pointers, memory and OOP and you cover both.

Do I need to know modern C++ (11/14/17)?

Knowing smart pointers, auto, range-based for loops and move semantics at a basic level is a strong plus, but fresher rounds still centre on pointers, memory, virtual functions and the STL.

How much STL detail is expected?

Know the common containers (vector, map, set, unordered_map) and their complexities, plus sort and find. Deep internals of every algorithm are not expected for freshers.

🎯

Ready to ace your next interview?

Practice with SpeakWell AI. Upload your resume → get resume-based questions → practice with AI interviewers → improve communication → track progress → get instant AI feedback.

Back to all guides