How to Pass a C++ Interview: Top Questions and Answers

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

How to Pass a C++ Interview: Top Questions with Code Examples



A C++ interview is one of the toughest challenges for a developer. This language combines the power of low-level programming with modern OOP and template paradigms. To successfully pass an interview, you need not just to know the syntax, but also to understand how memory, the compiler, and the standard library work. In this article, we will break down the top 10 questions most frequently asked in C++ interviews, with detailed answers and code examples.



1. What is RAII and why is it needed?

RAII (Resource Acquisition Is Initialization) is one of the key idioms of C++. The essence is simple: a resource (memory, file, mutex) is acquired in the constructor of an object and released in the destructor. This guarantees proper cleanup even in the case of exceptions.



Example of incorrect code (without RAII):

void process() {    int* ptr = new int(42);    // ... code that might throw an exception    delete ptr; // leak if exception occurred before delete}


Example with RAII (using std::unique_ptr):

#include <memory>

void process() { std::unique_ptr<int> ptr = std::make_unique<int>(42); // ... code with exceptions // ptr is automatically deleted when it goes out of scope}


Why this is asked: RAII is the foundation of safe resource management in C++. Without it, writing reliable code is impossible.



2. What is the difference between a virtual function and a pure virtual function?

A virtual function (virtual) can be overridden in a derived class but has an implementation in the base class. A pure virtual function (= 0) has no implementation in the base class and makes the class abstract — you cannot create an object of such a class.



Example:

class Base {public:    virtual void foo() { std::cout << "Base foo"; }   // virtual    virtual void bar() = 0;                               // pure virtual};

class Derived : public Base {public: void foo() override { std::cout << "Derived foo"; } void bar() override { std::cout << "Derived bar"; }};


Follow-up question: How does vtable work? The compiler creates a virtual function table for each class with virtual methods. Each object contains a pointer to this table (vptr). When a virtual function is called, dynamic dispatch occurs through the vtable.



3. What smart pointers are there in C++ and when to use them?

In C++11, three types of smart pointers appeared:

  • std::unique_ptr — exclusive ownership. Cannot be copied, only moved. Ideal for resources with a single owner.
  • std::shared_ptr — shared ownership. Uses a reference counter. Suitable when multiple objects need to own a single resource.
  • std::weak_ptr — does not own the resource but can observe a shared_ptr. Needed to break circular references.


Example of using unique_ptr:

#include <memory>

class MyClass { /* ... */ };

void example() { auto ptr = std::make_unique<MyClass>(); // auto ptr2 = ptr; // Error! Cannot copy auto ptr3 = std::move(ptr); // OK, move}


Example of shared_ptr and weak_ptr to prevent circular references:

struct Node {    std::shared_ptr<Node> next;    std::weak_ptr<Node> prev; // weak_ptr to avoid a cycle};


4. What is move semantics and std::move?

Move semantics allows "moving" resources from one object to another instead of copying. This is especially useful for temporary objects or when copying is expensive (e.g., std::string or

Blogs

Book Recommendations