Unit 2: Python data structures - Practice Quiz
1 Which Python data structure represents a sequence of characters?
2
What is the result of "Python"[0]?
"P"
"Python"
"n"
"y"
3 Which string method converts all letters to uppercase?
upper()
title()
lower()
capitalize()
4 Which statement about Python strings is correct?
5 Which expression creates a Python list?
[1, 2, 3]
{"a": 1}
{1, 2, 3}
(1, 2, 3)
6 Which method adds one element to the end of a list?
append()
remove()
sort()
Adding an element requires creating a completely new list with every original value
7
What is the result of len([10, 20, 30])?
3
60
30
2
8 Which statement about Python lists is correct?
9 What is a main characteristic of a Python set?
10 Which expression creates a non-empty set?
{1, 2, 3}
{"a": 1}
(1, 2, 3)
[1, 2, 3]
11 Which method adds an element to a set?
insert()
add()
extend()
append()
12
What is the result of len({1, 1, 2, 3})?
4
1
2
3
13 Which expression creates a tuple containing three values?
{"values": [1, 2, 3]}
[1, 2, 3]
{1, 2, 3}
(1, 2, 3)
14 Which statement about tuples is correct?
15
Which expression creates a tuple containing only the value 5?
(5,)
(5)
[5]
{5}
16
What is the result of (10, 20, 30)[1]?
30
10
20
IndexError
17 What does a Python dictionary store?
18 Which expression creates a dictionary?
["name", "Ana"]
{"name", "Ana"}
("name", "Ana")
{"name": "Ana"}
19
Given student = {"name": "Sam", "age": 18}, which expression returns "Sam"?
student.name()
student[0]
student["name"]
student["Sam"]
20 Which dictionary method returns a view of all keys?
items()
keys()
The method that returns every key and value as a newly sorted list
values()
21
What is the value of result after this code?
text = 'DataStructures'
result = text[1:10:2]
'aStut'
'aatut'
'aatuc'
'atrcu'
22
What does the following expression return?
'banana'.replace('a', 'o', 2)
'banana'
'bonona'
'bonano'
'bonono'
23
What is printed by the following code?
text = ' py thon '
print(text.strip().replace(' ', '_'))
py_thon
python
py__thon
__py_thon__
24
Given code = 'AB12-CD34', which expression returns '1234'?
code[2:4] + code[7:9]
code[2:4] + code[6:8]
code[2:5] + code[6:8]
code[1:3] + code[6:8]
25
What is printed by the following code?
a = [[1, 2], [3, 4]]
b = a.copy()
b[0].append(5)
b.append([6])
print(a)
[[1, 2], [3, 4]]
[[1, 2], [3, 4], [6]]
[[1, 2, 5], [3, 4]]
[[1, 2, 5], [3, 4], [6]]
26
What is the value of result after this code?
result = [x * 2 for x in range(6) if x % 2 != 0]
[2, 4, 6]
[2, 6, 10]
[0, 4, 8]
[1, 3, 5]
27
What is the final value of nums?
nums = [0, 1, 2, 3, 4]
nums[1:4] = [8, 9]
[0, 8, 9, 3, 4]
[8, 9, 3, 4]
[0, 1, 8, 9, 4]
[0, 8, 9, 4]
28
What does the following expression return?
sorted(['pear', 'fig', 'apple', 'kiwi'], key=lambda x: (len(x), x))
['fig', 'apple', 'kiwi', 'pear']
['apple', 'kiwi', 'pear', 'fig']
['fig', 'kiwi', 'pear', 'apple']
['fig', 'pear', 'kiwi', 'apple']
29
What is the value of (A | B) - (A & B) when A = {1, 2, 3, 4} and B = {3, 4, 5}?
{1, 2, 5}
{1, 2}
{3, 4}
{1, 2, 3, 4, 5}
30
What is the value of the following set comprehension?
{x % 3 for x in range(7)}
{0, 1, 2}
{0, 3, 6}
{0, 1, 2, 3}
{1, 2, 3}
31
What is printed by the following code?
s = {1, 2}
s.discard(3)
s.remove(2)
print(s)
{1}
{2}
set()
{1, 3}
32
Given A = {1, 2} and B = {1, 2, 3}, which pair of expressions both evaluates to True?
A == B and A <= B
A <= B and A < B
A < B and B < A
A >= B and A > B
33
What are the values of a, b, and rest after this assignment?
a, b, *rest = (1, 2, 3, 4)
1, 2, (3, 4)
1, [2, 3], 4
1, 2, [3, 4]
[1, 2], 3, 4
34
What is printed by the following code?
t = ([1, 2], 'x')
t[0].append(3)
print(t)
([1, 2, 3], 'x')
([1, 2], 'x', 3)
([1, 2], 'x')
([3, 1, 2], 'x')
35
What does the following expression return?
sorted([(2, 'b'), (1, 'c'), (1, 'a')])
[(1, 'a'), (2, 'b'), (1, 'c')]
[(2, 'b'), (1, 'a'), (1, 'c')]
[(1, 'c'), (1, 'a'), (2, 'b')]
[(1, 'a'), (1, 'c'), (2, 'b')]
36
What is printed by the following code?
t = (1, 2)
u = t
t += (3,)
print(t, u)
(1, 2, 3) (1, 2)
(1, 2) (1, 2)
(1, 2) (1, 2, 3)
(1, 2, 3) (1, 2, 3)
37
What is the value of d after this dictionary comprehension?
d = {x % 2: x for x in range(5)}
{0: 0, 1: 1}
{0: 4, 1: 1}
{0: 4, 1: 3}
{0: 2, 1: 3}
38
What is the final value of d?
d = {'a': [1]}
d.setdefault('a', []).append(2)
d.setdefault('b', []).append(3)
{'a': [2], 'b': [3]}
{'a': [1, 2], 'b': [3]}
{'a': [1], 'b': [3]}
{'a': [1, 2, 3], 'b': []}
39
What is the value of merged after this code?
d1 = {'a': 1, 'b': 2}
d2 = {'b': 5, 'c': 6}
merged = d1 | d2
{'a': 1, 'b': 7, 'c': 6}
{'a': 1, 'b': 5, 'c': 6}
{'a': 1, 'b': 2, 'c': 6}
{'b': 5, 'c': 6}
40
What is printed by the following code?
original = {'x': [1]}
clone = original.copy()
clone['x'].append(2)
print(original, clone)
{'x': [1]} {'x': [2]}
{'x': [1]} {'x': [1, 2]}
{'x': [1, 2]} {'x': [1]}
{'x': [1, 2]} {'x': [1, 2]}
41
What is the value of (s[8:1:-3], s[-2::-3], s[8:1:-3] == s[-2::-3]) when s = "0123456789"?
"852", "8520", False)
"852", "258", False)
"825", "852", False)
"852", "852", True)
42
What does "mississippi".replace("iss", "X", 1).partition("s") return?
"mX", "s", "issippi")
"mi", "s", "sissippi")
"mXi", "ss", "ippi")
"mXi", "s", "sippi")
43
What string is produced by "{0[1]:0{1}d}".format([3, 7], 4)?
" 7"
"7000"
"0004"
"0007"
44
Given table = str.maketrans({"a": "xy", "b": None}), what is "abca".translate(table)?
"xynonecxy"
"xybcxy"
"xycxy"
"acaxy"
45
What is a after executing a = [[0] * 2] * 3; a[0][1] = 7; a[1] = a[1] + [8]?
[[0, 7], [0, 7, 8], [0, 0]]
[[0, 7], [0, 7, 8], [0, 7, 8]]
[[0, 7, 8], [0, 7], [0, 7]]
[[0, 7], [0, 7, 8], [0, 7]]
46
What is a after a = [0, 1, 2, 3, 4, 5]; a[1:5:2] = [9, 8]; a[2:4] = [7]?
[0, 9, 7, 8, 4, 5]
[0, 9, 7, 3, 4, 5]
[0, 7, 8, 4, 5]
[0, 9, 7, 4, 5]
47
What is the final value of a after a = [1, 2, 3, 4, 5, 6] followed by for x in a: a.remove(x) if x % 2 else None?
[2, 3, 4, 6]
[2, 4, 6]
[2, 4, 5, 6]
[1, 3, 5]
48
Given rows = [("a", 2), ("b", 1), ("c", 2), ("d", 1)], what is rows after rows.sort(key=lambda x: x[1]); rows.sort(key=lambda x: x[0] in {"b", "c"})?
[("d", 1), ("a", 2), ("c", 2), ("b", 1)]
[("b", 1), ("c", 2), ("d", 1), ("a", 2)]
[("a", 2), ("d", 1), ("b", 1), ("c", 2)]
[("d", 1), ("a", 2), ("b", 1), ("c", 2)]
49
For A = {1, 2, 3}, B = {2, 3, 4}, C = {3, 4, 5}, and D = {4}, what is A | B & C - D?
{1, 2, 3, 4}
{1, 2, 3}
{3}
{1, 2, 3, 5}
50
What is the value of (len(s), s == {0, 1, 2}) after s = {True, 1, 1.0, False, 0, 0.0, 2}?
(3, True)
(7, False)
(3, False)
(4, True)
51
For A = {1, 2}, B = {1, 2}, and C = frozenset({1, 2}), what is (A <= B, A < B, C == A)?
(True, True, False)
(True, False, False)
(False, False, True)
(True, False, True)
52
What does {1, 2} in {frozenset({1, 2}), frozenset()} evaluate to in Python?
NotImplemented
True
False
TypeError
53
What are u and t after t = ([1],); u = t; t[0].append(2); t += ([3],)?
u == ([1],) and t == ([1, 2], [3])
u == ([1, 2], [3]) and t == ([1, 2], [3])
u == ([1, 2],) and t == ([1, 2], [3])
u == ([1], [3]) and t == ([1, 2], [3])
54
After executing t = ([1, 2], [3]); try: t[0] += [4]\nexcept TypeError: pass, what is t?
([1, 2, 4], [3, 4])
([1, 2], [3])
([1, 2], [3, 4])
([1, 2, 4], [3])
55
What is (a, b, c, d) after a, *b, (c, *d) = (1, 2, 3, (4, 5, 6))?
(1, [2], 3, [4, 5, 6])
(1, [2, 3], 4, (5, 6))
(1, [2, 3], 4, [5, 6])
(1, (2, 3), 4, [5, 6])
56
Which tuple can be used as a dictionary key without raising TypeError?
(1, [2, 3], frozenset({4}))
(1, (2, [3]), frozenset({4}))
(1, (2, 3), frozenset({4}))
(1, (2, 3), {4})
57
What is (len(d), list(d.keys()), d[True]) after d = {1: "int", True: "bool", 1.0: "float"}?
(2, [1, True], "bool")
(1, [1], "float")
(1, [1.0], "float")
(3, [1, True, 1.0], "bool")
58
What is d after d = dict.fromkeys(["a", "b"], []); d["a"].append(1); d["b"] = d["b"] + [2]?
{"a": [1], "b": [1, 2]}
{"a": [1, 2], "b": [1, 2]}
{"a": [], "b": [1, 2]}
{"a": [1], "b": [2]}
59
Given calls = [], def make(): calls.append("x"); return 99, and d = {"k": 1}, what are (v, calls, d) after v = d.setdefault("k", make())?
(1, [], {"k": 1})
(99, ["x"], {"k": 99})
(99, [], {"k": 1})
(1, ["x"], {"k": 1})
60
What is list(d.items()) after d1 = {"a": 1, "b": 2}; d2 = {"b": 9, "c": 3, "a": 7}; d = d1 | d2?
[("b", 9), ("c", 3), ("a", 7)]
[("a", 1), ("b", 2), ("c", 3)]
[("c", 3), ("a", 7), ("b", 9)]
[("a", 7), ("b", 9), ("c", 3)]
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 →