Operator Precedence and Associativity practice quetion :- Question 1 (Basic Arithmetic) -
What is the output?
* has higher precedence than +
So 5 * 2 = 10, then 10 + 10 = 20
(Hindi: गुणा को पहले प्राथमिकता मिलती है।)
Question 2 (Exponentiation - Right-to-Left)
What is the output?
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?
Answer: 40.0
Explanation:
/ and * have same precedence, left-to-right associativity
(100 / 5) * 2 = 20.0 * 2 = 40.0
(Hindi: बराबर प्राथमिकता, बाएँ‑से‑दाएँ।)
Question 4 (Floor Division & Modulo)
What is the output?
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:
So Answer: 0
Question 5 (Comparison Operators)
What is the output?
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?
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?
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?
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?
Answer: 3
Explanation:
& has higher precedence than |
5 & 3 = 1
1 | 2 = 3
(Hindi: & को | से पहले प्राथमिकता।)
Question 10 (Ternary Operator)
What is the output?
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?
Answer: True
Explanation (step-by-step):
5 * 2 = 10
10 + 10 = 20
30 // 3 = 10
Now: 20 > 20 = False
10 == 10 = True
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?
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