Sample questions
Question 1Easy
What is the output?
print(True + True + True, True * 10)
- 1 10
- 3 10
- 3 True
- True True
Answer: B. 3 10
Explanation
True behaves as the integer 1 in arithmetic.
Step-by-step solution
- True counts as 1 in arithmetic, so True + True + True is 1 + 1 + 1 = 3.
- True * 10 is 1 × 10 = 10, which is an int.
- So the output is 3 10, the correct answer.
Why the other options are wrong
- A. 1 10 is wrong; three Trues add up to 3.
- C. 3 True is wrong; True * 10 is the number 10, not True.
- D. True True is wrong; the arithmetic gives numbers.
Tip: True acts as 1 and False acts as 0 in arithmetic.
Question 2Easy
What is the output?
print(5 & 3, 5 | 3, 5 ^ 3)
- 0 7 6
- 1 7 6
- 15 8 6
- 1 7 2
Answer: B. 1 7 6
Explanation
101 & 011 = 001,
101 | 011 = 111,
101 ^ 011 = 110.
Step-by-step solution
- 5 is 101 and 3 is 011 in binary.
- 5 & 3 keeps the bits set in both, which is 001, or 1.
- 5 | 3 is 111, or 7, and 5 ^ 3 is 110, or 6. The output is 1 7 6, the correct answer.
Why the other options are wrong
- A. 0 7 6 is wrong; the bit that is set in both numbers gives 1 for AND.
- C. 15 8 6 is wrong; the AND is 1 and the OR is 7.
- D. 1 7 2 is wrong; 101 XOR 011 is 110, which is 6.
Tip: AND keeps bits set in both, OR keeps bits set in either, and XOR keeps bits set in exactly one.
Question 3Easy
Which operator in Python checks whether two names refer to the same object?
- id
- is
- ==
- in
Answer: B. is
Explanation
is compares object identity; == compares values and id() is a function, not an operator.
Step-by-step solution
- Is checks whether two names point to the same object, which is identity.
- == checks whether the values are equal, and id() is a function, not an operator.
- In checks membership in a collection, so the operator for identity is is, the correct answer.
Why the other options are wrong
- A. Id is a built-in function that returns an object's identity number, not an operator.
- C. == compares values, so two equal objects can still be different objects.
- D. In checks whether a value is inside a collection, not whether two names are the same object.
Tip: Use is only for None, True, False and identity checks. Use == for comparing values.
Question 4Easy
What is the output?
print(~5, ~-1)
- -6 0
- 4 0
- -6 -2
- -5 1
Answer: A. -6 0
Explanation
Bitwise NOT on integers is ~x = -x - 1, so ~5 = -6 and ~-1 = 0.
Step-by-step solution
- For integers, ~x is the same as -x - 1.
- ~5 = -5 - 1 = -6.
- ~-1 = 1 - 1 = 0. The output is -6 0, the correct answer.
Why the other options are wrong
- B. 4 0 is wrong; ~5 is -6, since ~x is -x - 1.
- C. -6 -2 is wrong; ~-1 is 0, not -2.
- D. -5 1 is wrong; ~5 is -6, not -5, and ~-1 is 0, not 1.
Tip: For integers, ~x equals -x - 1. Use this rule for any bitwise NOT.
Question 5Medium
What is the output?
x = 10
x //= 3
x **= 2
x %= 5
print(x)
- 1
- 4
- 0
- 9
Answer: B. 4
Explanation
10 // 3 = 3, then 3 ** 2 = 9, then 9 % 5 = 4.
Step-by-step solution
- X //= 3 gives 10 // 3 = 3.
- x = 2 gives 3 2 = 9.
- X %= 5 gives 9 % 5 = 4, so the output is 4, the correct answer.
Why the other options are wrong
- A. 1 is wrong; 9 % 5 is 4.
- C. 0 is wrong; 9 is not divisible by 5.
- D. 9 is wrong; the last step is % 5, which gives 4.
Tip: Compound assignments run one at a time, and each one uses the value left by the step before.
Question 6Medium
What is the output?
a = [1, 2]
b = [1, 2]
print(a == b, a is b, a is a[:])
- True False True
- True True False
- True False False
- False False False
Answer: C. True False False
Explanation
== compares contents; is compares identity, and both b and the slice a[:] are distinct list objects.
Step-by-step solution
- a == b is True, because the two lists hold the same values.
- A is b is False, because they are two separate list objects.
- a[:] makes a new copy, so a is a[:] is False. The output is True False False, the correct answer.
Why the other options are wrong
- A. True False True is wrong; a[:] is a new list, so it is not the same object as a.
- B. True True False is wrong; a and b are different objects.
- D. False False False is wrong; the two lists have the same values, so == is True.
Tip: == compares values, and is compares object identity. A slice like a[:] makes a new list.
Question 7Medium
What does 0.1 + 0.2 == 0.3 return in Python, and what is the usual fix?
- False; compare with math.isclose()
- TypeError; floats cannot be compared
- False; use int() on both sides
- True; floats are exact decimals
Answer: A. False; compare with math.isclose()
Explanation
Binary floating point cannot represent 0.1 exactly, so compare with a tolerance such as math.isclose().
Step-by-step solution
- Binary floating point cannot store 0.1 or 0.2 exactly, so their sum is slightly off.
- The sum is 0.30000000000000004, which is not equal to 0.3, so the result is False.
- The usual fix is to compare with a small tolerance, such as math.isclose(). So the answer is option A, the correct answer.
Why the other options are wrong
- B. No TypeError happens; floats compare normally.
- C. int() on both sides is not the fix; it would drop the decimal parts.
- D. Floats are not exact decimals, and 0.1 cannot be stored exactly.
Tip: For float comparisons, use math.isclose() or a small tolerance instead of ==.
Question 8Medium
What is the output?
print(not 1 == 2, not (1 and 0))
- False True
- True True
- True False
- False False
Answer: B. True True
Explanation
not has lower precedence than ==, so it is not (1 == 2) = True; (1 and 0) is 0 and not 0 is True.
Step-by-step solution
- Not has a lower priority than ==, so not 1 == 2 means not (1 == 2).
- 1 == 2 is False, so the first result is True.
- 1 and 0 gives 0, and not 0 is True. The output is True True, the correct answer.
Why the other options are wrong
- A. False True is wrong; 1 == 2 is False, so not gives True.
- C. True False is wrong; not 0 is True, not False.
- D. False False is wrong; both results are True.
Tip: Not has a lower priority than comparison operators, so not a == b checks the whole comparison.
Question 9Hard
What is the output?
print(int(True), float("inf") > 10**100, float("nan") == float("nan"))
- 1 True False
- 1 True True
- 1 False False
- True True True
Answer: A. 1 True False
Explanation
Infinity compares greater than any finite number, and NaN is never equal to anything, including itself.
Step-by-step solution
- int(True) is 1.
- Inf is bigger than any finite number, so float('inf') is greater than 10 100, so it is True.
- Nan is not equal to anything, even itself, so the last test is False. The output is 1 True False, the correct answer.
Why the other options are wrong
- B. 1 True True is wrong; nan is never equal to nan.
- C. 1 False False is wrong; inf is greater than 10 100.
- D. True True True is wrong; int(True) is 1, not True.
Tip: Use math.isnan() to check for NaN, because NaN is never equal to any value.
Question 10Hard
What is the output?
print(isinstance(True, int), True == 1, type(True) is int)
- False True False
- True True True
- True False False
- True True False
Answer: D. True True False
Explanation
bool is a subclass of int, so isinstance is True and True == 1, but the exact type of True is bool, not int.
Step-by-step solution
- Bool is a subclass of int, so isinstance(True, int) is True.
- True == 1 is True, because their values match.
- type(True) is bool, not int, so type(True) is int is False. The output is True True False, the correct answer.
Why the other options are wrong
- A. False True False is wrong; True is an instance of int.
- B. True True True is wrong; the exact type of True is bool, not int.
- C. True False False is wrong; True == 1 is True.
Tip: isinstance() also accepts subclasses, but type(x) is ... checks the exact type.
Practise all 31 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →