Embercode
✕
C++ OperatorsQuick quiz

Guards and XOR

6 questions. Covers XOR tricks.

Questions 1 to 5 of 6

  1. 1. The guard is meant to stop the read when i is 3, but it does not. What is the bug? ``cpp #include <iostream> #include <vector> using namespace std; int main() { vector<int> v = {4, 0, 7}; int i = 3; if (v[i] == 0 && i < static_cast<int>(v.size())) { cout << "zero\n"; } } ``
  2. 2. x is 5 and y is 3, so they differ, but the program prints same. What is the cause? ``cpp #include <iostream> using namespace std; int main() { int x = 5; int y = 3; if (x ^ y == 0) { cout << "same\n"; } } ``
  3. 3. What does this program print? Each line is listed in order, separated by commas. ``cpp #include <iostream> using namespace std; bool check(int v) { cout << "check " << v << "\n"; return v > 2; } int main() { if (check(1) && check(5)) cout << "yes\n"; if (check(3) || check(9)) cout << "ok\n"; } ``
  4. 4. What does this program print? ``cpp #include <iostream> using namespace std; int main() { int a = 9; int b = 4; a ^= b; b ^= a; a ^= b; cout << a << " " << b << "\n"; } ``
  5. 5. Type the value of x ^ x for any int x.

    Type your answer.

5 question(s) left on this page.

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