Embercode
✕
C++ Variables and Data TypesQuick quiz

Uninitialised values and booleans

7 questions. Covers Booleans and the = trap.

Questions 1 to 5 of 7

  1. 1. This program should count the values above 4, but it prints a different number on different runs. ``cpp #include <iostream> #include <vector> using namespace std; int main() { vector<int> values = {3, 8, 12, 5}; int big; for (int v : values) { if (v > 4) { ++big; } } cout << "values above 4: " << big << endl; } `` What is the fix?
  2. 2. This program should print full marks only when the score is 100. It prints full marks for a score of 75. ``cpp #include <iostream> using namespace std; int main() { int score = 75; if (score = 100) { cout << "full marks" << endl; } cout << "score is " << score << endl; } `` What is the bug?
  3. 3. This program should print any passed: true, because 72 and 91 pass. It prints any passed: false. ``cpp #include <iostream> #include <vector> using namespace std; int main() { vector<int> scores = {55, 72, 91, 40}; bool anyPassed = false; for (int s : scores) { anyPassed = s >= 60; } cout << boolalpha << "any passed: " << anyPassed << endl; } `` What is the bug?
  4. 4. A program declares int hits; outside every function, and int count; inside main. Both are read before anything sets them. Which statement is correct?
  5. 5. A program prints a bool variable done that is true, and the output is 1, not true. Which line is missing from the program?

5 question(s) left on this page.