Welcome to our ultimate Python operators MCQ quiz! Here, we have collected the best Python operators questions in quiz format with answers and explanations. Are you ready to test your knowledge on Python operators?
Whether you’re preparing for a job interview, a coding competition, or just want to sharpen your skills, these multiple-choice questions will challenge your understanding.
Each question has four options and you have to choose the correct one. There is no time limit to answer the questions. However, you can also set the time limit on your mobile. You can also bookmark this Python MCQ quiz to revise before exams! Letβs see if you can score 30/30βstart now with question 1! π
1. What will be the output of the following code?
x = 20
y = 30.00
sum = x + y
print(sum)
A. 50
B. 50.0
C. 50.00
D. All the above
b
x is an integer having a value of 20, and y is a float type variable having value of 30.00. When Python performs arithmetic operations between different numeric types (int + float), it automatically converts the result to the more precise type (float) to avoid losing decimal precision. So, 20 + 30.00 = 50.0 (a float value).
2. Which operator is used to check if two variables refer to the same object?
A. ==
B. is
C. equals
D. !=
b
The ‘is’ operator checks whether two variables refer to the same object in memory.
3. What is the output of the following code?
print(5 + 2 * 3 // 2)
A. 7
B. 6
C. 8
D. 10
c
* (Multiplication) and // (Floor Division) have higher precedence than + (Addition). However, they have the same precedence between * and // and Python evaluates them left-to-right.
Step 1: 2 * 3 β 6 (Multiplication first, leftmost high-precedence operator).
Step 2: 6 // 2 β 3 (Floor division truncates decimals, returning an integer).
Step 3: 5 + 3 β 8 (Finally, addition is performed).
4. What is the output of the following code snippet?
print(2 ** 3 ** 2)
A. 256
B. 512
C. 64
D. 8
b
Python follows right-to-left associativity for the exponentiation operator ‘**’. So, 3 ** 2 = 9, and 2 ** 9 = 512.
5. What will be the output of the following code when executed?
x = 20
y = 5
z = 10
x -= y
z -= x + y
print("Result = ", z)
A. -10
B. 10
C. 0
D. 20
a
x -= y is shorthand for x = x – y. x = 20 – 5 = 15. Similarly, z -= x + y is shorthand of z = z = z – (x + y). z = 10 – (15 + 5) = 10 – 20 = -10. So, the final output is -10.
6. What is the output of the below code when you will execute?
x = 10
y = 5
x |= y
print(x)
A. 10
B. 5
C. 15
D. 0
c
The |= operator performs a bitwise OR operation between x and y, then assigns the result back to x. The binary form of x = 10 is 1010 and the binary form of y = 5 is 0101. So, the bitwise OR (10 | 5) is 1 1 1 1 (Binary) = 15 (Decimal).
7. Which of the following is a correct output of the following code?
a, b, c = 19, 31, 50
a += 1
b -= 1
c *= 2
x = (10 + a)
y = x + 100
z = x + y + c
print(z)
A. 342
B. 362
C. 260
D. 360
c
Read Python assignment operator tutorial for understanding the concepts of this question.
8. What is the key difference between == and is in Python?
A. == operator compares values, while is operator checks object identity.
B. == operator checks memory addresses, while is operator compares values.
C. Both == and is operators compare values.
D. Both == and is operators check identity.
a
The equal operator (==) checks if two objects have the same value. While, the identity operator (is) checks if two variables point to the exact same object in memory.
9. Which of the following output is a correct if you execute the below code?
x = 50.0
y = 50
print(x == y)
r = '90'
s = 90
print(r == s)
A. True True
B. False True
C. True True
D. True False
d
The equality operator == compares values, not type. Python implicitly converts the integer value of y to float value 50.0 for comparison. Therefore, 50.0 == 50.0 results True. However, the equality operator does not convert types automatically. Therefore, == operator does not perform type conversion between str and int. Hence, ’90’ == 90 results False.
10. What will be the output of the following Python code?
print(not (not 5) == (not None))
A. True
B. False
C. SyntaxError
D. TypeError
a
(not 5) returns False, while (not None) returns True. So, not (False == True) is True.
11. What is the output of print(3 and 0 or 5)?
A. 0
B. 3
C. 5
D. False
c
3 is truthy value because it is a non-zero number. While, 0 is falsy value. So, 3 and 0 returns 0. 5 is truthy value and 0 is falsy value, so 0 or 5 returns 5 because 5 is truthy value.
The bitwise NOT operator inverts all bits. For -5 (two’s complement), ~-5 equals 4.
14. What will be the output of the below code
a = b = []
a.append(1)
print(b)
A. []
B. [1]
C. Error
D. None
b
Both a and b refer to the same list object, so modifying one affects the other.
15. What is the output of print(x := 5, x + 2)?
A. (5 7)
B. 7 5
C. 5 7
D. Error
c
The walrus operator added in Python 3.8+ version assigns 5 to x and returns 5, then x+2 is 7.
16. Which of the following operator is used to examine whether a value or variable is present in a sequence (such as a string, list, tuple, etc.) in Python?
A. is
B. and
C. or
D. in
d
The membership operator (in) is used to examine whether a value or variable is present in the sequence, such as string, list, etc.
17. What will be the result of the following code when executed?
my_dict = {
1: 'Orange',
2: 'Banana',
3: 'Apple'
}
print(3 in my_dict)
print('Banana' in my_dict)
A. True True
B. True False
C. False True
D. False False
b
The membership operator (in) can only test the presence of keys in the dictionary, not their values.
18. What is the output of print(0.1 + 0.2 == 0.3)?
A. True
B. False
C. SyntaxError
D. TypeError
b
Due to floating-point precision issues, 0.1 + 0.2 gives 0.30000000000000004 which is not exactly 0.3.
19. What is the output of print(+5 == ++5 == 5)?
A. True
B. False
C. SyntaxError
D. TypeError
a
+5 is just 5 because the first + is a unary plus. ++5 is not an increment in Python (unlike C/Java). So, ++5 is just +(+5), which returns 5 because the second + is another unary plus. Thus, all three (+5, ++5, 5) refer to the same integer object due to Python’s small integer caching. Therefore, the result is True.
20. Which of the following is a valid answer of the below code?
print(not not True == True)
print(1 == True != False)
A. True False
B. True True
C. False True
D. TypeError
b
Python evaluates as not (not (True == True)) β not (not True) β not False β True. Operator chaining evaluates as 1 == True and True != False, both of which are True.
21. Which of the following operator can be used to unpack the values of a list in Python?
A. *
B. **
C. #
D. &
a
The asterisk (*) operator is used to unpack iterable values in Python lists or tuples. The double asterisk (**) is used to unpack dictionary key-value pairs.
22. In Python, small integers from -5 to 256 are cached to optimize performance. Which of the following statements is True regarding this behavior?
A. Every time you create an integer object in this range, a new object is created.
B. Integers outside the range -5 to 256 are also cached by default.
C. Python does not cache any integers; it always creates a new object.
D. All variables referencing integers in the range -5 to 256 point to the same memory location.
d
In Python, small integers from -5 to 256 are internally cached for performance optimization. When you create variables with values in this range, they reference the same memory location, not separate objects.
23. What is the output of print(5 < 10 > 7)?
A. False
B. True
C. Error
D. None
b
Python supports chained comparisons. This evaluates as: (5 < 10) and (10 > 7) β True and True β True.
24. What will be the output of print((x := 10) + (x := 5) + x)?
A. 25
B. 15
C. 20
D. Error
c
(x := 10) assigns the value 10 to x and returns 10. Similarly, (x := 5) reassigns the value 5 to x and returns 5. So, the final value of x is 5. Thus, the sum of 10 + 5 + 5 = 20.
25. What is the output of print(True + True * False + False)?
Operator precedence: & > ^ > |. The binary form of 10 is 1010 and of 12 is 1100. So, 10 & 12 β 1010 & 1100 = 1000 (8 in decimal). The binary form of 7 is 0111. So, 7 ^ 8 β 0111 ^ 1000 = 1111 (15 in decimal). The binary form of 3 is 0011. Therefore, 15 | 3 β 1111 | 0011 = 1111, which is 15 in decimal number system.
The double asterisk (**) is used to unpack dictionary key-value pairs. When you use **d1 and **d2, it merges both dictionaries into a new dictionary. If there are duplicate keys, the value from the last dictionary being unpacked overwrites the previous one.
28. What is the output of print(... is Ellipsis)?
A. True
B. False
C. None
D. Error
a
The Ellipsis operator (…) is a singleton object in Python, which is often used in NumPy for slicing.
29. What is the output of:
print(-10 % 3)
A. 1
B. -1
C. 0
D. 2
d
The modulus operator (%) in Python returns the remainder when the first number is divided by the second. However, the result always has the same sign as the divisor (the second number). Division of -10 by 3 (Floor Division): -10 Γ· 3 = -3.33… Floor division (//) in Python rounds towards negative infinity, so, β10 // 3 = β4. The formula for modulus is: Dividend = (Divisor Γ Quotient) + Remainder. So, β10 = (3 Γ β4) + R. Hence, R is 2.
30. What is the output of:
class X:
def __add__(self, other):
return "overloaded"
print(X() + 10)
A. 10
B. “overloaded”
C. Error
D. None
b
__add__ is a special magic method in Python that is used to overload the + operator. By overriding __add__ in a class, we are defining custom behavior when two objects (i.e. an object and a value) are added.
class X defines the __add__ method, which takes two parameters: self and other. The self parameter defines the current object of class X and the other parameter defines a value or an object being added to self.
When Python execute X() + 10, X() creates an instance of class X with passing 10 as the other parameter in __add__. Instead of performing arithmetic addition, it returns the custom string “overloaded”.