Embercode
✕
OperatorsQuick quiz

Guards and XOR in Python

6 questions. Covers XOR tricks in Python.

Questions 1 to 5 of 6

  1. 1. What happens when this code runs? ``python values = [4, 0, 9] i = 3 if values[i] != 0 and i < len(values): print('ok') ``
  2. 2. This code is supposed to print "in range" only for a valid index, but it crashes on the last line. Why does the check say "in range" for index 5? ``python values = [4, 0, 9] index = 5 if index >= 0 or index < len(values): print('in range') print(values[index]) ``
  3. 3. What does this program print? ``python calls = 0 def check(r): global calls calls += 1 return r a = check(True) and check(False) and check(True) b = check(False) or check(True) or check(True) print(a, b, calls) ``
  4. 4. What does this program print? ``python v = [7, 9, 4] i = 2 j = 2 v[i] = v[i] ^ v[j] v[j] = v[j] ^ v[i] v[i] = v[i] ^ v[j] print(v) ``
  5. 5. Write the Python keyword that joins the two tests index >= 0 and index < len(values) in a guard, so that the list read after the guard runs only when both tests are true. Give the keyword.

    Type your answer.

5 question(s) left on this page.

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