PCEP Operator Precedence and Binding 3 — Questions and Answers
Question 1: What is the result of `-(2 ** 2)`?
- -4 (Correct answer)
- 4
- -8
- Error
Correct answer: -4
Exponentiation `**` has higher precedence than unary minus, so `2**2=4` first, then `-4`.
Question 2: Which expression is evaluated first in `a = 1 + 2 * 3 ** 1 - 4 // 2`?
- 3 ** 1 (Correct answer)
- 2 * 3
- 4 // 2
- 1 + 2
Correct answer: 3 ** 1
Exponentiation `**` has the highest precedence among arithmetic operators, so `3**1` is evaluated first.
Question 3: What is the value of `5 - 2 - 1`?
- 2 (Correct answer)
- 4
- 0
- Error
Correct answer: 2
Subtraction is left-associative: `(5-2)-1 = 3-1 = 2`.
Question 4: What does `1 < 2 < 3` evaluate to in Python?
- True (Correct answer)
- False
- Error
- None
Correct answer: True
Python supports chained comparisons; `1 < 2 < 3` is equivalent to `(1 < 2) and (2 < 3)` which is True.
Question 5: What is the result of `8 / 2 * 2`?
- 8.0 (Correct answer)
- 2.0
- 1.0
- 16.0
Correct answer: 8.0
`/` and `*` have the same precedence and are left-associative: `(8/2)*2 = 4.0*2 = 8.0`.
Question 6: In Python, what does `not 0 == 0` evaluate to?
- False (Correct answer)
- True
- Error
- None
Correct answer: False
Comparison `==` has higher precedence than `not`, so `0==0` is True first, then `not True` equals False.
Question 7: What is the result of `2 ** -1`?
- 0.5 (Correct answer)
- -2
- Error
- 0
Correct answer: 0.5
Python evaluates `2 ** -1` as `1/2 = 0.5` since `**` accepts negative exponents.
What is the result of `-(2 ** 2)`?