Embercode
✕
C++ ConditionalsQuick quiz

Dangling else and nulls

8 questions. Covers Null checks before conditions.

Questions 1 to 5 of 8

  1. 1. What is the bug in this code? ``cpp void check(int age, bool has_ticket) { if (age >= 18) if (has_ticket) cout << "Entry allowed" << endl; else cout << "Entry refused" << endl; } ``
  2. 2. What is the bug in this function? ``cpp bool is_long(const string* text) { return text->size() > 5 && text != nullptr; } ``
  3. 3. What is the bug in this loop? ``cpp while (node->value < limit && node != nullptr) { ++count; node = node->next; } ``
  4. 4. What does this program print? ``cpp #include <iostream> using namespace std; void run(int a, int b, int c) { if (a == 1) if (b == 1) if (c == 1) cout << "A"; else cout << "B"; else cout << "C"; cout << endl; } int main() { run(1, 0, 1); } ``
  5. 5. What does this program print? ``cpp #include <iostream> #include <string> using namespace std; bool is_blank(const string* text) { return text == nullptr || text->empty(); } int main() { string a = ""; string b = "x"; cout << boolalpha; cout << is_blank(nullptr) << " " << is_blank(&a) << " " << is_blank(&b) << endl; } ``

5 question(s) left on this page.