Embercode
✕
C++ ConditionalsQuick quiz

Pattern matching and initialisers

6 questions. Covers if with an initializer.

Questions 1 to 5 of 6

  1. 1. In this statement, which parts can use the variable n? ``cpp if (int n = 12; n > 10) { cout << "big\n"; } else { cout << "small\n"; } ``
  2. 2. Why does this statement fail to compile? ``cpp map<string, int> stock = {{"apple", 4}}; if (auto it = stock.find("apple")) { cout << "found\n"; } ``
  3. 3. What does this program print? ``cpp #include <iostream> using namespace std; int main() { int x = 5; if (int x = 0; x > 0) { cout << "then x=" << x; } else { cout << "else x=" << x; } cout << " outer x=" << x << endl; } ``
  4. 4. What does this program print? ``cpp #include <iostream> using namespace std; int main() { cout << "steps:"; for (int i = 0; i < 4; ++i) { if (int step = i * 3; step % 2 == 0) { cout << " " << step; } } cout << endl; } ``
  5. 5. The if (setup; test) form is a C++17 feature. Write the lowest -std value that compiles it with no warning, as the flag is spelled after the equals sign (for example c++14).

    Type your answer.

5 question(s) left on this page.

Typed answers ignore extra spaces and, for most questions, capital letters.