Python Practice Test: Lists and Dictionaries
- Lists, Dictionaries, Tuples and Sets
- 10 questions
- 13 minutes
Ten questions on Python's built-in collections. Tip: ask yourself whether each operation changes the original object or creates a new one.
Tap an option to mark your answer. You can change your mind, or tap the same option again to leave the question blank. The correct answers and explanations appear when you finish the test.Tap an option to answer; tap it again to leave the question blank.
Your result
- 0 correct
- 0 wrong
- 0 blank
-
Question 1
nums = [4, 8, 15, 16]
print(nums[-1], nums[1:3])What does the code print?
Correct answer: B
Index -1 is the last item. The slice 1:3 includes index 1 and 2 but stops before index 3.
-
Question 2
a = [1]
a.append([2, 3])
print(len(a))What does the code print?
Correct answer: A
append adds its argument as one single item, so the list holds 1 and the inner list [2, 3]. extend would add 2 and 3 separately.
-
Question 3
d = {"a": 1}
d["b"] = 2
print(list(d.keys()))What does the code print?
Correct answer: D
Assigning to a new key adds it. Dictionaries keep insertion order, so a comes before b.
-
Question 4
d = {"a": 1}
print(d.get("z", 0))What does the code print?
Correct answer: C
get returns the default value when the key is missing instead of raising an error. Without a default it would return None.
-
Question 5
names = ["bob", "Al", "cy"]
names.sort()
print(names)What does the code print?
Correct answer: B
Strings are compared by character code, and uppercase letters come before lowercase ones, so Al is first.
-
Question 6
print([x * x for x in range(5) if x % 2 == 0])
What does the code print?
Correct answer: A
The condition keeps 0, 2 and 4, and each is squared.
-
Question 7
t = (1, 2, 3)
t[0] = 9What happens when this code runs?
Correct answer: D
Tuples are immutable, so item assignment is not allowed.
-
Question 8
print(len({1, 2, 2, 3, 3, 3}))
What does the code print?
Correct answer: C
A set keeps only unique values, so it holds 1, 2 and 3.
-
Question 9
nums = [1, 2, 3]
print(nums.pop(0), nums)What does the code print?
Correct answer: A
pop(0) removes and returns the item at index 0, leaving the rest of the list.
-
Question 10
a = [1, 2, 3]
b = a[:]
b[0] = 99
print(a)What does the code print?
Correct answer: B
A slice makes a new list, so changing b does not affect a.
Questions you leave blank are not counted as wrong.