Unit 3: String, Lists, Tuples and Dictionaries - Practice Quiz
1 Why is a Python string called a compound data type?
2
What is the value of len("Python")?
3
Which statement visits every character in the string word?
for ch in word:
while word == ch:
if ch in word:
for word in range(ch):
4
What does "Programming"[0:3] return?
"Programming", because slicing always returns the complete original string
"rog"
"Prog"
"Pro"
5
What is the result of "cat" == "cat" in Python?
"cat"
True
None
False
6
What does "banana".find("na") return?
1
"na" occurs
2
3
7 Which initial value should usually be assigned to a variable used to count matching characters?
-1
None
0
1
8
Which expression creates a Python list containing the values 1, 2, and 3?
(1, 2, 3)
[1, 2, 3]
<1, 2, 3>
{1, 2, 3}
9
What is the value of len([10, 20, 30, 40])?
40
3
100, because len() adds all numeric elements in the list
4
10
What is the result of 3 in [1, 2, 3, 4]?
False
None
3
True
11
What does [1, 2] + [3, 4] produce?
[[1, 2], [3, 4]]
+ operator can only combine numeric variables
[4, 6]
[1, 2, 3, 4]
12
What does [10, 20, 30, 40][1:3] return?
[10, 20, 30]
[20, 30]
[10, 20]
[20, 30, 40]
13
Which statement deletes the element at index 1 from a list named items?
del items, which removes only the element stored at index 1
del items[1]
delete items[1]
items.delete(1)
14
Given colors = ["red", "green", "blue"], which expression accesses "green"?
colors[3]
colors[2]
colors[1]
colors[0]
15
What is printed by for value in [2, 4, 6]: print(value)?
0, 1, and 2
6
2, 4, and 6
16 When a list is passed to a function and the function changes one of its elements, what can happen to the original list?
17
Given matrix = [[1, 2], [3, 4]], what is the value of matrix[1][0]?
2
3
1
4
18 Which statement about tuples is correct?
19
After x, y = (5, 8), what values do x and y contain?
x = 5, y = 8
x = 5, y = (5, 8)
x = 8, y = 5
x = (5, 8), y = 0
20
Given student = {"name": "Asha", "age": 18}, which expression returns "Asha"?
student.values("name"), because values() accepts a key and returns its value
student["name"]
student[0]
student["Asha"]
21
What is printed by the following code?
word = "Python"
result = word[1] + word[-2]
print(result)
Pt
ot
yh
yo
22
What value is assigned to result?
text = "data science"
result = len(text[2:9])
8
9
7
6
23
What is printed by the following code?
text = "planet"
result = ""
for i in range(0, len(text), 2):
result += text[i]
print(result)
pnt
plt
lne
pae
24
What is the value of result after this statement?
text = "abcdefgh"
result = text[-2:1:-2]
gdb
hfdb
gec
geca
25
Which expression evaluates to True in Python?
"apple" > "Banana"
"20" > "3"
"cat" < "catalog"
"Zoo" > "apple"
26
What is printed by the following code?
text = "abracadabra"
print(text.find("abra", 1))
-1
7
0
1
27
What is printed by this code?
text = "banana"
count = 0
for i in range(len(text) - 1):
if text[i:i + 2] == "an":
count += 1
print(count)
4
3
1
2
28
What does the following expression evaluate to?
values = [[1, 2], [3, 4], 5]
result = 3 in values
TypeError
3
True
False
29
What is the value of result?
a = [1, 2]
b = [3]
result = a * 2 + b
[2, 4, 3]
[1, 2, 1, 2, 3]
[1, 2, 3, 3]
[1, 2, 2, 3]
30
What is printed by the following code?
nums = [0, 1, 2, 3, 4, 5]
nums[1:5:2] = [9, 8]
print(nums)
[9, 1, 8, 3, 4, 5]
[0, 9, 2, 8, 4, 5]
[0, 9, 2, 3, 8, 5]
[0, 9, 8, 3, 4, 5]
31
What is printed by this code?
items = ['a', 'b', 'c', 'd', 'e']
del items[1:4:2]
print(items)
['a', 'd', 'e']
['a', 'c', 'e']
['a', 'b', 'd', 'e']
['b', 'd']
32
What is printed by the following code?
nums = [1, 2, 3]
for value in nums:
value *= 2
print(nums)
[2, 4, 6]
[1, 2, 3]
[1, 4, 9]
[2, 2, 3]
33
What is printed by this program?
def update(values):
values.append(4)
values = [9, 9]
nums = [1, 2, 3]
update(nums)
print(nums)
[1, 2, 3, 4]
[9, 9]
[9, 9, 4]
[1, 2, 3]
34
What is printed by the following code?
matrix = [[1, 2], [3, 4], [5, 6]]
total = 0
for row in matrix:
total += row[1]
print(total)
9
15
12
10
35
What is printed by the following code?
data = ([1, 2], 3)
data[0].append(4)
print(data)
([1, 2, 4], 3)
([1, 2], 3, 4)
([1, 2], 4)
TypeError
36
What is printed by this code?
x, y, z = 2, 4, 6
x, y, z = z, x + y, y - x
print(x, y, z)
6 10 -4
6 8 2
2 6 6
6 6 2
37
What is printed by the following program?
def bounds(values):
return min(values), max(values)
low, high = bounds([8, 3, 11, 5])
print(high - low)
14
6
8
11
38
What is printed by this code?
counts = {'a': 2, 'b': 1}
for ch in "abac":
counts[ch] = counts.get(ch, 0) + 1
print(counts['a'], counts['c'])
4 0
4 1
2 1
3 1
39
A sparse matrix stores only nonzero entries in a dictionary using (row, column) keys. What is printed?
matrix = {(0, 2): 5, (2, 1): 7}
total = 0
for col in range(3):
total += matrix.get((2, col), 0)
print(total)
12
7
0
5
40
What is printed by the following code?
original = [[1], [2]]
copied = original[:]
copied[0].append(3)
copied.append([4])
print(original)
[[1, 3], [2], [4]]
[[1], [2]]
[[1], [2], [4]]
[[1, 3], [2]]
41
What is printed by the following code?
s = 'programming'
result = s[1] + s[-2] + s[len(s) // 2]
print(result)
rgn
rnm followed by a newline character
rnm
pim
42
What value does out contain after this traversal?
s = 'abcdef'
out = []
for i, ch in enumerate(s):
if i % 2 == 0:
out.append(s[-i - 1])
['f', 'c', 'a']
['f', 'd', 'b']
['e', 'c', 'a']
['a', 'c', 'e']
43
What is printed by the following code?
s = '0123456789'
a = s[-2:1:-3]
b = s[1:-1][::-3]
print(a + ':' + b)
852:741
258:258
963:852
852:852
44
Given Python's lexicographic string comparison, what is the value of the following expression?
words = ['9', '10', '2']
(min(words), max(words), '10' < '2')
('10', '9', True)
('10', '9', False)
('2', '10', False)
('9', '2', True)
45
What tuple is produced by this code?
s = 'ababaabababa'
a = s.find('ababa', 1)
b = s.rfind('ababa', 0, 10)
print((a, b))
(5, 7)
(-1, 5)
(5, 5)
(7, 5)
46
What does count('aaaaa', 'aa') return?
def count(s, sub):
total = 0
start = 0
while True:
position = s.find(sub, start)
if position == -1:
return total
total += 1
start = position + 1
5
2
4
3
47
What is the value of the final expression?
a = [1, [2, 3], []]
a.append(a[1])
a[1].append(4)
(len(a), len(a[-1]), a[-1][-1])
(4, 2, 4)
(3, 3, 4)
(4, 2, 3)
(4, 3, 4)
48
What is the value of the final expression?
items = [[1, 2], [3, [4]], '12']
(
2 in items,
[1, 2] in items,
4 in items[1],
'1' in items[2]
)
(False, True, False, True)
(True, True, True, False)
(True, False, False, True)
(False, True, True, True)
49
What is printed by this code?
a = [1, 2]
b = a
c = a + [3]
a += [4]
print((b, c, a is b))
([1, 2, 4], [1, 2, 3], True)
([1, 2, 4], [1, 2, 3, 4], True)
([1, 2], [1, 2, 3], True)
([1, 2], [1, 2, 3], False)
50
What is the final value of a?
a = list(range(8))
a[1:7:2] = [9, 8, 7]
a[2:6] = [5]
[0, 9, 5, 6, 7]
[0, 9, 5, 7, 6, 7]
[0, 5, 4, 7, 6, 7]
[0, 9, 5, 4, 7]
51
What is printed by the following code?
a = [0, 1, 2, 3, 4, 5]
del a[1::2]
x = a.pop(-2)
del a[:1]
print((x, a))
(2, [4])
(4, [2])
(3, [4, 5])
(2, [2, 4])
52
What value is assigned to result?
matrix = [[i + j for j in range(3)] for i in range(3)]
result = matrix[-1][-matrix[0][1]]
3
4
2
1
53
What is the value of a after the loop completes?
a = [2, 4, 6, 8]
for value in a:
if value % 2 == 0:
a.remove(value)
[4, 6]
[2, 6]
[]
[4, 8]
54
What is printed by this code?
def f(x, y=[]):
y.append(x.pop())
x = x + [0]
return x, y
a = [1, 2]
r1 = f(a)
r2 = f(a)
print((a, r1, r2))
([1, 2], ([1, 0], [2]), ([0], [1]))
([], ([1, 0], [2, 1]), ([0], [2, 1]))
([], ([1, 0], [2]), ([0], [1]))
([], ([1, 0], [2]), ([0], [2, 1]))
55
What is printed by the following code?
grid = [[0] * 3] * 2
grid[0][1] = 7
grid[1] = grid[1][:]
grid[1][2] = 9
print((grid, grid[0] is grid[1]))
([[0, 7, 0], [0, 7, 9]], False)
([[0, 7, 9], [0, 7, 9]], True)
([[0, 7, 0], [0, 0, 9]], False)
([[0, 7, 9], [0, 7, 9]], False)
56
What is printed after the exception is handled?
t = ([1, 2], 'x')
try:
t[0] += [3]
except TypeError:
pass
print(t)
([1, 2, 3], 'x')
([1, 2], 'x')
([3], 'x')
57
What is printed by this code?
a = [0, 1, 2]
i = 0
i, a[i] = 1, 9
print((i, a))
(1, [0, 9, 2])
(1, [9, 1, 2])
(1, [0, 1, 9])
(0, [0, 9, 2])
58
What is printed by the following code?
def stats(values):
return min(values), max(values), sum(values)
low, *middle, high = stats([3, 1, 4, 2])
middle.append(high - low)
print((low, middle, high))
(1, [10, 3], 4)
(1, [4, 9], 10)
(1, [4, 3], 10)
(1, [4, 10], 3)
59
What is printed in Python 3.7 or later, where dictionaries preserve insertion order?
d = {'a': 1, 'b': 2}
keys = d.keys()
x = d.setdefault('a', 9)
y = d.setdefault('c', 3)
d.update({'b': 5, 'd': 4})
p = d.pop('b')
print((list(keys), x, y, p))
(['a', 'b'], 9, 3, 2)
(['a', 'b', 'c'], 1, 3, 2)
(['a', 'c', 'd'], 1, 3, 5)
(['a', 'c', 'd'], 9, 3, 5)
60
A sparse matrix stores only nonzero entries in a dictionary. What is printed by this code?
matrix = {(0, 2): 4, (2, 1): -3}
alias = matrix
clone = matrix.copy()
alias[(1, 1)] = clone.get((1, 1), 0) + 5
del matrix[(0, 2)]
print((
clone.get((0, 2), 0),
alias.get((0, 2), 0),
matrix.get((1, 1), 0),
clone.get((1, 1), 0)
))
(4, 0, 5, 0)
(4, 4, 5, 0)
(0, 0, 5, 5)
(4, 0, 0, 5)
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →