1. Is the following C++ code valid and safe? Why or why not? const char * HelloString() { return "Hello"; }
const char * HelloString() {
return "Hello";
}
"Hello"
const
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?
for
4. Why should you use smart pointers rather than directly calling new and delete yourself?
new
delete
5. Which of the following will not compile? Assume std::vector<int> vec = GetVector();
std::vector<int> vec = GetVector();
auto iter = std::ranges::min_element(vec.begin(), vec.end());
auto iter = std::ranges::min_element(vec);
auto iter = std::min_element(vec);
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;
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?
double PI() { return 3.141592; } // In a header file
double area = PI() * r * r; // Later, where you're using it
constexpr double PI = 3.141592;
double area = PI * r * r;
double area = 3.141592 * r * r;
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?
std::array<int, 10> my_array
int my_array[10]
std::array
.at
.back
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.