Unit 2: Python data structures - Practice Quiz

ECAP776 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 Which Python data structure represents a sequence of characters?

Strings Easy
A. Dictionary
B. String
C. List
D. Set

2 What is the result of "Python"[0]?

Strings Easy
A. "P"
B. "Python"
C. "n"
D. "y"

3 Which string method converts all letters to uppercase?

Strings Easy
A. upper()
B. title()
C. lower()
D. capitalize()

4 Which statement about Python strings is correct?

Strings Easy
A. Strings always contain one character
B. Strings store only numbers
C. Strings are immutable
D. Strings can be modified directly by assigning a new character to any index

5 Which expression creates a Python list?

Lists Easy
A. [1, 2, 3]
B. {"a": 1}
C. {1, 2, 3}
D. (1, 2, 3)

6 Which method adds one element to the end of a list?

Lists Easy
A. append()
B. remove()
C. sort()
D. Adding an element requires creating a completely new list with every original value

7 What is the result of len([10, 20, 30])?

Lists Easy
A. 3
B. 60
C. 30
D. 2

8 Which statement about Python lists is correct?

Lists Easy
A. Lists have no order
B. Lists reject duplicates
C. Lists are mutable
D. Lists use key-value pairs

9 What is a main characteristic of a Python set?

Sets Easy
A. It maps every value to a separate key using insertion order
B. It stores indexed characters
C. It preserves duplicate elements
D. It stores unique elements

10 Which expression creates a non-empty set?

Sets Easy
A. {1, 2, 3}
B. {"a": 1}
C. (1, 2, 3)
D. [1, 2, 3]

11 Which method adds an element to a set?

Sets Easy
A. insert()
B. add()
C. extend()
D. append()

12 What is the result of len({1, 1, 2, 3})?

Sets Easy
A. 4
B. 1
C. 2
D. 3

13 Which expression creates a tuple containing three values?

Tuples Easy
A. {"values": [1, 2, 3]}
B. [1, 2, 3]
C. {1, 2, 3}
D. (1, 2, 3)

14 Which statement about tuples is correct?

Tuples Easy
A. Tuples are immutable
B. Tuples are always empty
C. Tuples allow their elements to be reassigned directly after they are created
D. Tuples reject duplicate values

15 Which expression creates a tuple containing only the value 5?

Tuples Easy
A. (5,)
B. (5)
C. [5]
D. {5}

16 What is the result of (10, 20, 30)[1]?

Tuples Easy
A. 30
B. 10
C. 20
D. IndexError

17 What does a Python dictionary store?

Dictionaries Easy
A. Key-value pairs
B. Characters only
C. Unique values only
D. A fixed sequence whose individual entries can never be changed after creation

18 Which expression creates a dictionary?

Dictionaries Easy
A. ["name", "Ana"]
B. {"name", "Ana"}
C. ("name", "Ana")
D. {"name": "Ana"}

19 Given student = {"name": "Sam", "age": 18}, which expression returns "Sam"?

Dictionaries Easy
A. student.name()
B. student[0]
C. student["name"]
D. student["Sam"]

20 Which dictionary method returns a view of all keys?

Dictionaries Easy
A. items()
B. keys()
C. The method that returns every key and value as a newly sorted list
D. values()

21 What is the value of result after this code?

text = 'DataStructures'

result = text[1:10:2]

Strings Medium
A. 'aStut'
B. 'aatut'
C. 'aatuc'
D. 'atrcu'

22 What does the following expression return?

'banana'.replace('a', 'o', 2)

Strings Medium
A. 'banana'
B. 'bonona'
C. 'bonano'
D. 'bonono'

23 What is printed by the following code?

text = ' py thon '

print(text.strip().replace(' ', '_'))

Strings Medium
A. py_thon
B. python
C. py__thon
D. __py_thon__

24 Given code = 'AB12-CD34', which expression returns '1234'?

Strings Medium
A. code[2:4] + code[7:9]
B. code[2:4] + code[6:8]
C. code[2:5] + code[6:8]
D. 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)

Lists Medium
A. [[1, 2], [3, 4]]
B. [[1, 2], [3, 4], [6]]
C. [[1, 2, 5], [3, 4]]
D. [[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]

Lists Medium
A. [2, 4, 6]
B. [2, 6, 10]
C. [0, 4, 8]
D. [1, 3, 5]

27 What is the final value of nums?

nums = [0, 1, 2, 3, 4]

nums[1:4] = [8, 9]

Lists Medium
A. [0, 8, 9, 3, 4]
B. [8, 9, 3, 4]
C. [0, 1, 8, 9, 4]
D. [0, 8, 9, 4]

28 What does the following expression return?

sorted(['pear', 'fig', 'apple', 'kiwi'], key=lambda x: (len(x), x))

Lists Medium
A. ['fig', 'apple', 'kiwi', 'pear']
B. ['apple', 'kiwi', 'pear', 'fig']
C. ['fig', 'kiwi', 'pear', 'apple']
D. ['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}?

Sets Medium
A. {1, 2, 5}
B. {1, 2}
C. {3, 4}
D. {1, 2, 3, 4, 5}

30 What is the value of the following set comprehension?

{x % 3 for x in range(7)}

Sets Medium
A. {0, 1, 2}
B. {0, 3, 6}
C. {0, 1, 2, 3}
D. {1, 2, 3}

31 What is printed by the following code?

s = {1, 2}

s.discard(3)

s.remove(2)

print(s)

Sets Medium
A. {1}
B. {2}
C. set()
D. {1, 3}

32 Given A = {1, 2} and B = {1, 2, 3}, which pair of expressions both evaluates to True?

Sets Medium
A. A == B and A <= B
B. A <= B and A < B
C. A < B and B < A
D. 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)

Tuples Medium
A. 1, 2, (3, 4)
B. 1, [2, 3], 4
C. 1, 2, [3, 4]
D. [1, 2], 3, 4

34 What is printed by the following code?

t = ([1, 2], 'x')

t[0].append(3)

print(t)

Tuples Medium
A. ([1, 2, 3], 'x')
B. ([1, 2], 'x', 3)
C. ([1, 2], 'x')
D. ([3, 1, 2], 'x')

35 What does the following expression return?

sorted([(2, 'b'), (1, 'c'), (1, 'a')])

Tuples Medium
A. [(1, 'a'), (2, 'b'), (1, 'c')]
B. [(2, 'b'), (1, 'a'), (1, 'c')]
C. [(1, 'c'), (1, 'a'), (2, 'b')]
D. [(1, 'a'), (1, 'c'), (2, 'b')]

36 What is printed by the following code?

t = (1, 2)

u = t

t += (3,)

print(t, u)

Tuples Medium
A. (1, 2, 3) (1, 2)
B. (1, 2) (1, 2)
C. (1, 2) (1, 2, 3)
D. (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)}

Dictionaries Medium
A. {0: 0, 1: 1}
B. {0: 4, 1: 1}
C. {0: 4, 1: 3}
D. {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)

Dictionaries Medium
A. {'a': [2], 'b': [3]}
B. {'a': [1, 2], 'b': [3]}
C. {'a': [1], 'b': [3]}
D. {'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

Dictionaries Medium
A. {'a': 1, 'b': 7, 'c': 6}
B. {'a': 1, 'b': 5, 'c': 6}
C. {'a': 1, 'b': 2, 'c': 6}
D. {'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)

Dictionaries Medium
A. {'x': [1]} {'x': [2]}
B. {'x': [1]} {'x': [1, 2]}
C. {'x': [1, 2]} {'x': [1]}
D. {'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"?

Strings Hard
A. ("852", "8520", False)
B. ("852", "258", False)
C. ("825", "852", False)
D. ("852", "852", True)

42 What does "mississippi".replace("iss", "X", 1).partition("s") return?

Strings Hard
A. ("mX", "s", "issippi")
B. ("mi", "s", "sissippi")
C. ("mXi", "ss", "ippi")
D. ("mXi", "s", "sippi")

43 What string is produced by "{0[1]:0{1}d}".format([3, 7], 4)?

Strings Hard
A. " 7"
B. "7000"
C. "0004"
D. "0007"

44 Given table = str.maketrans({"a": "xy", "b": None}), what is "abca".translate(table)?

Strings Hard
A. "xynonecxy"
B. "xybcxy"
C. "xycxy"
D. "acaxy"

45 What is a after executing a = [[0] * 2] * 3; a[0][1] = 7; a[1] = a[1] + [8]?

Lists Hard
A. [[0, 7], [0, 7, 8], [0, 0]]
B. [[0, 7], [0, 7, 8], [0, 7, 8]]
C. [[0, 7, 8], [0, 7], [0, 7]]
D. [[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]?

Lists Hard
A. [0, 9, 7, 8, 4, 5]
B. [0, 9, 7, 3, 4, 5]
C. [0, 7, 8, 4, 5]
D. [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?

Lists Hard
A. [2, 3, 4, 6]
B. [2, 4, 6]
C. [2, 4, 5, 6]
D. [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"})?

Lists Hard
A. [("d", 1), ("a", 2), ("c", 2), ("b", 1)]
B. [("b", 1), ("c", 2), ("d", 1), ("a", 2)]
C. [("a", 2), ("d", 1), ("b", 1), ("c", 2)]
D. [("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?

Sets Hard
A. {1, 2, 3, 4}
B. {1, 2, 3}
C. {3}
D. {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}?

Sets Hard
A. (3, True)
B. (7, False)
C. (3, False)
D. (4, True)

51 For A = {1, 2}, B = {1, 2}, and C = frozenset({1, 2}), what is (A <= B, A < B, C == A)?

Sets Hard
A. (True, True, False)
B. (True, False, False)
C. (False, False, True)
D. (True, False, True)

52 What does {1, 2} in {frozenset({1, 2}), frozenset()} evaluate to in Python?

Sets Hard
A. NotImplemented
B. True
C. False
D. TypeError

53 What are u and t after t = ([1],); u = t; t[0].append(2); t += ([3],)?

Tuples Hard
A. u == ([1],) and t == ([1, 2], [3])
B. u == ([1, 2], [3]) and t == ([1, 2], [3])
C. u == ([1, 2],) and t == ([1, 2], [3])
D. 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?

Tuples Hard
A. ([1, 2, 4], [3, 4])
B. ([1, 2], [3])
C. ([1, 2], [3, 4])
D. ([1, 2, 4], [3])

55 What is (a, b, c, d) after a, *b, (c, *d) = (1, 2, 3, (4, 5, 6))?

Tuples Hard
A. (1, [2], 3, [4, 5, 6])
B. (1, [2, 3], 4, (5, 6))
C. (1, [2, 3], 4, [5, 6])
D. (1, (2, 3), 4, [5, 6])

56 Which tuple can be used as a dictionary key without raising TypeError?

Tuples Hard
A. (1, [2, 3], frozenset({4}))
B. (1, (2, [3]), frozenset({4}))
C. (1, (2, 3), frozenset({4}))
D. (1, (2, 3), {4})

57 What is (len(d), list(d.keys()), d[True]) after d = {1: "int", True: "bool", 1.0: "float"}?

Dictionaries Hard
A. (2, [1, True], "bool")
B. (1, [1], "float")
C. (1, [1.0], "float")
D. (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]?

Dictionaries Hard
A. {"a": [1], "b": [1, 2]}
B. {"a": [1, 2], "b": [1, 2]}
C. {"a": [], "b": [1, 2]}
D. {"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())?

Dictionaries Hard
A. (1, [], {"k": 1})
B. (99, ["x"], {"k": 99})
C. (99, [], {"k": 1})
D. (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?

Dictionaries Hard
A. [("b", 9), ("c", 3), ("a", 7)]
B. [("a", 1), ("b", 2), ("c", 3)]
C. [("c", 3), ("a", 7), ("b", 9)]
D. [("a", 7), ("b", 9), ("c", 3)]