Editor

Python operators MCQ questions with answers

Arithmetic, comparison, logical and identity operators, with the traps exams like to set.

0 of 20 answered

  1. Question 1 · Easy

    What is the output of this code?

    print(17 // 5, 17 % 5)
    Show the answer

    Answer: A

    // is floor division, so 17 // 5 is 3 (not 3.4, which is what / gives). % gives the remainder: 17 = 5 * 3 + 2, so 17 % 5 is 2.

    Run this code in the editor
  2. Question 2 · Medium

    What is the output of this code?

    print(-17 // 5, -17 % 5)
    Show the answer

    Answer: B

    // rounds down toward minus infinity, so -17 / 5 = -3.4 floors to -4, not -3. The remainder then follows from -17 = 5 * -4 + 3, so % gives 3, which takes the sign of the divisor.

    Run this code in the editor
  3. Question 3 · Medium

    What is the output of this code?

    print(2 ** 3 ** 2)
    Show the answer

    Answer: C

    ** groups from the right, so 2 ** 3 ** 2 means 2 ** (3 ** 2), which is 2 9 = 512. Working left to right would give (2 3) ** 2 = 64.

    Run this code in the editor
  4. Question 4 · Easy

    What is the output of this code?

    x = 10
    x += 5
    x *= 2
    x //= 4
    print(x)
    Show the answer

    Answer: D

    Each augmented assignment updates x in turn: 10 + 5 = 15, then 15 * 2 = 30, then 30 // 4 = 7. //= uses floor division, so the result is the integer 7, not 7.5.

    Run this code in the editor
  5. Question 5 · Easy

    What is the output of this code?

    x = 5
    print(1 < x < 10, 10 < x < 20)
    Show the answer

    Answer: C

    1 < x < 10 is a chained comparison meaning 1 < x and x < 10, which is True for 5. 10 < x < 20 fails at 10 < x, so it is False.

    Run this code in the editor
  6. Question 6 · Hard

    What is the output of this code?

    print(3 > 2 > 1)
    print((3 > 2) > 1)
    Show the answer

    Answer: D

    Without brackets, 3 > 2 > 1 is a chain meaning 3 > 2 and 2 > 1, which is True. With brackets, (3 > 2) is evaluated first to True, which equals 1, and 1 > 1 is False.

    Run this code in the editor
  7. Question 7 · Medium

    What is the output of this code?

    a = [1, 2, 3]
    b = [1, 2, 3]
    c = a
    print(a == b, a is b, a is c)
    Show the answer

    Answer: B

    == compares values, and both lists hold the same items. is checks whether two names refer to the same object: b is a separate list, while c = a makes c another name for the list a refers to.

    Run this code in the editor
  8. Question 8 · Easy

    What is the output of this code?

    print("nan" in "banana")
    print(3 in [1, 2, 33])
    Show the answer

    Answer: A

    On strings, in looks for a substring, and "nan" appears inside "banana". On a list, in compares whole items, and 3 is not equal to 33.

    Run this code in the editor
  9. Question 9 · Medium

    What is the output of this code?

    print(0 or "default")
    print(3 and 7)
    Show the answer

    Answer: C

    or and and return one of their operands, not a bool. or returns the first truthy value ("default", since 0 is falsy), and and returns the last value it evaluates when all are truthy (7).

    Run this code in the editor
  10. Question 10 · Hard

    What is the output of this code?

    def check(n):
        print("check", n)
        return n
    
    result = check(0) or check(2) or check(3)
    print(result)
    Show the answer

    Answer: D

    or stops at the first truthy operand. check(0) returns 0, which is falsy, so check(2) runs and returns 2, which is truthy, and check(3) never runs. The result is that operand, 2, not True.

    Run this code in the editor
  11. Question 11 · Hard

    What is the output of this code?

    a = [1, 2]
    b = a
    a += [3]
    a = a + [4]
    print(b)
    Show the answer

    Answer: C

    On a list, a += [3] extends the list in place, so b, which refers to the same list, sees the 3. a = a + [4] builds a new list and points a at it, which leaves b unchanged.

    Run this code in the editor
  12. Question 12 · Medium

    What is the output of this code?

    print(6 & 3, 6 | 3, 6 ^ 3)
    Show the answer

    Answer: A

    These are bitwise operators. In binary 6 is 110 and 3 is 011: & keeps bits set in both (010 = 2), | keeps bits set in either (111 = 7), and ^ keeps bits set in exactly one (101 = 5). ^ is not the power operator.

    Run this code in the editor
  13. Question 13 · Easy

    Which error does this code raise?

    total = 10
    count = 0
    print(total / count)
    Show the answer

    Answer: B, ZeroDivisionError: division by zero

    Dividing by zero raises ZeroDivisionError with /, // and % alike. Python does not return infinity for division by zero.

    Run this code in the editor
  14. Question 14 · Medium

    Which error does this code raise?

    age = "18"
    if age >= 18:
        print("adult")
    Show the answer

    Answer: C, TypeError: '>=' not supported between instances of 'str' and 'int'

    Python does not convert types for ordering comparisons, so comparing a str with an int using >= raises TypeError. Convert first with int(age). Equality is different: "18" == 18 is allowed and is False.

    Run this code in the editor
  15. Question 15 · Easy

    Which error does this code raise?

    count = 5
    count++
    print(count)
    Show the answer

    Answer: D, SyntaxError: invalid syntax

    Python has no ++ operator, so count++ is not valid syntax and the program does not start. Write count += 1 instead. (++count is valid but does nothing: it is two unary plus signs.)

    Run this code in the editor
  16. Question 16 · Easy

    Which operator checks whether two names refer to the same object in memory?

    Show the answer

    Answer: B, is

    is compares identity: it is True only when both names refer to the same object. == compares values, so two separate lists with equal items are == but not is.

  17. Question 17 · Medium

    With no brackets, in which order does Python apply not, and and or?

    Show the answer

    Answer: A, not, then and, then or

    not binds tightest, then and, then or. So True or False and False means True or (False and False), which is True; reading left to right would give False.

  18. Question 18 · Medium

    What does the ^ operator do with two integers in Python?

    Show the answer

    Answer: B, Bitwise XOR: keeps the bits set in exactly one of them

    ^ is bitwise exclusive or: 5 (101) ^ 2 (010) is 7 (111). The power operator is **, so 5 ** 2 is 25.

  19. Question 19 · Hard

    Which expression is True exactly when year is a leap year (divisible by 4, except century years not divisible by 400)?

    Show the answer

    Answer: A

    and binds tighter than or, so the first option reads as (divisible by 4 and not by 100) or divisible by 400, which is the leap-year rule. Testing only % 4 wrongly makes 1900 a leap year.

  20. Question 20 · Easy

    For a positive integer n, which expression gives its last digit?

    Show the answer

    Answer: D

    % gives the remainder, and the remainder after dividing by 10 is the last digit: 4728 % 10 is 8. // removes the last digit instead (472), and % 100 keeps the last two digits.

Keep going