# Revision and Interview Questions — C++

Source: https://www.skillbyai.com/en/cpp/p-revision

> Recall core C++ concepts quickly for exams and interviews.

## Cheat sheet

**Basics**: compiled, zero-overhead abstractions; standards C++11/14/17/20/23; preprocessing, compiling translation units, linking; `-std=c++20 -Wall -Wextra`. **Types**: brace initialisation prevents narrowing; `const`, `constexpr`, `auto`, `<cstdint>`; signed overflow is UB. **Functions**: pass small by value, large by `const&`, modify by `&`; return by value; overloading; `[[nodiscard]]`. **Structure**: headers declare, sources define; `#pragma once`; ODR; namespaces; no `using namespace std` in headers. **Memory**: stack vs heap; references (alias, never null) vs pointers (nullable, reseatable); dangling; RAII; `unique_ptr`, `shared_ptr`, `weak_ptr`, `make_unique`. **Classes**: initialiser lists in declaration order, `explicit`, `const` member functions; copy vs move, `std::move` is a cast; rule of zero/three/five; `virtual`, `override`, `final`, pure virtual, virtual destructors, slicing. **STL**: vector by default, map/set O(log n), unordered O(1) average, iterator invalidation; algorithms, lambdas and captures, ranges and views, `erase_if`; optional, variant, string_view, span, expected. **Templates**: instantiation, headers, concepts, constexpr, consteval, `if constexpr`. **Robustness**: exceptions by const reference, `noexcept` moves, threads with jthread, mutex, scoped_lock, atomic; UB and sanitizers.

## Common interview questions

Answer each with a short code example.

```text
1. What is the difference between a pointer and a reference?
2. Explain RAII and give two standard-library examples.
3. unique_ptr vs shared_ptr vs weak_ptr: when do you use each?
4. What are the rule of zero, rule of three and rule of five?
5. What does std::move do, and what state is a moved-from object in?
6. Why do base classes need virtual destructors? What is object slicing?
7. How is a virtual function call dispatched at run time?
8. Compare std::map and std::unordered_map.
9. What happens to iterators when a std::vector reallocates?
10. What is undefined behaviour? Give three examples and how to detect them.
11. What are templates and how do concepts improve them?
12. Why are move constructors usually marked noexcept?
```

## Talk about ownership

Most C++ design questions come down to ownership and lifetime: who creates this object, who destroys it, and can anyone else outlive it? Answering in those terms shows real understanding.

**Quiz:** Which statement about a moved-from std::vector is correct?

- [ ] It is destroyed immediately
- [ ] Using it in any way is undefined behaviour
- [ ] It still holds all its original elements
- [x] It is in a valid but unspecified state and can be assigned a new value or destroyed

*Answer:* It is in a valid but unspecified state and can be assigned a new value or destroyed. Moved-from standard objects remain valid; you may reassign or destroy them.
