Multiple Choice Quiz

1. According to the core guidelines, should you prefer default arguments in a constructor or multiple versions of the constructor with different numbers of arguments? Why?

2. Why should a class destructor sometimes be marked virtual?

3. According to the core guidelines, where should you set the default values for member variables in a class?

4. Under which of these conditions is it good coding practice to use raw pointers in C++?

5. What are "structured bindings" in C++?

6. What order are a class' member variables initialized in?

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

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

9. Which of the following is a valid reason that you should avoid using namespace std; in your code?

10. According to the core guidelines, what should you do if you have multiple return values?

11. Which of the following is NOT an advantage of using std::unique_ptr over std::shared_ptr?

12. How should you decide whether to use '\n' or std::endl for a newline when you are outputting to a stream?

13. According to the C++ Core Guidelines, what is a good technique to reduce the number of parameters to a function?

14. When should you use the override keyword?

15. What can the keyword mutable be used for in C++?

16. Two types of smart pointers are commonly used from the C++ standard library, std::unique_ptr and std::shared_ptr. How should you decide which one to use?

17. While function arguments may be processed in any order, the same is not true for initialization lists. Consider the following C++ structure:
  struct MyClass {
    int a;
    int b;
    int c;
    MyClass(int x) : c(x++), b(x++), a(x++) { }
  };
If you create a variable with MyClass var(7), what will be the values of a, b, and c?

18. Which of the following is true about the C++ Core Guidelines as a whole?

19. Which of the following is NOT one of the C++ Core Guidelines? Hint: all of the others were mentioned in the core guidelines video that you watched.

20. What is the advantage of using std::make_unique or std::make_shared over constructing your unique/shared pointers directly?

21. Why is it important to use std::unique_ptr in a function that may throw an exception in the middle?

22. Why should I try to avoid casting away const on a variable even when I know that I am not changing its value?

23. Which of the following is good advice for deciding when to use a range-based for loop like:
  for (auto & x : data) { ... }
versus an indexed-based for loop:
  for (size_t i=0; i < data.size(); ++i) { ... }
when iterating over a vector?

24. Which of the following is a good reason to use std::unique_ptr instead of a raw pointer?

25. Sometimes arguments in a function call may have side-effects, so it's important to understand their order of evaluation. What will the following code output?
  int Compute(int a, int b, int c) { return a + 3*b + 10*c; }
  int main() {
    int i = 0;
    std::cout << Compute(i++, i++, i++) << std::endl;
  }

26. If you want to compile a C++ program without any optimizations turned on, how can you do this with clang or gcc?

27. Which of the following C++ utilities allows you to express an intention to have a value that might or might not be present?


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.