Programming Languages

C++ interview questions

C++ interviews often probe a candidate's deep understanding of memory management, object-oriented principles, template metaprogramming, and the Standard Template Library, alongside practical problem-solving skills. Interviewers look for proficiency in writing efficient, robust, and idiomatic C++ code, especially concerning resource management and performance.

15 questions (2 easy · 10 medium · 3 hard), each with what a strong answer covers and where people lose the point. Free to read, no account.

On this page (15 questions)
  1. 1.Explain the key differences between pointers and references in C++. When would you choose one over the other?
  2. 2.Describe the various ways the `const` keyword can be used in C++. Provide examples for each.
  3. 3.Compare and contrast stack and heap memory in C++. When would you use one over the other?
  4. 4.What are virtual functions in C++? How do they enable polymorphism, and what is the 'virtual table'?
  5. 5.Explain the Resource Acquisition Is Initialization (RAII) idiom in C++. Provide an example of how it's used.
  6. 6.Compare `std::unique_ptr` and `std::shared_ptr`. When would you use each, and what problem does `std::weak_ptr` solve?
  7. 7.What are lvalues and rvalues in C++? How do move semantics and rvalue references improve performance?
  8. 8.Explain the concept of templates in C++. When would you use template specialization?
  9. 9.Compare `std::vector` and `std::list` in terms of their underlying data structures, performance characteristics, and typical use cases.
  10. 10.What is a pure virtual function? How does it relate to abstract classes, and why would you use them?
  11. 11.Explain operator overloading in C++. What are the rules and best practices for overloading operators?
  12. 12.What is the Rule of Three/Five/Zero in C++? Why is it important for classes managing resources?
  13. 13.Describe the different meanings and uses of the `static` keyword in C++.
  14. 14.How do you handle errors in C++? Discuss exceptions, their benefits, and best practices.
  15. 15.Explain the purpose and dangers of `const_cast` and `reinterpret_cast` in C++.

1.Explain the key differences between pointers and references in C++. When would you choose one over the other?

Warm-up

What a strong answer covers

  • Define pointers as variables holding memory addresses, allowing null values and reseating.
  • Define references as aliases to existing objects, requiring initialization and disallowing null or reseating.
  • Discuss use cases: Pointers for dynamic memory, optional parameters, C-style arrays; References for function parameters (pass-by-reference), operator overloading, avoiding copies.
  • Mention safety aspects: References are generally safer due to non-null and non-reseatable properties.

Where people lose the point

  • Confusing syntax or claiming references can be null or reseated.
  • Failing to mention the performance implications (avoiding copies) for references.
  • Not discussing the 'ownership' aspect often associated with raw pointers vs. non-owning references.
Link to this question

2.Describe the various ways the `const` keyword can be used in C++. Provide examples for each.

Core

What a strong answer covers

  • Explain `const` with variables (compile-time constant, read-only).
  • Discuss `const` with pointers: pointer to `const` data (`const int* p`), `const` pointer (`int* const p`), and `const` pointer to `const` data (`const int* const p`).
  • Detail `const` with function parameters (pass-by-const-reference/pointer to prevent modification).
  • Explain `const` member functions (guaranteeing the function won't modify the object's state) and `mutable` keyword.

Where people lose the point

  • Incorrectly distinguishing between `const int* p` and `int* const p`.
  • Forgetting to mention `const` member functions and their importance for `const` objects.
  • Not explaining the 'bitwise constness' vs 'logical constness' concept implicitly.
Link to this question

3.Compare and contrast stack and heap memory in C++. When would you use one over the other?

Warm-up

What a strong answer covers

  • Define stack memory: automatic allocation/deallocation for local variables, function calls; fixed size, fast access.
  • Define heap memory: dynamic allocation/deallocation via `new`/`delete`; flexible size, slower access, potential for fragmentation.
  • Discuss use cases: Stack for small, fixed-size, short-lived data; Heap for large, variable-size, long-lived data (e.g., objects whose lifetime extends beyond function scope).
  • Mention risks: Stack overflow, heap memory leaks, fragmentation, dangling pointers.

Where people lose the point

  • Confusing the allocation/deallocation mechanisms for stack vs. heap.
  • Failing to mention the speed difference or size limitations.
  • Not connecting heap memory to `new`/`delete` or smart pointers.
Link to this question

4.What are virtual functions in C++? How do they enable polymorphism, and what is the 'virtual table'?

Core

What a strong answer covers

  • Define virtual functions as member functions declared with the `virtual` keyword in a base class, allowing derived classes to override them.
  • Explain how they enable runtime polymorphism (dynamic dispatch) when calling a function through a base class pointer or reference.
  • Describe the 'virtual table' (vtable) as a mechanism used by the compiler to resolve virtual function calls at runtime.
  • Discuss the overhead associated with virtual functions (vtable lookup, increased object size).

Where people lose the point

  • Confusing compile-time (static) polymorphism with runtime (dynamic) polymorphism.
  • Incorrectly stating that virtual functions work with objects directly (they require pointers/references).
  • Failing to mention the performance implications or the concept of object slicing without pointers/references.
Link to this question

5.Explain the Resource Acquisition Is Initialization (RAII) idiom in C++. Provide an example of how it's used.

Core

What a strong answer covers

  • Define RAII: a programming idiom where resource acquisition is tied to object initialization, and resource release is tied to object destruction.
  • Explain its purpose: to guarantee proper resource management (e.g., memory, file handles, mutexes) even in the presence of exceptions.
  • Provide an example using `std::unique_ptr` or `std::lock_guard` to demonstrate automatic resource cleanup.
  • Discuss how constructors acquire resources and destructors release them.

Where people lose the point

  • Only mentioning memory management and not other resources like file handles or locks.
  • Failing to emphasize the 'guarantee' of cleanup even with exceptions.
  • Not providing a concrete C++ example of RAII in action.
Link to this question

6.Compare `std::unique_ptr` and `std::shared_ptr`. When would you use each, and what problem does `std::weak_ptr` solve?

Core

What a strong answer covers

  • Describe `std::unique_ptr`: exclusive ownership, non-copyable (movable), lightweight, ideal for single ownership.
  • Describe `std::shared_ptr`: shared ownership, reference counting, copyable, resource released when last `shared_ptr` goes out of scope.
  • Discuss use cases: `unique_ptr` for local scope, factory functions returning ownership; `shared_ptr` for multiple owners, complex object graphs.
  • Explain `std::weak_ptr`: non-owning observer, used with `shared_ptr` to break circular references and prevent memory leaks.

Where people lose the point

  • Confusing the ownership semantics or copyability of `unique_ptr` and `shared_ptr`.
  • Not explaining *why* `weak_ptr` is needed (circular references).
  • Failing to mention the overhead of `shared_ptr` (reference count, control block).
Link to this question

7.What are lvalues and rvalues in C++? How do move semantics and rvalue references improve performance?

Hard

What a strong answer covers

  • Define lvalues as expressions that refer to a persistent object (e.g., named variables, `*p`).
  • Define rvalues as expressions that refer to a temporary object or value (e.g., literals, function return values, `x+y`).
  • Explain move semantics: transferring resources from a temporary (rvalue) object to a new object instead of deep copying.
  • Describe rvalue references (`&&`) as the mechanism to bind to rvalues, enabling move constructors and move assignment operators, and how `std::move` facilitates this.

Where people lose the point

  • Incorrectly identifying lvalues and rvalues in examples.
  • Claiming `std::move` actually moves data, rather than just casting to an rvalue reference.
  • Not explaining the performance benefit (avoiding deep copies) or the 'valid but unspecified' state of moved-from objects.
Link to this question

8.Explain the concept of templates in C++. When would you use template specialization?

Core

What a strong answer covers

  • Define templates as a feature for generic programming, allowing functions and classes to operate on generic types.
  • Discuss function templates and class templates, providing simple examples for each.
  • Explain template specialization (full and partial) as a way to provide a different implementation for specific types or categories of types.
  • Provide scenarios for specialization: performance optimization for specific types, handling types that don't fit the generic implementation, or providing specific behavior for pointer types.

Where people lose the point

  • Confusing templates with polymorphism (runtime vs. compile-time).
  • Failing to explain *why* specialization is needed (e.g., generic code doesn't work or isn't optimal for a specific type).
  • Not distinguishing between full and partial specialization.
Link to this question

9.Compare `std::vector` and `std::list` in terms of their underlying data structures, performance characteristics, and typical use cases.

Core

What a strong answer covers

  • Describe `std::vector`: dynamic array, contiguous memory, O(1) random access, O(N) insertion/deletion in middle, O(1) amortized push_back.
  • Describe `std::list`: doubly linked list, non-contiguous memory, O(N) random access, O(1) insertion/deletion anywhere (given iterator).
  • Discuss memory locality and cache performance: `vector` generally better due to contiguous memory.
  • Recommend use cases: `vector` for random access, frequent appending; `list` for frequent insertions/deletions in the middle, stable iterators.

Where people lose the point

  • Incorrectly stating random access complexity for `std::list`.
  • Failing to mention iterator invalidation rules for each container.
  • Not discussing memory overhead or cache performance implications.
Link to this question

10.What is a pure virtual function? How does it relate to abstract classes, and why would you use them?

Core

What a strong answer covers

  • Define a pure virtual function as a virtual function declared with `= 0` in the base class, having no implementation.
  • Explain that a class containing at least one pure virtual function becomes an abstract class, which cannot be instantiated directly.
  • Discuss the purpose: to define an interface or contract that derived classes *must* implement.
  • Provide use cases: creating an interface, enforcing a common behavior across a hierarchy, designing frameworks.

Where people lose the point

  • Confusing pure virtual functions with regular virtual functions.
  • Claiming an abstract class can be instantiated.
  • Not clearly explaining the 'contract' or 'interface' aspect.
Link to this question

11.Explain operator overloading in C++. What are the rules and best practices for overloading operators?

Core

What a strong answer covers

  • Define operator overloading as giving special meaning to operators when applied to user-defined types.
  • Discuss common operators that can be overloaded (e.g., `+`, `-`, `*`, `[]`, `<<`, `>>`).
  • Explain rules: cannot overload operators for built-in types, cannot create new operators, precedence/associativity remain unchanged.
  • Outline best practices: maintain natural semantics, return by value for arithmetic, return by reference for assignment, use non-member functions for binary operators where possible.

Where people lose the point

  • Attempting to overload operators like `.` or `::`.
  • Not adhering to natural semantics, leading to confusing code.
  • Incorrectly choosing between member and non-member functions for overloading.
Link to this question

12.What is the Rule of Three/Five/Zero in C++? Why is it important for classes managing resources?

Hard

What a strong answer covers

  • Explain the Rule of Three (C++98): If you define a custom destructor, copy constructor, or copy assignment operator, you likely need to define all three.
  • Extend to Rule of Five (C++11): With move semantics, if you define any of the above, you should also define a move constructor and move assignment operator.
  • Discuss the rationale: These special member functions handle resource management (e.g., dynamic memory) and ensure correct behavior during copying, moving, and destruction.
  • Introduce the Rule of Zero (modern C++): Prefer to avoid defining any of these by using RAII and smart pointers, letting the compiler generate them or explicitly deleting them.

Where people lose the point

  • Only mentioning the Rule of Three and not the Rule of Five/Zero.
  • Failing to connect the rule directly to resource management.
  • Not explaining *why* these functions are needed together (e.g., deep copy vs. shallow copy issues).
Link to this question

13.Describe the different meanings and uses of the `static` keyword in C++.

Core

What a strong answer covers

  • Explain `static` with local variables: retains value between function calls (static storage duration).
  • Discuss `static` with global variables/functions: limits scope to the current translation unit (internal linkage).
  • Detail `static` with class members (data members): shared by all objects of the class, belongs to the class itself.
  • Explain `static` with class members (member functions): can be called without an object, cannot access non-static members.

Where people lose the point

  • Confusing the meaning of `static` in different contexts (e.g., local vs. class member).
  • Incorrectly stating that static member functions can access non-static data.
  • Failing to mention the 'internal linkage' aspect for global static variables/functions.
Link to this question

14.How do you handle errors in C++? Discuss exceptions, their benefits, and best practices.

Core

What a strong answer covers

  • Discuss traditional error handling methods: return codes, `errno`, `assert` (for debug).
  • Explain exceptions: a mechanism to signal and handle runtime errors that cannot be handled locally.
  • Describe `try`, `catch`, `throw` keywords and their roles in exception handling.
  • Outline benefits: separation of error-handling code from normal logic, propagation of errors up the call stack, handling of constructor failures.
  • Mention best practices: throw by value, catch by const reference, use RAII to prevent resource leaks, avoid throwing exceptions from destructors.

Where people lose the point

  • Not mentioning RAII as a critical component for exception safety.
  • Incorrectly stating that exceptions are always faster than return codes.
  • Failing to discuss the overhead of exceptions or when not to use them (e.g., expected errors).
Link to this question

15.Explain the purpose and dangers of `const_cast` and `reinterpret_cast` in C++.

Hard

What a strong answer covers

  • Define `const_cast`: used to add or remove `const` or `volatile` qualifiers from a pointer or reference.
  • Explain its primary use case: calling a non-`const` function on a `const` object when you know it won't actually modify the underlying data, or when interacting with legacy APIs.
  • Define `reinterpret_cast`: converts any pointer type to any other pointer type, or an integer type to any pointer type and vice-versa.
  • Explain its purpose: low-level type punning, converting between unrelated types, often used for hardware interaction or specific memory layouts.
  • Discuss dangers: `const_cast` can lead to undefined behavior if used to modify an object originally declared `const`; `reinterpret_cast` offers no type safety and can easily lead to undefined behavior, alignment issues, or security vulnerabilities.

Where people lose the point

  • Confusing the specific use cases or limitations of each cast.
  • Understating the severe dangers and potential for undefined behavior.
  • Not mentioning that `const_cast` can only modify `const` or `volatile` qualifiers, not other types.
Link to this question
No account needed

Answer one real C++ question now

A question a C++ panel actually asks, answered out loud, scored on what you said and how you said it. Under two minutes, and nothing to sign up for.

Explain the key differences between pointers and references in C++. When would you choose one over the other?

We never store the audio. Your answer is deleted within 24 hours unless you save the result.

How C++ answers get judged

The weights a C++ interviewer is holding, whether or not they say so out loud. Round Zero scores your practice answers against exactly these, and quotes your own words back as the evidence for each.

Technical Correctness

30%

The accuracy of C++ concepts, syntax, and standard library usage. Answers should be free of factual errors.

Conceptual Depth

25%

Demonstrates a deep understanding of underlying mechanisms (e.g., vtables, memory layout, template instantiation) and not just surface-level definitions.

Idiomatic C++ & Best Practices

20%

Ability to discuss and apply modern C++ idioms (e.g., RAII, smart pointers, move semantics) and best practices for writing robust, efficient, and maintainable code.

Problem Solving & Application

15%

Ability to apply C++ knowledge to solve practical problems, choose appropriate constructs (e.g., container, smart pointer), and discuss trade-offs.

Clarity & Communication

10%

Ability to articulate complex C++ concepts clearly, concisely, and logically, using appropriate technical terminology.

Related Programming Languages skills

All skills →

Now say them out loud

You have read what strong C++ answers contain. The next thing that moves the needle is producing one under time, out loud, and finding out where it falls apart.

  • These questions asked back, with follow-ups
  • Flashcards for the ones you keep missing
  • A scored mock that quotes your own answers

Browse every skill

Practising C++: common questions

What C++ interview questions should I practice?
Start with the core areas C++ interviewers probe: Explain the key differences between pointers and references in C++. When would you choose one over the other; Describe the various ways the `const` keyword can be used in C++. Provide examples for each.; Compare and contrast stack and heap memory in C++. When would you use one over the other. This page outlines strong answers and common mistakes, and the scored path drills each one with follow-ups.
Is the C++ practice free?
Yes. The C++ path runs free inside Round Zero: lessons, practice questions and flashcards. Drills are unlimited on every plan, free included. So is the full scorecard. Free also covers 3 complete scored interviews, no card.
How is this different from a C++ question list?
A static list gives you questions with no feedback. Round Zero runs a live scored practice that probes your actual answers, rotates difficulty, and tells you exactly what to fix, grounded in a C++ rubric.
How should I prepare for a C++ interview?
Learn the concepts, drill the questions until answers come fast, then prove it in a scored mock. Round Zero sequences all three so you know you are ready, not just that you read about C++.
How is a C++ answer scored?
C++ answers are scored on technical correctness, conceptual depth, idiomatic c++ & best practices, problem solving & application, clarity & communication, with evidence quoted from what you actually said, so feedback is specific instead of generic praise.