Welcome to our Python Operators and Expressions quiz! Understanding operators and expressions is crucial in Python programming as they form the foundation for manipulating data and controlling program flow. This quiz will test your knowledge of various types of operators such as arithmetic, comparison, and logical operators, as well as how expressions are evaluated in Python. Get ready to challenge yourself and deepen your understanding of these fundamental concepts!
30+ MCQs on Python Operators and Expressions
Q1. Which operator is used to calculate the remainder of a division?
a) %
b) //
c) /
d) **
Answer: a
Explanation: The %
operator in Python is used to calculate the remainder of a division operation. For example, 10 % 3
results in 1
because 10 divided by 3 equals 3 with a remainder of 1.
Q2. What is the result of the expression 4 < 5 and 5 < 6
?
a) True
b) False
c) Error
d) None of the above
Answer: a
Explanation: The expression 4 < 5 and 5 < 6
evaluates to True
because both conditions are true. In Python, the and
operator returns True
only if both conditions on its left and right are true.
Q3. What will be the output of the following code?
print(3 ** 3)
a) 9
b) 27
c) 81
d) 6
Answer: b
Explanation: The code 3 ** 3
calculates 3 raised to the power of 3, which is equal to 27. Therefore, the output will be 27.
Q4. What does the expression not(10 == 10)
evaluate to?
a) True
b) False
c) Error
d) None of the above
Answer: b
Explanation: The expression 10 == 10
evaluates to True
because 10 is indeed equal to 10. The not
operator negates this result, so not(10 == 10)
evaluates to False
.
Q5. Which operator is used to perform floor division?
a) %
b) //
c) /
d) **
Answer: b
Explanation: The //
operator in Python is used to perform floor division, which returns the largest integer less than or equal to the quotient of the division.
Q6. What will be the output of the following code?
print(9 % 4)
a) 1
b) 2
c) 3
d) 0
Answer: a
Explanation: The %
operator calculates the remainder of the division operation. Here, 9 % 4
results in 1
because 9 divided by 4 equals 2 with a remainder of 1.
Q7. What does the expression 3 != 3 or 5 > 4
evaluate to?
a) True
b) False
c) Error
d) None of the above
Answer: a
Explanation: The expression 3 != 3
evaluates to False
because 3 is indeed equal to 3. However, 5 > 4
evaluates to True
. Since it is connected by or
, if either condition is True
, the whole expression evaluates to True
.
Q8. What will be the output of the following code?
print(7 // 2)
a) 3.5
b) 4
c) 3
d) 2
Answer: c
Explanation: The //
operator performs floor division, returning the largest integer less than or equal to the quotient of the division. Here, 7 // 2
results in 3
.
Q9. What does the expression not(3 < 2)
evaluate to?
a) True
b) False
c) Error
d) None of the above
Answer: a
Explanation: The expression 3 < 2
evaluates to False
because 3 is not less than 2. The not
operator negates this result, so not(3 < 2)
evaluates to True
.
Q10. Which operator is used to perform logical AND operation?
a) &
b) &&
c) and
d) AND
Answer: c
Explanation: The and
keyword is used in Python to perform logical AND operation between two operands.
Q11. What will be the output of the following code?
print(2 ** 0)
a) 1
b) 0
c) 2
d) 3
Answer: a
Explanation: Any number raised to the power of 0 equals 1. So, 2 ** 0
results in 1
.
Q12. What does the expression 10 == 10 and 5 > 6
evaluate to?
a) True
b) False
c) Error
d) None of the above
Answer: b
Explanation: The expression 10 == 10
evaluates to True
because 10 is equal to 10. However, 5 > 6
evaluates to False
. Since it is connected by and
, both conditions must be True
for the whole expression to be True
.
Q13. Which operator is used to perform logical OR operation?
a) |
b) ||
c) or
d) OR
Answer: c
Explanation: The or
keyword is used in Python to perform logical OR operation between two operands.
Q14. What does the expression (5 != 5) or (6 >= 6)
evaluate to?
a) True
b) False
c) Error
d) None of the above
Answer: a
Explanation: The expression (5 != 5)
evaluates to False
because 5 is equal to 5. However, (6 >= 6)
evaluates to True
. Since it is connected by or
, if either condition is True
, the whole expression evaluates to True
.
Q15. Which operator is used to perform bitwise XOR operation?
a) ^
b) ^^
c) xor
d) XOR
Answer: a
Explanation: The ^
operator in Python is used to perform bitwise XOR operation between two operands.
Q16. Which operator is used to perform left shift?
a) <<
b) >>
c) <<>>
d) LSH
Answer: a
Explanation: The <<
operator in Python is used to perform left shift operation on the binary representation of a number.
Q17. What does the expression (6 > 5) or (7 <= 7)
evaluate to?
a) True
b) False
c) Error
d) None of the above
Answer: a
Explanation: Both conditions (6 > 5)
and (7 <= 7)
are True
, and since they are connected by or
, the whole expression evaluates to True
.
Q18. What is the result of the following expression in Python?
10 * (3 + 5) // 2
a) 40
b) 35
c) 20
d) 25
Answer: c
Explanation: Parentheses have higher precedence than multiplication, which has higher precedence than floor division, so the expression is evaluated as 10 * (3 + 5) // 2, resulting in 20.
Q19. What is the result of the following expression in Python?
8 / 2 + 2 * 3
a) 14
b) 10
c) 12
d) 16
Answer: c
Explanation: Multiplication and division have the same precedence and are evaluated from left to right, so the expression is evaluated as 8 / 2 + 2 * 3, resulting in 12.
Q20. What is the result of the following expression in Python?
5 + 2 * 3 ** 2
a) 35
b) 23
c) 25
d) 17
Answer: c
Explanation: Exponentiation has higher precedence than multiplication, which has higher precedence than addition, so the expression is evaluated as 5 + (2 * 3 ** 2), resulting in 25.
Q21. What is the result of the following expression in Python?
10 > 5 < 2
a) True
b) False
c) 7
d) Error
Answer: b
Explanation: Chained comparison operators are evaluated left to right, so 10 > 5 < 2 evaluates to False.
Q22. What is the result of the following expression in Python?
3 * "Hello"
a) “HelloHelloHelloHello”
b) “Hello 3 times”
c) “HelloHelloHello”
d) Error
Answer: a
Explanation: The multiplication (*) operator repeats the string “Hello” three times, resulting in “HelloHelloHello”.
Q23. What is the result of the following expression in Python?
-5 // 2
a) -2
b) -3
c) 2
d) 3
Answer: b
Explanation: Floor division always rounds towards negative infinity, so -5 // 2 equals -3.
Q24. What will be the result of the following expression?
8 % 3 + 2 ** 2 * (2 + 2)
a) 21
b) 20
c) 19
d) 18
Answer: b
Explanation: First, 8 % 3
results in 2
. Then, 2 ** 2
results in 4
. Next, (2 + 2)
results in 4
. So, the expression becomes 2 + 4 * 4
, which equals 20
.
Q25. What does the following expression evaluate to?
(3 + 2) * 4 / 2 ** 2
a) 12.0
b) 5.0
c) 10.0
d) 6.0
Answer: a
Explanation: Parentheses are evaluated first, so (3 + 2)
becomes 5
. Then, 2 ** 2
is 4
. After that, 5 * 4
is 20
, and finally, 20 / 4
equals 5.0
.
Q26. What will be the output of the following code snippet?
x = 5
y = x * 2 if x < 10 else x / 2
print(y)
a) 10
b) 2.5
c) 5
d) 25
Answer: a
Explanation: Since x
is less than 10, the expression x * 2
is evaluated, resulting in 10.
Q27. What does the expression bool(0)
evaluate to?
a) True
b) False
c) None
d) Error
Answer: b
Explanation: In Python, 0 is considered as False
when converted to a boolean using the bool()
function.
Q28. What does the expression len(‘Python’) return?
a) 7
b) 6
c) 8
d) 5
Answer: b
Explanation: The len() function returns the length of a string, so len(‘Python’) returns 6.
Q29. What will be the value of y
after executing the following code snippet?
x = 5
y = x if x < 10 else x / 2
a) 5
b) 2.5
c) 10
d) Error
Answer: a
Explanation: Since x
is less than 10, the value of y
will be x
, which is 5
Q30. What is the output of the expression min(4, -2, 7, 1)
?
a) 4
b) -2
c) 7
d) 1
Answer: b
Explanation: The min()
function returns the smallest of the input values. So, min(4, -2, 7, 1)
returns -2.
Q31. What is the output of the expression round(2.564, 2)?
a) 2.56
b) 2.6
c) 2.57
d) 2.564
Answer: b
Explanation: The round() function rounds the given number to the specified number of digits after the decimal point. So, round(2.564, 2) returns 2.56.
Q32. What is the output of the expression abs(-5.5)?
a) -5.5
b) 5.5
c) -5
d) 5
Answer: b
Explanation: The abs() function returns the absolute (positive) value of a number. So, abs(-5.5) equals 5.5.
Q33. What will be the value of y
after executing the following code snippet?
x = 5
y = x if x != 5 else x + 2
a) 5
b) 7
c) 10
d) Error
Answer: b
Explanation: Since x
is equal to 5, the value of y
will be x + 2
, which is 7.
Congratulations on completing the Python Operators and Expressions quiz! We hope you found the questions both challenging and informative. Mastering operators and expressions is essential for writing efficient and error-free Python code. Whether you aced the quiz or encountered some challenges, use this experience to further enhance your understanding of Python programming. Keep practicing, exploring, and experimenting with Python, and you’ll continue to grow as a proficient programmer. Keep coding and happy learning!
You can also enroll in out free Python Course Today!
Read our more articles related to MCQs in Python: