Question 1
What will be the output of the following Python code snippet?
a = 7.5
b = 2.0
c = 3
d = -3.0
e = a // b
f = d // c
print(e + f)
$e = 7.5 // 2.0 = 3.0. f = -3.0 // 3 = -1.0. Therefore, e + f = 3.0 + (-1.0) = 2.0.$
What will be the output of the following Python code snippet?
a = 7.5
b = 2.0
c = 3
d = -3.0
e = a // b
f = d // c
print(e + f)
$e = 7.5 // 2.0 = 3.0. f = -3.0 // 3 = -1.0. Therefore, e + f = 3.0 + (-1.0) = 2.0.$
Which of the following represents the correct ascending order of operator precedence in Python (from lowest to highest precedence)?
1. not in
2. <<
3. not
4. ^
In Python, logical 'not' has lowest precedence, followed by membership operator 'not in', bitwise XOR '^', and bitwise left shift '<<' (highest among these four). Hence order is 3, 1, 4, 2.
What is the result of evaluating the expression in Python?
result = 18 & 5 + 3 << 2 ^ 24 + 4 // 5
Precedence: addition and floor division are evaluated first: 5+3=8 and 4//5=0, so 24+0=24. Then 8 << 2 = 32. The expression becomes 18 & 32 ^ 24. Bitwise AND has higher precedence than XOR, so 18 & 32 = 0, and 0 ^ 24 = 24.
Consider the following statement: 'Python evaluates expression A ** B ** C from right to left, whereas expression A + B - C is evaluated from left to right.' Which of the following options accurately describes operator associativity?
Exponentiation operator (**) in Python is right-associative (right-to-left), while addition (+) and subtraction (-) are left-associative (left-to-right).
What will be the output of the following code segment?
a = 0o37
b = a << 2
c = a >> 2
print(b + c)
0o37 in octal is 3*8 + 7 = 31 in decimal. b = 31 << 2 = 124. c = 31 >> 2 = 7. b + c = 124 + 7 = 131.