Given values = ["a", "b", "c"], what does values[1] return?
6 Python Collections Online Quiz Questions
Use this free practice quiz with 20 questions to review 6 Python Collections, test your knowledge, and prepare for your next test or exam.
True or false: In Python, (42,) is a one-item tuple, but (42) is not a tuple.
- A
True
- B
False
What is the result of numbers[1:4] when numbers = [0, 1, 2, 3, 4, 5]?
- A
[1, 2, 3] - B
[0, 1, 2] - C
[1, 2, 3, 4] - D
[2, 3, 4]
What list method creates a shallow copy of the outer list?
Complete this list comprehension: squares = [n * n n range(1, 4)].
Select all correct statements about iterating over the dictionary prices = {"pen": 1.50, "notebook": 4.00}.
- A
Iterating directly over a dictionary yields its keys.
- B
prices.items()can be used to iterate over key-value pairs. - C
prices.values()can be used to iterate over values only. - D
Iterating directly over a dictionary always yields key-value pairs.
- E
prices.items()yields only the values.
True or false: A for loop over a dictionary iterates over the dictionary's keys by default.
- A
True
- B
False
What integer does len([10, 20, 30, 40][1:3]) return?
Which set operator should be used to find the values common to both frontend and backend?
- A
| - B
& - C
- - D
^
Complete the expression so that a missing key produces zero: count = word_counts.get("python", )
Select all values that can be used as dictionary keys in Python.
- A
A string such as
"name" - B
A number such as
28 - C
A tuple containing only hashable values
- D
A list such as
[1, 2] - E
A dictionary such as
{"a": 1}
Explain how to choose among a list, tuple, dictionary, and set for a programming task. State the main deciding property of each collection type and include at least one suitable use case.
What is printed after this code runs? bad = [[]] * 3; bad[0].append("x"); print(bad)
- A
[['x'], [], []] - B
[[], ['x'], []] - C
[['x'], ['x'], ['x']] - D
An
IndexError
What integer does this code produce? scores = [88, 91]; scores.append(82); len(scores)
After this code runs, what is the value of values?
values = [3, 1, 2]
values.sort()
- A
[3, 1, 2] - B
[1, 2, 3] - C
None - D
An error occurs
Which expression creates a tuple containing exactly one string, "ready"?
- A
("ready") - B
["ready"] - C
("ready",) - D
{"ready"}
What value does this expression produce?
numbers = [10, 20, 30, 40, 50, 60]
numbers[1:5:2]
- A
[20, 40] - B
[10, 30, 50] - C
[20, 30, 40, 50] - D
[20, 40, 60]
What does letters[-2] evaluate to?
letters = ["a", "b", "c", "d"]
- A
"a" - B
"b" - C
"d" - D
"c"
True or false: A set supports positional indexing, so my_set[0] retrieves an arbitrary element.
- A
True
- B
False
Which built-in Python function should you use to iterate over two related collections in parallel, such as a list of names and a list of scores?