Multiple Choice Quiz

1. Is the following C++ code valid and safe? Why or why not?
  const char * HelloString() {
    return "Hello";
  }

2. What types of optimizations do C++ compilers give you if you don't specify an optimization level?

3. When should you use a standard library algorithm instead of writing a for loop to do the same task?

4. Why should you use smart pointers rather than directly calling new and delete yourself?

5. Which of the following will not compile?
Assume std::vector<int> vec = GetVector();

6. What is the output of the following code snippet?
  std::vector<int> vec{3, 1, -2};
  auto rng = vec
    | std::views::transform([](int j){ return (j % 2 == 0) ? j : 2 * j;})
    | std::views::transform([](int j){ return j*j;});
  for(auto i : rng){
    std::cout << i << " ";
  }
  std::cout << std::endl;

7. What functionality is NOT provided by Doxygen?

8. What is small string optimization?

9. How should you handle "magic numbers" in the code, such as 3.141592 for PI?

10. You should use an std::array from the C++ standard library (e.g., std::array<int, 10> my_array rather than a C-style array (e.g., int my_array[10]). Which of the following is NOT a reason why?


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.