Tech

C++

C++ is a high-performance, systems-level language used in game engines, embedded systems, operating systems, high-frequency trading, and graphics rendering. It gives developers fine-grained control over memory and hardware. Interview questions focus on memory management (pointers, RAII, smart pointers), the Standard Template Library, object-oriented design, and performance optimization techniques that are impossible to achieve in higher-level languages.

What you get

Questions

20

Difficulty

3 levels

Answer Formats

2

Use the toggle on each card to move between an interview-ready answer and a simpler explanation. Questions are sorted from beginner to advanced, and the keywords are highlighted. You can also blur the answers to practice recalling them from memory.

Questions

Practice the answers out loud.

All topics

Question 1

What is C++ and how does it differ from C?

Beginner

How to answer in an interview

C++ is a general-purpose, compiled language that extends C by adding object-oriented programming features like classes, inheritance, and polymorphism, along with features like templates, exception handling, and the Standard Template Library. While C is purely procedural, C++ supports both procedural and object-oriented paradigms.

Question 2

Explain the core OOP concepts in C++.

Beginner

How to answer in an interview

C++ supports encapsulation through access specifiers like private and public, inheritance through deriving classes from base classes, polymorphism through virtual functions and operator overloading, and abstraction through abstract classes with pure virtual functions, hiding implementation details behind clean interfaces.

Question 3

What is a pointer, and how is it different from a reference?

Beginner

How to answer in an interview

A pointer is a variable that stores the memory address of another variable, can be reassigned to point elsewhere, and can be null. A reference is an alias for an existing variable, must be initialized when declared, cannot be null, and cannot be reassigned to refer to a different variable afterward.

Question 4

Explain constructors and destructors in C++.

Beginner

How to answer in an interview

A constructor is a special member function automatically called when an object is created, used to initialize its state, while a destructor is automatically called when an object goes out of scope or is deleted, used to release resources like memory or file handles. Together they manage an object's lifecycle, forming the basis of the RAII pattern.

Question 5

What is the difference between a struct and a class in C++?

Beginner

How to answer in an interview

In C++, structs and classes are functionally almost identical, both supporting member functions, inheritance, and constructors, but the key difference is the default access modifier: struct members default to public, while class members default to private. By convention, structs are typically used for simple data containers and classes for more complex objects with behavior.

Question 6

What is the difference between stack and heap memory allocation?

Intermediate

How to answer in an interview

Stack memory is automatically allocated and deallocated for local variables as functions are called and return, offering fast access but limited size. Heap memory is manually allocated using 'new' and deallocated using 'delete', allowing dynamic-sized data that persists beyond the scope it was created in, but it requires careful management to avoid memory leaks.

Question 7

What is operator overloading?

Intermediate

How to answer in an interview

Operator overloading allows developers to redefine the behavior of operators like +, -, or == for user-defined types, making custom classes behave more intuitively, such as allowing two custom Vector objects to be added directly using the '+' operator.

Question 8

Explain virtual functions and polymorphism in C++.

Intermediate

How to answer in an interview

A virtual function is declared in a base class and can be overridden in a derived class, enabling runtime polymorphism — when called through a base class pointer or reference, the derived class's version executes instead. This is implemented internally using a virtual table (vtable) that resolves the correct function at runtime.

Question 9

What is a pure virtual function and an abstract class?

Intermediate

How to answer in an interview

A pure virtual function is declared with '= 0' and has no implementation in the base class, forcing derived classes to provide their own implementation. A class containing at least one pure virtual function becomes an abstract class, which cannot be instantiated directly and serves as an interface-like base for derived classes.

Question 10

What is the difference between malloc/free and new/delete?

Intermediate

How to answer in an interview

malloc and free are C-style memory allocation functions that only allocate and deallocate raw memory without calling constructors or destructors. new and delete are C++ operators that allocate memory and also call the appropriate constructor or destructor, making them the preferred choice for allocating objects in C++.

Question 11

What is a memory leak and how do you avoid it in C++?

Intermediate

How to answer in an interview

A memory leak occurs when dynamically allocated memory is never freed because the pointer to it is lost or the delete call is forgotten, causing the program to consume increasing amounts of memory over time. It's avoided by always pairing new with delete, or better, using smart pointers like unique_ptr and shared_ptr which automatically manage memory through RAII.

Question 12

Explain templates in C++.

Intermediate

How to answer in an interview

Templates allow writing generic, type-independent code, such as a function or class that works with any data type, with the actual type specified when the template is used. This enables code reuse without duplicating logic for each type, and it's the foundation of the Standard Template Library's containers like vector and map.

Question 13

What is the difference between deep copy and shallow copy in C++?

Intermediate

How to answer in an interview

A shallow copy copies an object's member values directly, including pointer addresses, so both the original and copy end up pointing to the same underlying memory, which can cause issues like double deletion. A deep copy allocates new memory and copies the actual pointed-to data, ensuring the two objects are fully independent, typically implemented via a custom copy constructor.

Question 14

Explain const correctness in C++.

Intermediate

How to answer in an interview

Const correctness means using the 'const' keyword to explicitly indicate that a variable, parameter, or member function does not modify the object it's associated with, helping catch accidental modifications at compile time and clearly communicating intent, such as marking a getter method 'const' since it shouldn't alter object state.

Question 15

What is a friend function in C++?

Intermediate

How to answer in an interview

A friend function is a function that isn't a member of a class but is granted access to its private and protected members, declared using the 'friend' keyword inside the class. It's used sparingly when tight coupling between a function and a class's internals is necessary, such as for certain operator overloads.

Question 16

Explain the Standard Template Library (STL).

Intermediate

How to answer in an interview

The STL is a library of template-based generic classes and functions providing common data structures like vector, list, and map (containers), a uniform way to traverse them (iterators), and reusable algorithms like sort and find that operate generically on any compatible container.

Question 17

Explain smart pointers: unique_ptr and shared_ptr.

Advanced

How to answer in an interview

unique_ptr represents exclusive ownership of a dynamically allocated object, automatically deleting it when the unique_ptr goes out of scope, and it cannot be copied, only moved. shared_ptr allows multiple pointers to share ownership of an object using reference counting, automatically deleting the object once the last shared_ptr referencing it is destroyed.

Question 18

What is the diamond problem in multiple inheritance?

Advanced

How to answer in an interview

The diamond problem occurs when a class inherits from two classes that both inherit from a common base class, causing ambiguity about which copy of the base class's members to use. C++ resolves this using virtual inheritance, which ensures only a single shared instance of the common base class exists in the derived class.

Question 19

Explain the concept of RAII.

Advanced

How to answer in an interview

RAII, or Resource Acquisition Is Initialization, is a C++ idiom where resource management, such as memory, file handles, or locks, is tied to object lifetime — resources are acquired in a constructor and released in a destructor. This ensures resources are automatically cleaned up when an object goes out of scope, even if an exception occurs, reducing the risk of leaks.

Question 20

What are move semantics and rvalue references?

Advanced

How to answer in an interview

Move semantics, introduced in C++11, allow resources like heap-allocated memory to be transferred from a temporary or expiring object to another object instead of being expensively copied, using rvalue references denoted by '&&'. The std::move function casts an object to an rvalue reference, enabling the move constructor or move assignment operator to 'steal' its resources efficiently.

More in Tech

Keep browsing related topics.

Browse all