1. What is the output from the following code fragment? int var = 5; switch (var) { case 1: std::cout << "1"; break; case 2: std::cout << "2"; break; case 3: std::cout << "3"; case 4: std::cout << "4"; break; case 5: std::cout << "5"; default: std::cout << "d"; }
int var = 5;
switch (var) {
case 1: std::cout << "1"; break;
case 2: std::cout << "2"; break;
case 3: std::cout << "3";
case 4: std::cout << "4"; break;
case 5: std::cout << "5";
default: std::cout << "d";
}
5
5d
d
12345d
switch
2. Which of the following is NOT a legal function declaration
const std::string & MyFun(std::string);
std::string MyFun(std::string name="default");
void MyFun(int x, int y);
std::string MyFun(std::string name=default);
3. 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(12, 34);
PrintBoth("12", 34);
4. You have two variables 'day' and 'temperature'. You want to stop a loop if either day becomes greater than 365 or if temperature goes below zero. You want to keep looping as long as neither of those conditions triggers. What should your loop look like?
while (day <= 365 || temperature >= 0) {
while (day > 365 && temperature < 0) {
while (day > 365 || temperature < 0) {
while (day <= 365 && temperature >= 0) {
5. 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;
nullptr
value
unsigned
node
node->next->value
6. What is the output of the following program: std::map<int, std::string> star_map; star_map[0] += "*"; star_map[2] += "*"; star_map[3] += "*"; star_map[0] += "*"; std::cout << star_map[0] << "-" << star_map[1] << "-" << star_map[2];
std::map<int, std::string> star_map;
star_map[0] += "*";
star_map[2] += "*";
star_map[3] += "*";
std::cout << star_map[0] << "-" << star_map[1] << "-" << star_map[2];
*--**
**--*
***--
7. Given the following function, which one of the following claims is incorrect? (i.e., find the FALSE statement) int CrazyFun(int & in) { return ++in * 10; }
int CrazyFun(int & in) { return ++in * 10; }
CrazyFun(x)
8. Which of the following is NOT a correct way to indicate a newline at the end of "Hello World!"?
std::cout << "Hello World!\endl";
std::cout << "Hello World!"; std::cout << "\n";
std::cout << "Hello World!\n";
std::cout << "Hello World!" << std::endl;
9. 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(int param1, int param2=0);
void TestFunction(char param);
void TestFunction(double param);
void TestFunction(std::string param);
10. What is the difference between the keyword struct and the keyword class?
struct
class
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.