All interview guides

C++ Interview Questions and Answers

24 questions that come up in C++ technical interviews, each with the answer and an explanation of why it is right.

Test yourself — 90 question bank

1. What is C++?

Beginner

Answer: General-purpose programming language with low-level access

C++ is a general-purpose programming language created by Bjarne Stroustrup. It provides low-level memory access with high-level abstractions and object-oriented features.

2. What is move semantics?

Advanced

Answer: Transfers resources instead of copying

Move semantics transfers resources instead of copying. Uses rvalue references (&&). Move constructor and move assignment operator. Improves performance for temporary objects.

3. What is STL?

Intermediate

Answer: Standard Template Library with containers, algorithms, iterators

STL (Standard Template Library) provides containers (vector, map, set), algorithms (sort, find), and iterators. Built on templates for type safety and efficiency.

4. What is a template?

Intermediate

Answer: Blueprint for generic programming

Templates enable generic programming. Define functions/classes that work with any type. Compiler generates type-specific code. Two types: function templates and class templates.

5. What is an rvalue reference?

Advanced

Answer: Reference to temporary object (&&)

Rvalue reference (&&) binds to temporary objects (rvalues). Enables move semantics and perfect forwarding. Distinguishes from lvalue references (&). C++11 feature.

6. What is the difference between C and C++?

Beginner

Answer: C++ adds OOP, templates, STL to C

C++ extends C with object-oriented programming, templates, STL, better type checking, function overloading, references, and many modern features.

7. What is vector?

Intermediate

Answer: Dynamic array that grows/shrinks automatically

vector is a dynamic array that automatically grows/shrinks. Contiguous memory. Random access. Use push_back(), pop_back(), size(). Include <vector>.

8. What is the entry point of a C++ program?

Beginner

Answer: main()

The main() function is the entry point of every C++ program. Execution starts from main(). Returns int, typically 0 for success.

9. What is std::move?

Advanced

Answer: Casts lvalue to rvalue reference

std::move casts lvalue to rvalue reference, enabling move semantics. Doesn't actually move anything. Indicates object can be moved from. Use with caution.

10. What is perfect forwarding?

Advanced

Answer: Forwards arguments preserving value category

Perfect forwarding preserves argument value category (lvalue/rvalue). Uses forwarding references (T&&) and std::forward. Essential for generic code, wrappers.

11. What is map?

Intermediate

Answer: Associative container of key-value pairs

map is an associative container storing sorted key-value pairs. Implemented as red-black tree. Keys unique. O(log n) operations. Include <map>.

12. What does #include <iostream> do?

Beginner

Answer: Includes input/output stream library for cin/cout

#include <iostream> includes the input/output stream library, providing cin (input), cout (output), cerr (error), and clog (logging).

13. What is a smart pointer?

Advanced

Answer: RAII wrapper managing pointer lifetime automatically

Smart pointers are RAII wrappers managing pointer lifetime automatically. Types: unique_ptr (exclusive ownership), shared_ptr (shared ownership), weak_ptr (non-owning). Prevent leaks.

14. What is a namespace?

Beginner

Answer: Declarative region preventing name conflicts

Namespace is a declarative region providing scope for identifiers. Prevents name conflicts. std is the standard library namespace. Use using namespace std; or std::.

15. What is the difference between map and unordered_map?

Intermediate

Answer: map sorted (tree), unordered_map unsorted (hash)

map is sorted (red-black tree, O(log n)). unordered_map is hash table (O(1) average). Use map for ordered iteration, unordered_map for faster lookup.

16. What is cout?

Beginner

Answer: Output stream object for printing to console

cout is the standard output stream object. Use with << operator for output: cout << "Hello". Part of iostream library in std namespace.

17. What is unique_ptr?

Advanced

Answer: Smart pointer with exclusive ownership

unique_ptr has exclusive ownership. Cannot be copied, only moved. Lightweight, no overhead. Use make_unique. Default choice for smart pointers.

18. What is an iterator?

Intermediate

Answer: Object for traversing containers

Iterator is an object for traversing containers. Acts like pointer. Types: begin(), end(), rbegin(), rend(). Use with ++, --, *, ->. Generic way to access elements.

19. What is shared_ptr?

Advanced

Answer: Smart pointer with shared ownership via reference counting

shared_ptr shares ownership via reference counting. Object deleted when last shared_ptr destroyed. Overhead from ref count. Use make_shared. Watch for cycles.

20. What is inheritance?

Intermediate

Answer: Mechanism for creating class from existing class

Inheritance creates new class (derived/child) from existing class (base/parent). Promotes code reuse. Supports is-a relationship. Types: single, multiple, multilevel, hierarchical.

21. What is cin?

Beginner

Answer: Input stream object for reading from console

cin is the standard input stream object. Use with >> operator for input: cin >> variable. Reads from standard input (keyboard).

22. What is weak_ptr?

Advanced

Answer: Non-owning reference breaking shared_ptr cycles

weak_ptr is non-owning reference to shared_ptr. Doesn't increase ref count. Prevents cycles. Convert to shared_ptr with lock(). Check validity with expired().

23. What is polymorphism?

Intermediate

Answer: Objects of different types accessed through common interface

Polymorphism allows objects of different types to be accessed through common interface. Achieved via virtual functions. Enables runtime polymorphism (dynamic binding).

24. What is a variable?

Beginner

Answer: Named storage location with specific type

A variable is a named storage location with a specific data type. Must be declared before use: int x = 5; Type determines size and operations.

Ready to test yourself?

The full C++ bank has 90 questions across 3 difficulty levels — timed, shuffled, and scored.

Take the C++ quiz