Interview Prep | C++
C++ Interview Questions

Entry, Mid, and Advanced C++ questions with short answers. Use search to filter questions quickly.

🔍

C++ Entry Level Q&A

Back to Top ↑
🧠
Q1. What is RAII?
A. Resource Acquisition Is Initialization: manage resources using object lifetime (constructor acquires, destructor releases).
Entry
🧠
Q2. Reference vs pointer?
A. A reference is an alias and cannot be null; a pointer holds an address and can be null/reseated.
Entry
🧠
Q3. struct vs class?
A. Default access is public for struct and private for class; otherwise they are the same.
Entry
🧠
Q4. What is a virtual function?
A. A function enabling runtime polymorphism via vtable dispatch.
Entry
🧠
Q5. Why should base destructors be virtual?
A. Deleting derived objects via base pointer requires virtual destructor; otherwise behavior is undefined.
Entry
🧠
Q6. What is the STL?
A. Standard Template Library: containers, algorithms, iterators, utilities.
Entry
🧠
Q7. What is const correctness?
A. Use const to prevent modifications and express intent; enables better API design.
Entry
🧠
Q8. What is a namespace used for?
A. To avoid naming collisions and organize code.
Entry

C++ Mid Level Q&A

Back to Top ↑
⚙️
Q1. unique_ptr vs shared_ptr?
A. unique_ptr is single ownership and movable; shared_ptr is reference-counted shared ownership (overhead, cycles possible).
Mid
⚙️
Q2. What is move semantics?
A. Transferring resources from temporaries instead of copying, using move ctor/assignment and std::move.
Mid
⚙️
Q3. Rule of 3/5/0?
A. If you define destructor/copy ops, likely define all. Add move ops. Prefer Rule of 0 using RAII types.
Mid
⚙️
Q4. vector vs list?
A. vector is contiguous and cache-friendly; list has high overhead and poor cache locality.
Mid
⚙️
Q5. What is iterator invalidation?
A. Operations may invalidate iterators/references (e.g., vector reallocation). Know rules to avoid UB.
Mid
⚙️
Q6. What is object slicing?
A. Copying derived object into base by value drops derived parts. Prefer refs/pointers for polymorphism.
Mid
⚙️
Q7. What is constexpr?
A. Enables compile-time evaluation when possible, improving performance and correctness.
Mid
⚙️
Q8. What is template specialization?
A. Providing a specific implementation for a template for certain types/values.
Mid

C++ Advanced Level Q&A

Back to Top ↑
🚀
Q1. What is exception safety?
A. Basic: invariants preserved. Strong: commit/rollback. No-throw: operation never throws (important for destructors).
Advanced
🚀
Q2. new/delete vs malloc/free?
A. new/delete run constructors/destructors and are type-safe; malloc/free do not. Do not mix them.
Advanced
🚀
Q3. What is the One Definition Rule (ODR)?
A. Entities must have exactly one definition across the program; violations cause UB/linker issues.
Advanced
🚀
Q4. What is SFINAE?
A. Substitution Failure Is Not An Error; enables template-based overload selection.
Advanced
🚀
Q5. What are data races?
A. Unsynchronized concurrent access where at least one write occurs; causes undefined behavior.
Advanced
🚀
Q6. How do you avoid shared_ptr cycles?
A. Use weak_ptr for back-references to break reference cycles.
Advanced
🚀
Q7. What does std::move do?
A. It casts to an rvalue reference; moving happens in move operations, not in std::move itself.
Advanced
🚀
Q8. What is ABI and why it matters?
A. Binary interface for linking compiled code; ABI breaks can cause runtime crashes across builds.
Advanced