Sample questions
Question 1Easy
What is the output?
print(list(filter(None, [0, 1, "", "a", None, [], [0]])))
- [1, 'a']
- [1, 'a', [], [0]]
- [0, 1, '', 'a', None, [], [0]]
- [1, 'a', [0]]
Answer: D. [1, 'a', [0]]
Explanation
filter(None, ...) keeps only truthy items; 0, "", None and [] are falsy, while [0] is a non-empty list and so truthy.
Step-by-step solution
- filter(None, ...) keeps only truthy items.
- 0, "", None and [] are falsy and are removed.
- 1, "a" and [0] are truthy: [0] is a non-empty list.
- The output is [1, 'a', [0]], which is option D.
Why the other options are wrong
- A. The list [0] is non-empty, so it is truthy and is kept.
- B. The empty list [] is falsy, so it is removed.
- C. filter(None, ...) removes falsy values; it does not keep them all.
Tip: Falsy values are 0, "", None, [] and other empty containers; a non-empty list is truthy.
Question 2Easy
What is the output?
print(round(3.14159, 2), int(7.9), int("-12"))
- 3.14 8 -12
- 3.15 7 -12
- 3.14 7 12
- 3.14 7 -12
Answer: D. 3.14 7 -12
Explanation
round(x, 2) keeps two decimals, int() truncates towards zero (7.9 becomes 7) and parses signed strings.
Step-by-step solution
- round(3.14159, 2) rounds to two decimals, giving 3.14.
- int(7.9) truncates toward zero, giving 7.
- int("-12") reads the signed string, giving -12.
- The output is 3.14 7 -12, which is option D.
Why the other options are wrong
- A. int(7.9) is 7, not 8, because int() truncates.
- B. round(3.14159, 2) is 3.14, not 3.15, and int(7.9) is 7 with no rounding up.
- C. int("-12") keeps the minus sign, so it is -12, not 12.
Tip: int() cuts off the decimal part toward zero; round() rounds to the nearest value.
Question 3Easy
What is the output?
print(divmod(17, 5), pow(2, 10, 1000), abs(-7.5))
- (2, 3) 24 7.5
- (3, 2) 24 7.5
- (3.4, 2) 24 7
- (3, 2) 1024 7.5
Answer: B. (3, 2) 24 7.5
Explanation
divmod returns (quotient, remainder) = (3, 2); three-argument pow computes 2**10 mod 1000 = 1024 mod 1000 = 24.
Step-by-step solution
- divmod(17, 5) returns (quotient, remainder) = (3, 2) since 17 = 3 × 5 + 2.
- pow(2, 10, 1000) computes 2^10 = 1024, then takes the remainder mod 1000, giving 24.
- abs(-7.5) is 7.5.
- The printed output is (3, 2) 24 7.5, which is option B.
Why the other options are wrong
- A. Divmod gives (2, 3) here; the quotient comes first and it is 3.
- C. Divmod returns integers, not 3.4, and abs(-7.5) is 7.5, not 7.
- D. Three-argument pow returns 1024 mod 1000 = 24, not 1024.
Tip: divmod(a, b) gives (a // b, a % b); pow(x, y, m) gives xy % m without building the big number.
Question 4Easy
What is the output?
nums = [4, 8, 15]
print(sum(nums), sum(nums, 10), len(nums))
- 37 27 3
- 27 27 3
- 27 37 3
- 27 10 3
Answer: C. 27 37 3
Explanation
sum adds the items (27); the optional second argument is the starting value, giving 10 + 27 = 37.
Step-by-step solution
- sum(nums) adds 4 + 8 + 15 = 27.
- sum(nums, 10) starts from 10, so it gives 10 + 27 = 37.
- len(nums) is 3.
- The output is 27 37 3, which is option C.
Why the other options are wrong
- A. The second sum starts at 10, giving 37, not the first total.
- B. The second value starts at 10, so it is 37, not 27.
- D. The sum with start 10 is 37, not 10.
Tip: The optional second argument to sum is the starting value, not an extra item.
Question 5Medium
What is the output?
import operator
pairs = [("b", 2), ("a", 2), ("c", 1)]
print(sorted(pairs, key=operator.itemgetter(1)))
- [('c', 1), ('b', 2), ('a', 2)]
- [('c', 1), ('a', 2), ('b', 2)]
- [('b', 2), ('a', 2), ('c', 1)]
- [('a', 2), ('b', 2), ('c', 1)]
Answer: A. [('c', 1), ('b', 2), ('a', 2)]
Explanation
Sorting by the second element puts c first; sorted is stable, so b and a (both 2) keep their original relative order.
Step-by-step solution
- itemgetter(1) sorts by the second element of each pair: 2, 2 and 1.
- The smallest key is 1, so ('c', 1) comes first.
- Sorted is stable, so ('b', 2) stays before ('a', 2), which was their original order.
- The output is [('c', 1), ('b', 2), ('a', 2)], which is option A.
Why the other options are wrong
- B. Stable sorting keeps b before a, since both have the key 2 and b was first.
- C. The list is sorted by the number, so ('c', 1) must come first.
- D. The sort key is the number, not the letter, so the order is by the second element.
Tip: Sorted is stable: items with equal keys keep their original order.
Question 6Medium
What is the output?
import itertools
print(list(itertools.combinations("ABC", 2)), len(list(itertools.permutations("ABC", 2))))
- [('A', 'B'), ('A', 'C'), ('B', 'C')] 9
- [('A', 'B'), ('A', 'C'), ('B', 'C')] 6
- [('A', 'B'), ('A', 'C'), ('B', 'C')] 3
- [('A', 'B'), ('B', 'A'), ('A', 'C')] 6
Answer: B. [('A', 'B'), ('A', 'C'), ('B', 'C')] 6
Explanation
combinations ignore order (3C2 = 3 pairs); permutations count order (3P2 = 6).
Step-by-step solution
- combinations of ABC taken 2: AB, AC, BC.
- permutations count order: 3 x 2 = 6.
- Output lists the 3 pairs and 6.
Why the other options are wrong
- A. Permutations of 3 taken 2 are 6, not 9.
- C. Permutations count ordering so there are 6.
- D. Combinations never include both AB and BA.
Tip: nCr vs nPr.
Question 7Medium
What is the output?
from collections import Counter
a = Counter("aab")
b = Counter("abc")
print(sorted((a + b).items()), sorted((a - b).items()))
- [('a', 3), ('b', 2), ('c', 1)] [('a', 1), ('b', 0)]
- [('a', 3), ('b', 2), ('c', 1)] [('a', 1), ('c', -1)]
- [('a', 2), ('b', 1), ('c', 1)] [('a', 1)]
- [('a', 3), ('b', 2), ('c', 1)] [('a', 1)]
Answer: D. [('a', 3), ('b', 2), ('c', 1)] [('a', 1)]
Explanation
Counter addition sums the counts. Subtraction subtracts and drops counts that are zero or negative, leaving only a: 1.
Step-by-step solution
- Counter("aab") is a:2, b:1; Counter("abc") is a:1, b:1, c:1.
- Addition gives a:3, b:2, c:1.
- Subtraction gives a:1; b becomes 0 and c becomes -1, and both are dropped.
- The output is [('a', 3), ('b', 2), ('c', 1)] [('a', 1)], which is option D.
Why the other options are wrong
- A. Counter subtraction drops zero and negative counts, so b:0 is not kept.
- B. C:-1 is dropped by Counter subtraction, so it is not shown.
- C. The addition counts are 3 for a and 2 for b, not 2 and 1.
Tip: Counter subtraction keeps only positive results; use + and - to combine counts.
Question 8Medium
Which built-in returns both the index and the value while looping over a list?
range(len())zip()items()enumerate()
Answer: D. enumerate()
Explanation
enumerate(iterable) yields (index, value) pairs; lists have no items() method, and range(len()) gives only indices.
Step-by-step solution
- enumerate(iterable) gives pairs of (index, value) while looping.
- Lists have no items() method, and range(len()) gives only indices.
- So the correct answer is option D, enumerate().
Why the other options are wrong
- A. range(len(...)) gives only indices, so the value must be looked up separately.
- B. Zip pairs two iterables, not an index with a value.
- C. items() is a dict method and lists do not have it.
Tip: Use for i, x in enumerate(items) to get both the index and the value.
Question 9Hard
What is the output?
from itertools import groupby
data = "aabbbacc"
print([(k, len(list(g))) for k, g in groupby(data)])
- [('a', 2), ('b', 3), ('c', 2)]
- [('a', 3), ('b', 3), ('c', 2)]
- [('a', 2), ('b', 3), ('a', 1), ('c', 2)]
- [('a', 3), ('c', 2), ('b', 3)]
Answer: C. [('a', 2), ('b', 3), ('a', 1), ('c', 2)]
Explanation
groupby groups only consecutive equal elements, so the single a after the bs forms a new group; sort first if you want one group per key.
Step-by-step solution
- Groupby makes a new group whenever the value changes, and only consecutive equal items join a group.
- Aa gives a group of 2, bbb a group of 3, then the single a gives a new group of 1.
- Cc gives a group of 2.
- The output is [('a', 2), ('b', 3), ('a', 1), ('c', 2)].
Why the other options are wrong
- A. The second a is a separate group, so the output has four pairs, not three.
- B. Groupby does not merge the a values that are not next to each other.
- D. The groups come out in the order they appear, so c is not before the b group.
Tip: Groupby only groups neighbouring equal items; sort first to group all equal keys together.
Question 10Hard
What is the output?
def describe(v):
match v:
case 0:
return "zero"
case int(n) if n < 0:
return "negative"
case [x, y]:
return f"pair {x}+{y}"
case {"id": i}:
return f"id {i}"
case _:
return "other"
print(describe(-3), describe((1, 2)), describe({"id": 7, "x": 0}), describe("ab"))
- negative pair 1+2 id 7 other
- negative other id 7 other
- negative pair 1+2 other other
- negative pair 1+2 id 7 pair a+b
Answer: A. negative pair 1+2 id 7 other
Explanation
A sequence pattern matches tuples as well as lists but never str; a mapping pattern matches dicts that contain the given keys, extra keys allowed.
Step-by-step solution
- describe(-3): it is not 0, but it is an int with n < 0, so the second case returns negative.
- describe((1, 2)): a tuple matches the sequence pattern [x, y] with x=1 and y=2, so it returns pair 1+2.
- describe({"id": 7, "x": 0}): a dict is not a sequence, but it has the key id, so it returns id 7.
- describe("ab"): a str is never matched by a sequence pattern, so it falls to case _ and returns other.
- The printed output is negative pair 1+2 id 7 other, which is option A.
Why the other options are wrong
- B. A tuple (1, 2) matches the [x, y] pattern, so it prints pair 1+2, not other.
- C. The dict {"id": 7, "x": 0} matches the mapping pattern, so it prints id 7, not other.
- D. A string is not matched by a sequence pattern, so "ab" prints other, not pair a+b.
Tip: In match, a sequence pattern matches lists and tuples but never str; a mapping pattern matches a dict that has the given keys.
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 →