1. Which of these numeric types is represented using the fewest bytes in C++?
long long
int
char
float
long
2. If I created an array of strings with "std::string word_list[5000]", what should I do when I'm done using word_list?
std::string word_list[5000]
for (auto x : word_list) delete x;
delete [] word_list
delete word_list
3. You want to execute code if either var1 is true or var2 is true, but not if they are both true. Given that both variables are type bool, three of the following techniques work; which one will NOT work?
var1
var2
bool
if ( var1 XOR var2) {
}
if ( var1 != var2) {
if ( (var1 || var2) && !(var1 && var2) ) {
if ( (var1 && !var2) || (var2 && !var1) ) {
4. Which of the following is NOT a good reason a to define a member variable in the private section of a class definition?
5. What does the following line of code do? assert(x == 10);
assert(x == 10);
x
10
6. What is a difference between a native array (such as "int a[100]") and std::vector (such as "std::vector<int> v(100)")?
int a[100]
std::vector<int> v(100)
7. Why should you use "include guards" (or #pragma once) in your header files?
#pragma once
8. Which of the following is an illegal use of the keyword "auto"?
const auto & my_err = std::cerr;
auto my_str = std::string("Test.");
auto my_var = std::string("abcd");
auto i;
9. What will happen if you do not explicitly initialize an int variable?
10. What will the following code output? std::vector<int> v{1,2,3,4}; std::cout << std::accumulate(v.begin()+1, v.end()-1, 0) << std::endl;
std::vector<int> v{1,2,3,4};
std::cout << std::accumulate(v.begin()+1, v.end()-1, 0) << std::endl;
1
5
6
0
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.