Python Quiz
Questions: 16 · 10 minutes
1. What is printed by this code?
class Counter:
def __init__(self, start=0):
self.value = start
def increment(self):
self.value += 1
a = Counter(2)
b = Counter()
a.increment()
print(a.value, b.value)
2 0
3 1
2 1
3 0
2. What is assigned to ordered? Python's sort is stable, so equal-length words retain their original relative order.
words = [""pear"", ""fig"", ""kiwi"", ""plum""]
ordered = sorted(words, key=len)
[""pear"", ""kiwi"", ""plum"", ""fig""]
[""fig"", ""kiwi"", ""pear"", ""plum""]
[""fig"", ""pear"", ""kiwi"", ""plum""]
[""plum"", ""kiwi"", ""pear"", ""fig""]
3. A program must generate one million squared numbers and process them one at a time without first storing every result. Which expression is most suitable?
A generator expression: (n * n for n in range(1_000_000))
A set comprehension: {n * n for n in range(1_000_000)}
A list comprehension: [n * n for n in range(1_000_000)]
An eagerly constructed tuple: tuple(n * n for n in range(1_000_000))
4. What does this expression print?
print(-7 // 3)
The floating-point quotient -2.3333333333333335
The floor-divided integer -3
The integer -2 after truncation toward zero
The positive integer 3
5. What is printed by this code?
def add_item(item, bucket=None):
if bucket is None:
bucket = []
bucket.append(item)
return bucket
print(add_item(""a""), add_item(""b""))
['a', 'b'] ['a', 'b']
['a'] ['b']
['b'] ['a']
['a'] ['a', 'b']
6. Which statement correctly describes the usual difference between Python's == and is operators?
#ИМЯ?
#ИМЯ?
#ИМЯ?
#ИМЯ?
7. What output is produced by this code?
def check():
print(""checked"")
return True
result = False and check()
print(result)
It prints `checked` and then `False`.
It prints only `False`.
It prints only `True`.
It prints `checked` and then `True`.
8. What is printed by this slice operation?
text = ""python""
print(text[1:5:2])
yto
yh
pto
yth
9. What value is assigned to pairs?
names = [""Ada"", ""Linus"", ""Guido""]
scores = [9, 8]
pairs = list(zip(names, scores))
[(""Ada"", 9), (""Linus"", 8)]
[(""Ada"", 9), (""Linus"", 8), (""Guido"", 9)]
[(""Ada"", ""Linus""), (9, 8)]
[(""Ada"", 9), (""Linus"", 8), (""Guido"", None)]
10. What is printed when this code runs?
try:
value = int(""3.5"")
except ValueError:
value = 0
print(value)
3
Nothing, because the unhandled error stops the program.
3.5
0
11. What value is assigned to result?
nums = [1, 2, 3, 4, 5]
result = [n * n for n in nums if n % 2 == 0]
[1, 9, 25]
[2, 4]
[4, 16]
[1, 4, 9, 16, 25]
12. What is a primary benefit of opening a file with a with statement, as in `with open(""notes.txt"") as file:`?
The file is closed automatically when the block ends, including when many exceptions occur.
The entire file is automatically converted into a list.
The file becomes writable even if it was opened in read mode.
The file contents remain permanently cached after the block ends.
13. What is printed by this code?
x = 10
def change():
x = 3
return x
change()
print(x)
An UnboundLocalError occurs.
3
None
10
14. You need to remove duplicates from values while preserving the order of each value's first appearance. Which expression does that?
values = [""b"", ""a"", ""b"", ""c"", ""a""]
list(set(values))
sorted(set(values))
list(dict.fromkeys(sorted(values)))
list(dict.fromkeys(values))
15. What is printed by this code?
items = [1, 2]
alias = items
alias.append(3)
print(items)
A list containing only 3: [3]
The original two-item list: [1, 2]
The updated list: [1, 2, 3]
An error occurs because alias is not a list.
16. What is printed after this loop counts the letters?
counts = {}
for ch in ""aba"":
counts[ch] = counts.get(ch, 0) + 1
print(counts[""a""], counts[""b""])
1 1
2 2
2 1
3 1