1. Why should you use "include guards" (or #pragma once) in your header files?
#pragma once
2. 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 ^^ var2) {
}
if ( (var1 && !var2) || (var2 && !var1) ) {
if ( var1 != var2) {
if ( (var1 || var2) && !(var1 && var2) ) {
3. Which of the following is a correct syntax for defining a lambda function in C++?
lambda(int x) { return x * x; };
[](int x){ return x * x; };
(int x) => x * x;
auto f = (int x) { return x * x; };
4. Given that a Node is defined as: struct Node{ int value = 0; Node * next = nullptr; }; Consider the following code: Node * node = new Node; node->value = 100; node->next = new Node; std::cout << node->next->value; delete node; delete node->next; Which of the following is a problem with this code:
struct Node{
int value = 0;
Node * next = nullptr;
};
Node * node = new Node;
node->value = 100;
node->next = new Node;
std::cout << node->next->value;
delete node;
delete node->next;
value
unsigned
node->next->value
node
5. What will the following code print? for (int i = 0; i < 6; ++i) { if (i == 2) continue; if (i == 4) break; std::cout << i; }
for (int i = 0; i < 6; ++i) {
if (i == 2) continue;
if (i == 4) break;
std::cout << i;
01
2
013
0123
012345
6. If you make a function call TestFunction('a') and all of the following options are available, which version of the function will be called?
void TestFunction(double param);
void TestFunction(char param);
void TestFunction(int param1, int param2=0);
void TestFunction(std::string param);
7. 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(12, 34);
PrintBoth('a', 'b');
PrintBoth("12, 34");
PrintBoth("12", "34");
8. When compiling a large C++ project with many files, which of the following is NOT always good advice?
9. 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)
10. Which of these numeric types is represented using the fewest bytes in C++?
long
uint64_t
double
int
char
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.