Multiple Choice Quiz

1. Which scenario would MOST likely require profiling?

2. How do move semantics contribute to the efficiency of classes with value semantics?

3. What is a common pitfall when writing a microbenchmark in C++?

4. Which standard C++ library provides time-based tools that can be used for benchmarking?

5. When should you avoid marking a function as constexpr?

6. There are several things to make sure you understand when benchmarking, which of the following is NOT a reasonable goal?

7. What is small string optimization?

8. Andrei Alexandrescu's small string optimization is often given as an example of really clever problem solving. What did he gain when he swapped string size for _remaining_ string size?

9. Given a string representation that tracks string length (8 bytes), string capacity (8 bytes) and a pointer to the string (8 bytes), how long of a string can you store using small-string optimization (SSO)?

10. Why should you try to avoid branching in highly optimized code?

11. Which of the following is a valid use of constexpr in C++?

12. If we profile a scope that has a lot of code and see that it is taking longer than expected, which of the following is NOT typically an effective next step for identifying the problem?

13. Which of the following factors would make it impossible to effectively profile a function?

14. When an l-value argument is passed by value into a function parameter, which of the following statements is true?

15. What does it mean if a function is marked constexpr?

16. Why is it important to profile your code before manually optimizing it?

17. Which of the following do you need to worry about when instrumenting your code for profiling?

18. Which of the following features helps to facilitate efficient pass-by-value for classes with a large memory footprint?

19. Which of the following is a benefit of benchmarking your code?

20. When deciding between passing-by-value and passing-by-reference, which of the following is NOT going to be relevant?

21. What is the direct result of profiling your code?

22. What is one advantage of providing a constexpr constructor in a class?

23. Assuming we have the function:
  constexpr int add(int x, int y) {
    return x + y;
  }
What would happen if we call:
  add(std::cin.get(), 3);

24. If a variable is declared as constexpr, will its value be set at compile time?

25. Which statement correctly distinguishes between const and constexpr?

26. Many std::string implementations maintain an instance of the empty string rather than just using a null pointer. Why?

27. If you want to use value semantics on objects of a custom class, which of the following is most important to implement?

28. Is it better to benchmark in debug or release mode?

29. Some implementations of std::string use a "copy on write" technique where copies keep pointing to the original memory for the string sequence, but also maintain a reference count of how many instances exist. Only once a string needs to be changed is it actually copied first. Why?

30. Some implementations of std::string use a "copy on write" (COW) technique where copies keep pointing to the original memory for the string sequence, but also maintain a reference count of how many instances exist. Why have most modern implementations moved away from this COW technique?


Click Check Answers to identify any errors and try again. Click Show Answers if you also want to know which answer is the correct one.