Arithmetic Operators & Precedence - Python

  1. Which expression evaluates to 2.5 in Python?
    Answer: 5 / 2 The / operator does true division, so 5 / 2 is 2.5. 5 // 2.0 is tempting because one operand is a float, but // still rounds down, giving 2.0.
  2. What value is assigned to value?
    Answer: 22 When Python evaluates an expression with multiple operators, it follows a strict order of operations: exponentiation (**) first, then floor division (//) and modulo (%) left to right, and finally addition and subtraction. This question tests whether you apply that order correctly. Starting with 70 % 4 ** 2 // 3, Python first evaluates the exponent: 42=164^2 = 16, giving you 70 % 16 // 3. Now, % and // have equal precedence, so Python works left to right: 70mod  16=670 \mod 16 = 6 (since 16×4=6416 \times 4 = 64 and 70−64=670 - 64 = 6), leaving 6 // 3. Finally, 6÷3=26 \div 3 = 2, so value = 2, confirming D is correct. Choice A (1) likely comes from applying floor division before modulo — doing 16 // 3 = 5 first, then 70 % 5 = 0... actually it reflects a different incorrect ordering. Choice B (0) might come from mistakenly computing 70 % 4 = 2, then 2 ** 2 = 4, then 4 // 3 = 1, or some other reordering that collapses the expression to zero. Choice C (6) is a tempting trap — it's the correct intermediate result of 70 % 16, but the student who picks it forgets to complete the final // 3 step. A useful tip: when you see chained operators of the same precedence level, always resolve them left to right. Write out each step explicitly on scratch paper — skipping steps is the most common source of errors on operator precedence questions.
  3. Which value and type are assigned to result?
    Answer: 6.06.0, a float When evaluating complex Python expressions, you need to apply two key rules simultaneously: operator precedence and type propagation. Work through the expression 84 / (2 * 3 + 1) // 2 step by step. First, resolve the parentheses: 2×3+1=72 \times 3 + 1 = 7. Now you have 84 / 7 // 2. Next, since / and // share the same precedence level, Python evaluates left to right: 84 / 7 uses the true division operator, which always returns a float — giving 12.0. Finally, 12.0 // 2 applies floor division, which truncates to the nearest whole number. However, because one operand (12.0) is already a float, the result stays a float: 6.0. So result is 6.0, a float — confirming D. A (12.0) is wrong because it stops after the division step and ignores the // 2 floor division that follows. B (6, an int) gets the numeric value right but misses the critical type rule: once / produces a float, subsequent // operations preserve the float type — Python does not silently convert back to int. C (14.0) reflects a precedence error, likely skipping the parentheses and computing incorrectly; no valid parsing of this expression yields 14. The key study tip: in Python, / always produces a float, and that "float contamination" carries through the rest of the expression — even floor division // won't convert it back to int. Watch for chains of operators and always track the type alongside the value.
  4. What value is assigned to value?
    Answer: −0.5-0.5 When you see a complex expression like this, the key concept being tested is operator precedence — specifically how Python handles unary negation, exponentiation (**), and the order they interact. Here's the critical rule: in Python, ** has higher precedence than unary minus (-). This means -3 ** 2 is evaluated as -(3 ** 2) = -9, not (-3) ** 2 = 9. Also, ** is right-associative, and a negative exponent like 2 ** -1 evaluates to 2−1=0.52^{-1} = 0.5. Breaking down the full expression value = -3 ** 2 + (-3) ** 2 - 2 ** -1: - -3 ** 2 → -(3 ** 2) → −9-9 - (-3) ** 2 → (−3)2=9(-3)^2 = 9 (parentheses force the negative into the base) - 2 ** -1 → 2−1=0.52^{-1} = 0.5 So: −9+9−0.5=−0.5-9 + 9 - 0.5 = -0.5, confirming D is correct. A (17.5) likely comes from misreading -3 ** 2 as 9 and adding 0.5 instead of subtracting it. B (−18.5) treats both -3 ** 2 and (-3) ** 2 as -9, giving -9 + (-9) - 0.5. C (0) ignores the 2 ** -1 term entirely and incorrectly cancels the first two terms as equal. Study tip: Always remember that -x ** 2 in Python is -(x ** 2). If you want the negative base squared, you must use parentheses: (-x) ** 2. This is one of Python's most common exam traps.
  5. What value is assigned to result?
    Answer: −8-8 When you see Python's // operator, remember it performs floor division — it always rounds toward negative infinity, not toward zero. This distinction is critical when negative numbers are involved. Take the first part: −17//5-17 // 5. The true result of −17÷5-17 \div 5 is −3.4-3.4. Floor division rounds down (toward negative infinity), so the result is −4-4, not −3-3. Now the second part: 17//−517 // -5. The true result of 17÷−517 \div -5 is −3.4-3.4. Again, floor division rounds toward negative infinity, giving −4-4, not −3-3. Adding them together: (−4)+(−4)=−8(-4) + (-4) = -8, confirming the correct answer is C. As for the wrong answers — A (00) likely comes from assuming the two expressions cancel each other out symmetrically, which they don't since both round the same direction. B (−6-6) comes from truncating toward zero instead of flooring — if you incorrectly used −3-3 for both operations, you'd get −3+(−3)=−6-3 + (-3) = -6. D (−7-7) might come from flooring one expression correctly (−4-4) but truncating the other incorrectly (−3-3), giving a mixed result. A useful tip: whenever you do floor division with a negative result in Python, ask yourself "which integer is further from zero?" — that's the floor. Unlike truncation (which moves toward zero), floor division always moves toward the more negative integer. Burn this in by testing // in a Python shell with a few negative examples.
  6. Which value and type are assigned to value?
    Answer: 16.016.0, a float When Python evaluates an expression with multiple operators at the same precedence level — like /, //, and * — it processes them left to right. This question tests that rule combined with how Python handles integer vs. float arithmetic. Starting with 48 / 3 // 2 * 2, work through it step by step. First, 48 / 3 uses true division, which always returns a float: 48/3=16.048 / 3 = 16.0. Next, 16.0 // 2 is floor division — but because one operand is a float, the result stays a float: 16.0//2=8.016.0 // 2 = 8.0. Finally, 8.0 * 2 again keeps the float: 8.0×2=16.08.0 \times 2 = 16.0. So value is 16.016.0, a float — confirming C is correct. Choice A, 8.08.0, would result if you stopped after the floor division step and forgot to apply the final multiplication. Choice B, 1616 as an int, tempts students who assume that floor division // "converts" the result to an integer — it doesn't when a float is involved; the result mirrors the type of its operands. Choice D, 4.04.0, might arise from incorrectly reordering operations, perhaps applying // before /, which would give 48 // 3 = 16, then 16 / 2 = 8.0, then 8.0 * 2 = 16.0 — wait, actually 4.04.0 has no clean path, suggesting a parenthesization error. Study tip: Remember that in Python, / is a "float infector" — once it produces a float, subsequent operations in that chain stay floats, even //.
  7. Which statement about Python's ** operator is true?
    Answer: It groups right to left, so 2 ** 3 ** 2 is 512 ** is right-associative, so 2 ** 3 ** 2 means 2 ** (3 ** 2), which is 2 9 = 512. Grouping left to right, as most operators do, would give 8 2 = 64, but that is not how Python groups **.
  8. What value is assigned to result?
    Answer: 88 When you see a complex expression like 2 ** 3 ** 2 // 64, the key concept being tested is operator precedence and associativity. Python evaluates exponentiation (**) right to left (right-associative), and then floor division (//) left to right — and both come before floor division in precedence order. Start from the rightmost exponent: 3∗∗2=93 ** 2 = 9. Then evaluate the remaining exponentiation: 2∗∗9=5122 ** 9 = 512. Finally, apply floor division: 512//64=8512 // 64 = 8. So result is assigned 8, confirming B is correct. A (1) reflects a mistake where you might incorrectly apply floor division first — 2∗∗(3//2)//642 ** (3 // 2) // 64 would give 2∗∗1//64=02 ** 1 // 64 = 0, which isn't even among the choices. A student picking 1 likely evaluated 8//8=18 // 8 = 1 by miscomputing intermediate steps, perhaps reading exponentiation left to right: (2∗∗3)∗∗2=512(2 ** 3) ** 2 = 512, then 512//512512 // 512... still not quite 1, showing muddled thinking. C (4) likely comes from computing (2∗∗3)∗∗2=64(2 ** 3) ** 2 = 64 incorrectly as 64 and then doing 64//16=464 // 16 = 4, combining multiple errors. D (512) is the result of stopping too early — computing 2∗∗9=5122 ** 9 = 512 and forgetting to apply the // floor division at the end. Strategy: Always remember ** is right-associative in Python — evaluate chains like a ** b ** c as a ** (b ** c), not left to right. Write out each step to avoid careless errors.
  9. Which expression evaluates to 20 in Python?
    Answer: (2 + 3) * 4 Parentheses make 2 + 3 happen first, giving 5 * 4 = 20. In 2 + 3 * 4 the multiplication happens before the addition, so it gives 14. Reading it left to right is what makes it look like 20.
  10. What does print(-7 // 2 * 3) display?
    Answer: -12 // and * have equal precedence, so they run left to right. -7 // 2 floors -3.5 down to -4, and -4 * 3 = -12. The answer -9 comes from truncating toward zero, but // always rounds down.
  11. What value is assigned to result?
    Answer: 44 When you see a compound arithmetic expression in Python, your first instinct should be to apply operator precedence — Python doesn't evaluate left to right; it follows a strict order: exponentiation first, then multiplication/division/floor division, then addition/subtraction. Let's walk through 18 // 4 * 3 - 2 ** 3 step by step. First, exponentiation: 2∗∗3=82 ** 3 = 8. Now the expression becomes 18 // 4 * 3 - 8. Next, multiplication and floor division share the same precedence level and are evaluated left to right: 18//4=418 // 4 = 4 (floor division drops the decimal), then 4∗3=124 * 3 = 12. Finally, subtraction: 12−8=412 - 8 = 4. So A) 4 is correct. As for the wrong answers — D) 6 likely comes from mishandling precedence, perhaps computing 18//(4∗3)=118 // (4 * 3) = 1, then 1−8=−71 - 8 = -7... which actually leads to B) -7, a trap for students who incorrectly group the multiplication with the divisor before performing floor division. B) -7 results from that exact mistake: treating 4 * 3 as the divisor. C) 5.5 reflects confusing // (floor division) with / (true division) — if you used regular division, 18/4=4.518 / 4 = 4.5, and 4.5∗3−8=5.54.5 * 3 - 8 = 5.5. A useful memory trick: think PEMDAS, but remember that in Python, // and % sit alongside * and / at the same level, all evaluated left to right. Always resolve exponents before anything else.
  12. What value is assigned to total?
    Answer: 2323 When Python evaluates an expression, it follows operator precedence — a strict order of operations. The hierarchy from highest to lowest priority is: exponentiation (**), then multiplication/modulo (*, %), then addition/subtraction (+, -). Operators at the same level evaluate left to right. Apply this to total = 7 + 20 % 6 * 2 ** 3: Start with the highest precedence — the exponent: 23=82^3 = 8. The expression becomes 7+20%6∗87 + 20 \% 6 * 8. Next, handle % and * left to right: 20%6=220 \% 6 = 2 (since 20 divided by 6 leaves a remainder of 2), giving 7+2∗87 + 2 * 8. Then 2∗8=162 * 8 = 16, leaving 7+16=237 + 16 = 23. The correct answer is B. Each wrong answer reflects a specific mistake. A (19) likely comes from misapplying modulo — for example, computing 20%6=220 \% 6 = 2 correctly but then adding before multiplying: 7+2=97 + 2 = 9, then 9∗8=...9 * 8 = ... or some similar misordering. C (27) could result from treating 20 % 6 as 20 divided normally, perhaps calculating 20/6≈320 / 6 \approx 3, then 3∗8=243 * 8 = 24... or misreading the modulo entirely. D (24) might arise from ignoring operator precedence and evaluating strictly left to right: 7+20=277 + 20 = 27, 27%6=327 \% 6 = 3, 3∗2=63 * 2 = 6, 63=2166^3 = 216 — or some other left-to-right misreading. A reliable strategy: whenever you see a complex expression, rewrite it with parentheses reflecting precedence before calculating. This eliminates ordering errors entirely.
  13. Which of these expressions raises a ZeroDivisionError in Python?
    Answer: 7 // 0 Any division or modulo by zero raises ZeroDivisionError, and 7 // 0 divides by zero. 0 ** 0 is tempting because it is often called undefined in math, but Python defines it as 1 and raises no error.
  14. What value is assigned to code?
    Answer: 2727 When Python evaluates the modulo operator (%) with mixed signs, it follows a specific rule: the result always takes the sign of the divisor (the right-hand number). This is different from many other languages, and it's exactly what this question tests. Start with −17mod  5-17 \mod 5. Since the divisor is positive (5), the result must be non-negative. Python computes this as −17=5×(−4)+3-17 = 5 \times (-4) + 3, so −17mod  5=3-17 \mod 5 = 3. Multiplying: 3×10=303 \times 10 = 30. Now evaluate 17mod  −517 \mod -5. Since the divisor is negative (−5), the result must be non-positive. Python computes 17=−5×(−4)+(−3)17 = -5 \times (-4) + (-3), so 17mod  −5=−317 \mod -5 = -3. Adding both parts: 30+(−3)=2730 + (-3) = 27, which is answer A. Choice B (3232) likely comes from treating both modulo results as positive — using 33 and 22 — a mistake from applying C-style modulo rules where results follow the dividend's sign. Choice C (−18-18) results from incorrectly computing −17mod  5=−2-17 \mod 5 = -2 and 17mod  −5=−317 \mod -5 = -3, ignoring Python's sign convention entirely. Choice D (1717) may arise from arithmetic errors when combining the two parts or misreading the expression structure. A useful tip: whenever you see modulo with a negative number in Python, immediately ask "what sign is the divisor?" — the result inherits that sign. Practice a few examples with negative divisors and dividends separately until this feels automatic.
  15. What value is assigned to score?
    Answer: 7.57.5 When you see an expression mixing //, /, and %, you're being tested on Python's arithmetic operators and operator precedence. All three operators share the same precedence level and evaluate left to right, so break the expression into three separate operations first. Starting with score = 9 // 2 + 9 / 2 - 9 % 2, evaluate each operator independently: - 9 // 2 is floor division (integer division, discarding the remainder): 9÷2=4.5→49 \div 2 = 4.5 \rightarrow 4 - 9 / 2 is true division (always returns a float): 9÷2=4.59 \div 2 = 4.5 - 9 % 2 is the modulo (remainder after division): 9=4×2+1→19 = 4 \times 2 + 1 \rightarrow 1 Now combine: 4+4.5−1=7.54 + 4.5 - 1 = 7.5, which is answer C. As for the wrong answers: A (7) likely comes from treating 9 / 2 as floor division (getting 4 instead of 4.5), making everything integer math and producing 7. B (8.5) probably results from forgetting the modulo term entirely, computing only 4+4.5=8.54 + 4.5 = 8.5. D (8.0) might come from miscalculating the modulo as 0 (perhaps confusing 9 % 2 with 8 % 2), giving 4+4.5−0=8.54 + 4.5 - 0 = 8.5, or from other arithmetic slips that land near the right neighborhood. A key tip: in Python 3, / always returns a float — never assume it behaves like //. When even one operand in a chain produces a float, the final result will be a float too.

Start this quiz