Operator Precedence and Binding Flashcards
7 cards from real PCEP practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 7 Operator Precedence and Binding flashcards as text
What is the result of the following expression? `print(2 + 3 * 4)`
Answer: 14
In Python, the multiplication operator (`*`) has higher precedence than the addition operator (`+`). Therefore, `3 * 4` is evaluated first, resulting in 12. Then, `2 + 12` is evaluated, giving the final result of 14.
What will be printed to the console after executing this code? `print(-4 ** 2)`
Answer: -16
The exponentiation operator (`**`) has a higher precedence than the unary minus operator (`-`). The expression is evaluated as `-(4 ** 2)`, which calculates to `-(16)`, resulting in -16.
What is the output of the following expression? `print(10 // 3 % 2)`
Answer: 1
The floor division (`//`) and modulo (`%`) operators have the same precedence and are evaluated from left to right. First, `10 // 3` is calculated, which results in 3. Then, `3 % 2` is calculated, which results in 1.
What is the result of this expression? `print(2 ** 3 ** 2)`
Answer: 512
The exponentiation operator (`**`) has right-to-left associativity. This means the expression is evaluated as `2 ** (3 ** 2)`. First, `3 ** 2` is calculated to be 9, and then `2 ** 9` is calculated, which is 512.
What is the output of the following code? `print(5 * 2 > 8 and 3 + 4 == 7)`
Answer: True
First, the arithmetic and comparison expressions are evaluated. `5 * 2 > 8` becomes `10 > 8`, which is `True`. `3 + 4 == 7` becomes `7 == 7`, which is also `True`. Finally, the logical `and` operator evaluates `True and True`, resulting in `True`.
What value is assigned to `result`? `result = (2 + 3) * 5 - 1`
Answer: 24
Expressions inside parentheses are always evaluated first. `(2 + 3)` results in 5. The expression then becomes `5 * 5 - 1`. Multiplication has higher precedence, so `5 * 5` is 25. Finally, `25 - 1` is 24.
What is the output of this code? `print(not True or False)`
Answer: False
The `not` operator has higher precedence than the `or` operator. First, `not True` is evaluated to `False`. The expression then becomes `False or False`, which evaluates to `False`.