Embercode
✕
C++ ConditionalsQuick quiz

Guards and De Morgan

8 questions. Covers De Morgan rewrites.

Questions 1 to 5 of 8

  1. 1. Predict the output of this program. ``cpp #include <iostream> #include <string> using namespace std; string check(int n) { if (n < 0) { return "negative"; } if (n > 10) { return "big"; } return "small"; } int main() { cout << check(-3) << " " << check(12) << " " << check(4) << "\n"; } ``
  2. 2. Predict the output of this program. ``cpp #include <iostream> using namespace std; int main() { cout << boolalpha; int age = 17; bool has_id = true; cout << !(age >= 18 && has_id) << " " << (age < 18 && !has_id) << "\n"; } ``
  3. 3. This guard is meant to refuse an amount that is zero or negative. What is the bug? ``cpp string check_amount(int amount) { if (amount > 0) { return "amount must be positive"; } return "ok"; } ``
  4. 4. A student wants to refuse a person who is under 18. What is the bug? ``cpp int age = 16; if (!age >= 18) { cout << "refused\n"; } ``
  5. 5. True or false: for a double x, !(x < 1.0) and x >= 1.0 always give the same answer.

5 question(s) left on this page.