1. What is the difference between the keyword struct and the keyword class?
struct
class
2. Given the following templated function: template <typename T> void PrintBoth(T in1, T in2) { std::cout << in1 << ", " << in2 << std::endl; } Which function call will cause a compilation error?
template <typename T>
void PrintBoth(T in1, T in2) {
std::cout << in1 << ", " << in2 << std::endl;
}
PrintBoth(nullptr, nullptr);
PrintBoth("12", 34);
PrintBoth('a', 'b');
PrintBoth("12", "34");
3. Which of these numeric types is represented using the most bytes in C++?
char
float
void
double
bool
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 will the following code print? char x = 'A'; while (x <= 'E') { std::cout << x++; }
char x = 'A';
while (x <= 'E') {
std::cout << x++;
ABCD
ABCDE
ABCDEF
A
BCDEF
6. What would be the output of the following C++ code? std::string main_str{"This is a test string."}; auto pos = main_str.find("is"); std::cout << pos << std::endl;
std::string main_str{"This is a test string."};
auto pos = main_str.find("is");
std::cout << pos << std::endl;
2
21
5
0
std::npos
7. When would you mark a stand-alone function as const?
const
8. 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]
delete [] word_list
delete word_list
for (auto x : word_list) delete x;
9. What error is in the following function? template <typename T> T & Multiply(const T in1, const T in2) { return in1 * in2; }
T & Multiply(const T in1, const T in2) {
return in1 * in2;
10. What is a difference between pointers and references in C++? +
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.