57 C++ interview questions with answers
57 C++ interview questions with detailed answers, sorted from basic to advanced. Free to practice, with progress saved automatically.
All C++ interview questions
What are the most important differences between C and C++?
What is a reference in C++, and how does it differ from a pointer?
What is the difference between `new` and `malloc`?
What is the difference between stack and heap memory in C++?
What is an inline function in C++? When should you use it?
What are function overloading and operator overloading in C++?
What is a dangling pointer in C++? How do you avoid it?
What is RAII in C++ and why does it matter?
How do `unique_ptr`, `shared_ptr`, and `weak_ptr` differ in C++11?
What is the difference between shallow copy and deep copy in C++? When is deep copy needed?
What is the difference between `const int*`, `int* const`, and `const int* const`?
What are the four pillars of OOP in C++?
What is the difference between a virtual function and a pure virtual function?
How does the vtable (virtual table) work in C++?
What is the diamond problem in C++? How is it solved?
When are constructors and destructors called? What is the order in inheritance?
What is the STL in C++? What are its main components?
How does `std::vector` work internally? What is the difference between `size()` and `capacity()`?
What is the difference between `std::map` and `std::unordered_map`? When should you use each?
What are iterators in the STL? How many categories are there?
When should you use `std::vector` vs `std::list`?
What is a lambda expression in C++11? How does the capture clause work?
What are move semantics and rvalue references (`&&`) in C++11? Why do they matter?
How are the `auto` keyword and range-based for loop used in C++11?
How does `nullptr` in C++11 differ from `NULL` and `0`?
What are the Rule of Three and Rule of Five in C++?
What are templates in C++? How do function templates and class templates differ?
What is `constexpr` in C++11/14? How does it differ from `const`?
What are structured bindings in C++17? Give a practical example.
What is undefined behavior (UB) in C++? What are common examples?
When do memory leaks occur in C++? How do you detect and prevent them?
When should you use each of the four C++ casts: `static_cast`, `dynamic_cast`, `reinterpret_cast`, `const_cast`?
What is the `explicit` keyword used for in C++?
What is a const member function in C++? Why is it important?
What are `friend` functions and `friend` classes in C++? When should you use them?
What is a static member in C++? How does it differ from a regular member?
How are the key STL algorithms `std::sort`, `std::find`, `std::accumulate`, and `std::transform` used?
What is the difference between `std::set` and `std::multiset`? When should you use each?
How does exception handling with `try`/`catch`/`throw` work in C++? What is `noexcept`?
What is `std::optional<T>` in C++17? When should you use it instead of a pointer or sentinel value?
What is `std::variant<Types...>` in C++17? How does it differ from a `union`?
What are copy elision, RVO, and NRVO in C++? Why do they matter?
What is the difference between `std::move` and `std::forward`? What is perfect forwarding?
What is `std::string_view` in C++17? What are its advantages over `const std::string&`?
What is `std::initializer_list<T>` in C++11? How does it interact with constructors?
How do you use `std::thread` and `std::mutex` in C++11 for thread creation and shared data protection?
What is the difference between pass-by-value, pass-by-reference, and pass-by-pointer in C++? When should you use each?
What are namespaces in C++? Why should you avoid `using namespace std;` in headers?
What is the Pimpl idiom (Pointer to Implementation) in C++? What are its benefits?
What are `std::stack`, `std::queue`, and `std::priority_queue`? What are their structures and complexities?
What is the difference between `struct` and `class` in C++?
`const` vs `#define` — which should you use, and why?
How does compile-time polymorphism differ from runtime polymorphism?
Why should a base class destructor be `virtual`?
What is the `volatile` keyword used for?
What happens if you call a virtual function inside a constructor/destructor?
What is the difference between `delete` and `delete[]`?