Python Operators and Expressions 3 — Questions and Answers
Question 1: What is the result of `5 // -2` in Python?
- -3 (Correct answer)
- -2
- 2
- -2.5
Correct answer: -3
Floor division rounds toward negative infinity, so `5 // -2` is `-2.5` rounded down to `-3`.
Question 2: What does the bitwise XOR operator `^` return for `6 ^ 3`?
- 5 (Correct answer)
- 7
- 2
- 1
Correct answer: 5
`6` is `110` and `3` is `011` in binary; XOR gives `101`, which is `5`.
Question 3: What is the result of `[1, 2] + [3, 4]`?
- [1, 2, 3, 4] (Correct answer)
- [4, 6]
- [1, 2, [3, 4]]
- Raises a TypeError
Correct answer: [1, 2, 3, 4]
The `+` operator on lists performs concatenation, combining both lists into a single new list `[1, 2, 3, 4]`.
Question 4: What is the precedence order (highest to lowest) for these operators: `+`, `**`, `*`?
- **, *, + (Correct answer)
- *, **, +
- +, *, **
- **, +, *
Correct answer: **, *, +
Python's operator precedence places `**` (exponentiation) highest, then `*` (multiplication), then `+` (addition).
Question 5: What does `x = 10; x &= 6` result in for `x`?
- 2 (Correct answer)
- 14
- 4
- 16
Correct answer: 2
`10` is `1010` and `6` is `0110` in binary; bitwise AND gives `0010`, which is `2`.
Question 6: What is the result of evaluating `3 < 5 > 2` in Python?
- True (Correct answer)
- False
- Raises a SyntaxError
- Raises a TypeError
Correct answer: True
Python supports chained comparisons; `3 < 5 > 2` is evaluated as `3 < 5 and 5 > 2`, which is `True and True = True`.
Question 7: What is the result of `divmod(17, 5)`?
- (3, 2) (Correct answer)
- (2, 3)
- (3, 5)
- (17, 5)
Correct answer: (3, 2)
`divmod(17, 5)` returns a tuple of `(quotient, remainder)`, which is `(3, 2)` since `17 = 5*3 + 2`.
What is the result of `5 // -2` in Python?