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 Python Interview Questions.
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.
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.
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.
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
.
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.
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.
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
.
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
.
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
.
a) &
b) &&
c) and
d) AND
Answer: c
Explanation: The and
keyword is used in Python to perform logical AND operation between two operands.
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
.
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
.
a) |
b) ||
c) or
d) OR
Answer: c
Explanation: The or
keyword is used in Python to perform logical OR operation between two operands.
(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
.
a) ^
b) ^^
c) xor
d) XOR
Answer: a
Explanation: The ^
operator in Python is used to perform bitwise XOR operation between two operands.
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.
(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
.
10 * (3 + 5) // 2
a) 40
b) 35
c) 20
d) 25
Answer: a
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 40.
8 / 2 + 2 * 3
a) 14
b) 10
c) 12
d) 16
Answer: b
Explanation: Multiplication and division have the same precedence and are evaluated from left to right, followed by addition. So, the expression is evaluated as 8 / 2 + 2 * 3
, resulting in 10.
5 + 2 * 3 ** 2
a) 35
b) 23
c) 25
d) 17
Answer: b
Explanation: Exponentiation has the highest precedence, followed by multiplication, and then addition. So the expression is evaluated as 5 + 2 * (3 ** 2)
, resulting in 23.
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.
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”.
-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.
8 % 3 + 2 ** 2 * (2 + 2)
a) 21
b) 20
c) 19
d) 18
Answer: d
Explanation: Parentheses are evaluated first, followed by exponentiation, multiplication, modulus, and then addition. The expression simplifies to 8 % 3 + 4 * 4
, which results in 18.
(3 + 2) * 4 / 2 ** 2
a) 12.0
b) 5.0
c) 10.0
d) 6.0
Answer: b
Explanation: Parentheses are evaluated first, followed by exponentiation, multiplication, and then division. The expression is evaluated as (3 + 2) * 4 / 2 ** 2
, resulting in 5.0.
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.
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.
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.
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
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.
a) 2.56
b) 2.6
c) 2.57
d) 2.564
Answer: a
Explanation: The round()
function rounds the number to the specified number of decimal places. In this case, round(2.564, 2)
rounds to two decimal places, resulting in 2.56.
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.
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:
Evil Site, Good Questions, incorrect answers please fix thanks for the blinding