Multiple Choice Quiz

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";
  }

2. Which of the following is NOT a legal function declaration

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?

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?

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:

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];

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; }

8. Which of the following is NOT a correct way to indicate a newline at the end of "Hello World!"?

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?

10. What is the difference between the keyword struct and the keyword 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.