C++ Quiz
Thirty questions covering the whole course. Choose an answer for each, then press Check answers at the bottom to see your score and an explanation for every question.
1. Why should you prefer '\n' to std::endl in most output?
Use std::endl only when you really need the flush (lesson 03).
2. What does int b{3.9}; do?
int a = 3.9; would compile and silently truncate (lesson 04).
3. What is the type of auto name = "Sam";?
String literals are C-style arrays; write std::string{"Sam"} or "Sam"s for a std::string (lesson 04).
4. What's the difference between const and constexpr variables?
(lesson 05)
5. After std::cin >> n; reads a number, why might the next std::getline return an empty string?
Skip it with std::cin >> std::ws first (lesson 07).
6. Which of these compares two std::strings by content?
Unlike C strings, std::string overloads the comparison operators (lesson 08).
7. In for (auto x : v) x *= 2;, what happens to v?
Use auto& to modify the elements (lesson 11).
8. How should a function take a large std::vector it only reads?
No copy, and the function can't modify it (lessons 14 and 19).
9. What does std::move(name) do by itself?
Afterwards, don't rely on the moved-from object's value (lesson 14).
10. What's the danger of a std::string_view?
Use views for parameters; be careful storing them (lesson 19).
11. What does m["key"] do on a std::map when the key is missing?
Use contains, find or at to look without inserting (lesson 21).
12. When does iterating a std::unordered_map give a predictable order?
Use std::map or sort the results when order matters (lesson 21).
13. What does end() of a container point to?
Dereferencing end() is undefined behaviour (lesson 23).
14. In [&total, limit](int v) { … }, how are the variables captured?
(lesson 24)
15. What makes C++20 views "lazy"?
Which is why views::iota(1) can be infinite (lesson 26).
16. Which statement about owning raw pointers is the modern guideline?
(lessons 27–29)
17. Which smart pointer should you use by default for a single owner?
unique_ptr has no overhead compared with a raw pointer (lesson 29).
18. What does RAII guarantee?
(lesson 30)
19. What does const after a member function's parameter list mean?
(lesson 31)
20. In what order are members initialised?
Compilers warn when the initialiser list order differs (lesson 32).
21. Why mark a single-argument constructor explicit?
(lesson 32)
22. What does auto operator<=>(const T&) const = default; provide?
(lessons 20 and 34)
23. What is the "rule of zero"?
(lessons 30 and 35)
24. A base class is used polymorphically through pointers. What must it have?
Otherwise deleting a derived object through a base pointer is undefined behaviour (lesson 37).
25. What does override do?
final prevents further overriding (lesson 37).
26. What makes a class abstract?
Abstract classes can't be instantiated (lesson 38).
27. Where should template definitions usually go?
(lessons 39–40)
28. What do concepts add to templates?
(lesson 41)
29. When is std::optional a better choice than throwing an exception?
(lessons 42–43)
30. What is a data race?
Data races are undefined behaviour; protect shared data with mutexes or atomics (lesson 47).