Important Notice:

practice quetion

practice quetion

25 views 4 min read

Operator Precedence and Associativity practice quetion :- Question 1 (Basic Arithmetic) -

What is the output?

python-
result = 10 + 5 * 2
print(result)
Answer: 20
Explanation:
  • * has higher precedence than +

  • So 5 * 2 = 10, then 10 + 10 = 20
    (Hindi: गुणा को पहले प्राथमिकता मिलती है।)

Question 2 (Exponentiation - Right-to-Left)

What is the output?

python
result = 2 ** 3 ** 2
print(result)

Answer: 512
Explanation:

  • ** has right-to-left associativity in Python.

  • So it's evaluated as 2 ** (3 ** 2) = 2 ** 9 = 512

  • Not (2 ** 3) ** 2 = 8 ** 2 = 64
    (Hindi: ** दाएँ‑से‑बाएँ होता है, इसलिए पहले 3**2 फिर 2**9।)

Question 3 (Division & Multiplication - Same Precedence)

What is the output?

python
result = 100 / 5 * 2
print(result)

Answer: 40.0
Explanation:

  • / and * have same precedenceleft-to-right associativity

  • (100 / 5) * 2 = 20.0 * 2 = 40.0
    (Hindi: बराबर प्राथमिकता, बाएँ‑से‑दाएँ।)

Question 4 (Floor Division & Modulo)

What is the output?

python
result = 17 // 5 % 3
print(result)

Answer: 1
Explanation:

  • // and % have same precedence, left-to-right

  • First 17 // 5 = 3

  • Then 3 % 3 = 0... Wait, that's not right. Let me recalculate:

Actually:

  • 17 // 5 = 3

  • 3 % 3 = 0 → Answer should be 0

Let me correct:

python
result = 17 // 5 % 3 # 3 % 3 = 0 print(result) # 0

So Answer: 0

Question 5 (Comparison Operators)

What is the output?

python
result = 5 < 10 > 7
print(result)

Answer: True
Explanation:

  • Comparison operators (<>) have same precedence and left-to-right associativity but Python supports chaining

  • 5 < 10 > 7 means (5 < 10) and (10 > 7) = True and True = True

Question 6 (Logical Operators - Precedence)

What is the output?

python
result = True or False and False
print(result)

Answer: True
Explanation:

  • In Python, and has higher precedence than or

  • So False and False = False

  • Then True or False = True
    (Hindi: and को or से पहले प्राथमिकता मिलती है।)

Question 7 (Not Operator)

What is the output?

python
result = not True or False
print(result)

Answer: False
Explanation:

  • not has higher precedence than or

  • not True = False

  • False or False = False
    (Hindi: not पहले आता है, फिर or।)

Question 8 (Membership & Comparison)

What is the output?

python
result = 5 in [1, 2, 3, 4, 5] == True
print(result)

Answer: False
Explanation:

  • in and == have same precedence and support chaining

  • 5 in [1,2,3,4,5] is True

  • Then True == True is True... Actually wait:

Python chains it as:
(5 in [1,2,3,4,5]) and ([1,2,3,4,5] == True)

  • First part: True

  • Second part: [1,2,3,4,5] == True is False

  • So result = False
    (Hindi: चेनिंग की वजह से दोनों कंडीशन चेक होती हैं।)

Question 9 (Bitwise Operators)

What is the output?

python
result = 5 & 3 | 2
print(result)

Answer: 3
Explanation:

  • & has higher precedence than |

  • 5 & 3 = 1

  • 1 | 2 = 3
    (Hindi: & को | से पहले प्राथमिकता।)

Question 10 (Ternary Operator)

What is the output?

python
a = 5
b = 10
result = a if a > b else b
print(result)

Answer: 10
Explanation:

  • Ternary operator has lower precedence than comparison

  • a > b is evaluated first: 5 > 10 = False

  • So else part executes: b = 10

Question 11 (Combining Arithmetic & Logical)

What is the output?

python
result = 10 + 5 * 2 > 20 and 30 // 3 == 10
print(result)

Answer: True
Explanation (step-by-step):

  1. 5 * 2 = 10

  2. 10 + 10 = 20

  3. 30 // 3 = 10

  4. Now: 20 > 20 = False

  5. 10 == 10 = True

  6. False and True = False

Wait, I made a mistake. Let me recalculate:

Actually:

  • 10 + 5 * 2 > 20 → 10 + 10 > 20 → 20 > 20 → False

  • 30 // 3 == 10 → 10 == 10 → True

  • False and True = False

Correct Answer: False

Question 12 (Identity Operators)

What is the output?

python
a = [1, 2, 3]
b = [1, 2, 3]
result = a is b or a == b
print(result)

Answer: True
Explanation:

  • is and == have same precedence, left-to-right

  • a is b → False (different objects)

  • a == b → True (same content)

  • False or True = True

Related Notes