Notice: Due to an ongoing migration of our storage and publishing system, material requested since last month will not be uploaded for the time being, and new requests are paused. All existing notes remain available.
You're offline
Unit 2 MCQsPython data structures
ECAP776 ·
60 questions. Pick an option to check it.
Words are ordered first by length and then alphabetically when their lengths are equal.
Question 29 of 60 · Sets · Medium
What is the value of (A | B) - (A & B) when A = {1, 2, 3, 4} and B = {3, 4, 5}?
Not quite. Try another option.
Answer: {1, 2, 5}
The expression removes the common elements from the union, leaving elements found in exactly one set.
Question 30 of 60 · Sets · Medium
What is the value of the following set comprehension?
{x % 3 for x in range(7)}
Not quite. Try another option.
Answer: {0, 1, 2}
Remainders after division by 3 can only be 0, 1, or 2, and sets remove duplicates.
Question 31 of 60 · Sets · Medium
What is printed by the following code?
s = {1, 2}
s.discard(3)
s.remove(2)
print(s)
Not quite. Try another option.
Answer: {1}
discard(3) does nothing because 3 is absent, while remove(2) deletes 2.
Question 32 of 60 · Sets · Medium
Given A = {1, 2} and B = {1, 2, 3}, which pair of expressions both evaluates to True?
Not quite. Try another option.
Answer: A <= B and A < B
A is both a subset and a proper subset of B because B contains an additional element.
Question 33 of 60 · Tuples · Medium
What are the values of a, b, and rest after this assignment?
a, b, *rest = (1, 2, 3, 4)
Not quite. Try another option.
Answer: 1, 2, [3, 4]
Extended unpacking assigns the first two values individually and collects the remaining values into a list.
Question 34 of 60 · Tuples · Medium
What is printed by the following code?
t = ([1, 2], 'x')
t[0].append(3)
print(t)
Not quite. Try another option.
Answer: ([1, 2, 3], 'x')
The tuple cannot be structurally changed, but its mutable list element can still be modified.
Question 35 of 60 · Tuples · Medium
What does the following expression return?
sorted([(2, 'b'), (1, 'c'), (1, 'a')])
Not quite. Try another option.
Answer: [(1, 'a'), (1, 'c'), (2, 'b')]
Tuples are compared element by element, so the numbers are compared first and the strings break the tie.
Question 36 of 60 · Tuples · Medium
What is printed by the following code?
t = (1, 2)
u = t
t += (3,)
print(t, u)
Not quite. Try another option.
Answer: (1, 2, 3) (1, 2)
Tuple concatenation creates a new tuple for t; u continues to reference the original tuple.
Question 37 of 60 · Dictionaries · Medium
What is the value of d after this dictionary comprehension?
d = {x % 2: x for x in range(5)}
Not quite. Try another option.
Answer: {0: 4, 1: 3}
Repeated keys overwrite earlier values, so each key retains the last corresponding number.
Question 38 of 60 · Dictionaries · Medium
What is the final value of d?
d = {'a': [1]}
d.setdefault('a', []).append(2)
d.setdefault('b', []).append(3)
Not quite. Try another option.
Answer: {'a': [1, 2], 'b': [3]}
setdefault() returns the existing list for 'a' and creates a new empty list for the missing key 'b'.
Question 39 of 60 · Dictionaries · Medium
What is the value of merged after this code?
d1 = {'a': 1, 'b': 2}
d2 = {'b': 5, 'c': 6}
merged = d1 | d2
Not quite. Try another option.
Answer: {'a': 1, 'b': 5, 'c': 6}
The union operator combines both dictionaries, with values from the right dictionary replacing duplicate keys.
Question 40 of 60 · Dictionaries · Medium
What is printed by the following code?
original = {'x': [1]}
clone = original.copy()
clone['x'].append(2)
print(original, clone)
Not quite. Try another option.
Answer: {'x': [1, 2]} {'x': [1, 2]}
copy() is shallow, so both dictionaries reference the same nested list.
Question 41 of 60 · Strings · Hard
What is the value of (s[8:1:-3], s[-2::-3], s[8:1:-3] == s[-2::-3]) when s = "0123456789"?
Not quite. Try another option.
Answer: ("852", "852", True)
Both slices start at index 8 and move backward by 3, selecting indices 8, 5, and 2.
Question 42 of 60 · Strings · Hard
What does "mississippi".replace("iss", "X", 1).partition("s") return?
Not quite. Try another option.
Answer: ("mXi", "s", "sippi")
The first "iss" becomes "X", producing "mXissippi"; partition then splits around its first "s".
Question 43 of 60 · Strings · Hard
What string is produced by "{0[1]:0{1}d}".format([3, 7], 4)?
Not quite. Try another option.
Answer: "0007"
0[1] selects 7, while the nested field {1} makes the format specification 04d, padding 7 with zeros to width 4.
Question 44 of 60 · Strings · Hard
Given table = str.maketrans({"a": "xy", "b": None}), what is "abca".translate(table)?
Not quite. Try another option.
Answer: "xycxy"
Each a expands to xy, b is deleted because it maps to None, and c remains unchanged.
Question 45 of 60 · Lists · Hard
What is a after executing a = [[0] * 2] * 3; a[0][1] = 7; a[1] = a[1] + [8]?
Not quite. Try another option.
Answer: [[0, 7], [0, 7, 8], [0, 7]]
Initially all elements reference one inner list. The item mutation affects all three, but assigning a[1] replaces only that reference with a new list.
Question 46 of 60 · Lists · Hard
What is a after a = [0, 1, 2, 3, 4, 5]; a[1:5:2] = [9, 8]; a[2:4] = [7]?
Not quite. Try another option.
Answer: [0, 9, 7, 4, 5]
The extended slice replaces indices 1 and 3, yielding [0, 9, 2, 8, 4, 5]; the second slice then replaces 2, 8 with one element.
Question 47 of 60 · Lists · Hard
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?
Not quite. Try another option.
Answer: [2, 4, 6]
Removing each encountered odd value shifts the next element left, so the iterator subsequently encounters the next odd value at its new index.
Question 48 of 60 · Lists · Hard
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"})?
Not quite. Try another option.
Answer: [("d", 1), ("a", 2), ("b", 1), ("c", 2)]
Python's sort is stable. The second sort places false keys before true keys while preserving the order established by the first sort within each group.
Question 49 of 60 · Sets · Hard
For A = {1, 2, 3}, B = {2, 3, 4}, C = {3, 4, 5}, and D = {4}, what is A | B & C - D?
Not quite. Try another option.
Answer: {1, 2, 3}
Operator precedence evaluates C - D first, then its intersection with B, and finally the union with A.
Question 50 of 60 · Sets · Hard
What is the value of (len(s), s == {0, 1, 2}) after s = {True, 1, 1.0, False, 0, 0.0, 2}?
Not quite. Try another option.
Answer: (3, True)
True, 1, and 1.0 are equal with matching hashes, as are False, 0, and 0.0, so only three distinct set elements remain.
Question 51 of 60 · Sets · Hard
For A = {1, 2}, B = {1, 2}, and C = frozenset({1, 2}), what is (A <= B, A < B, C == A)?
Not quite. Try another option.
Answer: (True, False, True)
A is a subset but not a proper subset of equal set B. A set and frozenset compare equal when they contain the same elements.
Question 52 of 60 · Sets · Hard
What does {1, 2} in {frozenset({1, 2}), frozenset()} evaluate to in Python?
Not quite. Try another option.
Answer: True
For set membership, an unhashable set operand is temporarily treated as an equivalent frozenset, which matches the stored element.
Question 53 of 60 · Tuples · Hard
What are u and t after t = ([1],); u = t; t[0].append(2); t += ([3],)?
Not quite. Try another option.
Answer: u == ([1, 2],) and t == ([1, 2], [3])
Appending mutates the shared inner list. Tuple concatenation then creates a new tuple and rebinds only t.
Question 54 of 60 · Tuples · Hard
After executing t = ([1, 2], [3]); try: t[0] += [4]\nexcept TypeError: pass, what is t?
Not quite. Try another option.
Answer: ([1, 2, 4], [3])
The list's in-place addition succeeds before Python attempts—and fails—to assign the list back into the immutable tuple.
Question 55 of 60 · Tuples · Hard
What is (a, b, c, d) after a, *b, (c, *d) = (1, 2, 3, (4, 5, 6))?
Not quite. Try another option.
Answer: (1, [2, 3], 4, [5, 6])
Each starred target collects unmatched elements into a list at its own unpacking level.
Question 56 of 60 · Tuples · Hard
Which tuple can be used as a dictionary key without raising TypeError?
Not quite. Try another option.
Answer: (1, (2, 3), frozenset({4}))
A tuple is hashable only when every nested element is hashable. Lists and mutable sets make the other tuples unhashable.
Question 57 of 60 · Dictionaries · Hard
What is (len(d), list(d.keys()), d[True]) after d = {1: "int", True: "bool", 1.0: "float"}?
Not quite. Try another option.
Answer: (1, [1], "float")
The three keys compare equal and have matching hashes. Later entries update the value, while the first inserted key object remains.
Question 58 of 60 · Dictionaries · Hard
What is d after d = dict.fromkeys(["a", "b"], []); d["a"].append(1); d["b"] = d["b"] + [2]?
Not quite. Try another option.
Answer: {"a": [1], "b": [1, 2]}
fromkeys initially shares one list between both keys. The append affects that list, but concatenation creates a new list assigned only to b.
Question 59 of 60 · Dictionaries · Hard
Given calls = [], def make(): calls.append("x"); return 99, and d = {"k": 1}, what are (v, calls, d) after v = d.setdefault("k", make())?
Not quite. Try another option.
Answer: (1, ["x"], {"k": 1})
Function arguments are evaluated before setdefault runs, so make() is called even though the key already exists.
Question 60 of 60 · Dictionaries · Hard
What is list(d.items()) after d1 = {"a": 1, "b": 2}; d2 = {"b": 9, "c": 3, "a": 7}; d = d1 | d2?
Not quite. Try another option.
Answer: [("a", 7), ("b", 9), ("c", 3)]
The right dictionary supplies values for duplicate keys, but those keys retain their positions from the left dictionary; the new key c is appended.
That's unit 2
Answer a few questions and your score shows up here.
Nearly through. Did unit 2 of ECAP776 save you a night before the exam?
LPU Notes is free. A student pays for the servers.
Did unit 2 of ECAP776 save you a night before the exam?
Then you can help keep it free for the next batch. 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.