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?
virtual
delete
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++?
struct
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?
constexpr double PI = 3.141592;
double area = PI * r * r;
double PI() { return 3.141592; } // In a header file
double area = PI() * r * r; // Later, where you're using it
double area = 3.141592 * r * r;
8. Why should you use smart pointers rather than directly calling new and delete yourself?
new
9. Which of the following is a valid reason that you should avoid using namespace std; in your code?
using namespace std;
using
typedef
10. According to the core guidelines, what should you do if you have multiple return values?
tuple
11. Which of the following is NOT an advantage of using std::unique_ptr over std::shared_ptr?
std::unique_ptr
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?
'\n'
std::endl
13. According to the C++ Core Guidelines, what is a good technique to reduce the number of parameters to a function?
std::pair
Point
14. When should you use the override keyword?
override
15. What can the keyword mutable be used for in C++?
mutable
const
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?
struct MyClass {
int a;
int b;
int c;
MyClass(int x) : c(x++), b(x++), a(x++) { }
};
MyClass var(7)
a
b
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?
std::make_unique
std::make_shared
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?
for
for (auto & x : data) { ... }
for (size_t i=0; i < data.size(); ++i) { ... }
24. Which of the following is a good reason to use std::unique_ptr instead of a raw pointer?
std::vector
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; }
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;
}
12 // i.e., Compute(2,0,1)
23 // i.e., Compute(0,1,2)
5 // i.e., Compute(2,1,0)
21 // i.e., Compute(1,0,2)
26. If you want to compile a C++ program without any optimizations turned on, how can you do this with clang or gcc?
-g
--no-debug
-Onone
-DNDEBUG
27. Which of the following C++ utilities allows you to express an intention to have a value that might or might not be present?
std::none
std::maybe
std::any
std::nullable
std::optional
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.