Embercode
✕
C++ ConditionalsQuick quiz

Switch

8 questions. Covers Fall-through on purpose.

Questions 1 to 5 of 8

  1. 1. Predict the output of this program. ``cpp #include <iostream> using namespace std; int main() { int level = 2; int discount = 0; switch (level) { case 3: discount += 10; [[fallthrough]]; case 2: discount += 5; [[fallthrough]]; case 1: discount += 2; break; default: break; } cout << "discount: " << discount << "%\n"; } ``
  2. 2. Predict the output of this program. ``cpp #include <iostream> using namespace std; int main() { int code = 200; switch (code) { case 200: cout << "OK\n"; case 404: cout << "Not found\n"; break; case 500: cout << "Server error\n"; break; default: cout << "Other\n"; break; } } ``
  3. 3. Predict the output of this program. ``cpp #include <iostream> #include <map> #include <string> using namespace std; int main() { map<string, int> prices = {{"apple", 30}}; int tea = prices["tea"]; cout << tea << " " << prices.size() << "\n"; } ``
  4. 4. Which way of reading a std::map leaves the map unchanged when the key may be missing?
  5. 5. Why can't switch (command) be used when command is a std::string?

5 question(s) left on this page.