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?

2. What does int b{3.9}; do?

3. What is the type of auto name = "Sam";?

4. What's the difference between const and constexpr variables?

5. After std::cin >> n; reads a number, why might the next std::getline return an empty string?

6. Which of these compares two std::strings by content?

7. In for (auto x : v) x *= 2;, what happens to v?

8. How should a function take a large std::vector it only reads?

9. What does std::move(name) do by itself?

10. What's the danger of a std::string_view?

11. What does m["key"] do on a std::map when the key is missing?

12. When does iterating a std::unordered_map give a predictable order?

13. What does end() of a container point to?

14. In [&total, limit](int v) { … }, how are the variables captured?

15. What makes C++20 views "lazy"?

16. Which statement about owning raw pointers is the modern guideline?

17. Which smart pointer should you use by default for a single owner?

18. What does RAII guarantee?

19. What does const after a member function's parameter list mean?

20. In what order are members initialised?

21. Why mark a single-argument constructor explicit?

22. What does auto operator<=>(const T&) const = default; provide?

23. What is the "rule of zero"?

24. A base class is used polymorphically through pointers. What must it have?

25. What does override do?

26. What makes a class abstract?

27. Where should template definitions usually go?

28. What do concepts add to templates?

29. When is std::optional a better choice than throwing an exception?

30. What is a data race?