Embercode
✕
C++ ConditionalsQuick quiz

Ranges

6 questions. Covers Range checks without chained comparisons.

Questions 1 to 5 of 6

  1. 1. The programmer wants to know whether x is from 1 to 10, including both ends. What is the bug? ``cpp int x = 5; if (1 <= x <= 10) { cout << "in range\n"; } ``
  2. 2. The programmer wants to print valid only when x is from 1 to 10, including both ends. What is the bug? ``cpp int x = 50; if (x >= 1 || x <= 10) { cout << "valid\n"; } ``
  3. 3. What number does this program print? A die has faces from 1 to 6. ``cpp #include <algorithm> #include <iostream> #include <vector> using namespace std; bool is_valid_roll(int roll) { return 1 <= roll <= 6; } int main() { vector<int> rolls = {3, 7, 0, 6, 1}; cout << ranges::count_if(rolls, is_valid_roll) << "\n"; } ``
  4. 4. Predict the output of this program. ``cpp #include <iostream> using namespace std; bool in_range(int value, int low, int high) { return low <= value && value <= high; } int main() { cout << boolalpha; cout << in_range(5, 1, 10) << " " << in_range(15, 1, 10) << " " << in_range(7, 10, 1) << "\n"; } ``
  5. 5. True or false: the condition 0 <= x && x < 5 is true when x is 5.

5 question(s) left on this page.