Python operators MCQ questions with answers
Arithmetic, comparison, logical and identity operators, with the traps exams like to set.
0 of 20 answered
Question 1 · Easy
What is the output of this code?
print(17 // 5, 17 % 5)
Show the answer
Answer: A
Run this code in the editor//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.Question 2 · Medium
What is the output of this code?
print(-17 // 5, -17 % 5)
Show the answer
Answer: B
Run this code in the editor//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.Question 3 · Medium
What is the output of this code?
print(2 ** 3 ** 2)
Show the answer
Answer: C
Run this code in the editor**groups from the right, so2 ** 3 ** 2means2 ** (3 ** 2), which is 2 9 = 512. Working left to right would give (2 3) ** 2 = 64.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
Run this code in the editorxin 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.Question 5 · Easy
What is the output of this code?
x = 5 print(1 < x < 10, 10 < x < 20)
Show the answer
Answer: C
Run this code in the editor1 < x < 10is a chained comparison meaning1 < x and x < 10, which isTruefor 5.10 < x < 20fails at10 < x, so it isFalse.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,
Run this code in the editor3 > 2 > 1is a chain meaning3 > 2 and 2 > 1, which isTrue. With brackets,(3 > 2)is evaluated first toTrue, which equals 1, and1 > 1isFalse.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
Run this code in the editor==compares values, and both lists hold the same items.ischecks whether two names refer to the same object:bis a separate list, whilec = amakescanother name for the listarefers to.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,
Run this code in the editorinlooks for a substring, and"nan"appears inside"banana". On a list,incompares whole items, and 3 is not equal to 33.Question 9 · Medium
What is the output of this code?
print(0 or "default") print(3 and 7)
Show the answer
Answer: C
Run this code in the editororandandreturn one of their operands, not a bool.orreturns the first truthy value ("default", since 0 is falsy), andandreturns the last value it evaluates when all are truthy (7).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
Run this code in the editororstops at the first truthy operand.check(0)returns 0, which is falsy, socheck(2)runs and returns 2, which is truthy, andcheck(3)never runs. The result is that operand, 2, notTrue.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,
Run this code in the editora += [3]extends the list in place, sob, which refers to the same list, sees the 3.a = a + [4]builds a new list and pointsaat it, which leavesbunchanged.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
Run this code in the editor110and 3 is011:&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.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
Run this code in the editorZeroDivisionErrorwith/,//and%alike. Python does not return infinity for division by zero.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
Run this code in the editorstrwith anintusing>=raisesTypeError. Convert first withint(age). Equality is different:"18" == 18is allowed and isFalse.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
Run this code in the editor++operator, socount++is not valid syntax and the program does not start. Writecount += 1instead. (++countis valid but does nothing: it is two unary plus signs.)Question 16 · Easy
Which operator checks whether two names refer to the same object in memory?
Show the answer
Answer: B, is
iscompares identity: it isTrueonly when both names refer to the same object.==compares values, so two separate lists with equal items are==but notis.Question 17 · Medium
With no brackets, in which order does Python apply
not,andandor?Show the answer
Answer: A, not, then and, then or
notbinds tightest, thenand, thenor. SoTrue or False and FalsemeansTrue or (False and False), which isTrue; reading left to right would giveFalse.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**, so5 ** 2is 25.Question 19 · Hard
Which expression is
Trueexactly whenyearis a leap year (divisible by 4, except century years not divisible by 400)?Show the answer
Answer: A
andbinds tighter thanor, 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% 4wrongly makes 1900 a leap year.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 % 10is 8.//removes the last digit instead (472), and% 100keeps the last two digits.
Keep going
- Read the lesson behind these questions: Operators in the PythonBook
- Next topic: Python string MCQ questions with answers
- Before this: Python basics MCQ questions with answers
- Write the code yourself in the Python coding challenges, which check your solution with tests.