Python Collection Counters
Question 1
What is the primary purpose of the Counter class in Python's collections module?
- A
To create a mapping of unique keys to their values
- B
To count the occurrences of elements in an iterable
- C
To perform arithmetic operations on numbers
- D
To sort elements in a collection
Question 2
Which method would you use to retrieve the most common elements from a Counter object?
- A
retrieve_common()
- B
most_common()
- C
get_top()
- D
frequent_elements()
Question 3
When using the subtract() method on a Counter object, what happens to the counts of elements?
- A
Counts are doubled
- B
Counts can go negative
- C
Counts are reset to zero
- D
Counts are ignored
Question 4
How can you initialize a Counter with keyword arguments?
- A
Counter(**{key value})
- B
Counter({key value})
- C
Counter(key=value)
- D
Counter([key=value])
Question 5
Which of the following operations is NOT supported by Counter objects?
- A
Intersection
- B
Division
- C
Addition
- D
Subtraction
Question 6
What will be the output of accessing a non-existent key in a Counter object?
- A
None
- B
An error is raised
- C
0
- D
False
Question 7
What will be the output of the following code?
from collections import Counter
c = Counter("aabbbcccc")
print(c.most_common(1))
- A
['c']
- B
[('c', 4)]
- C
[('a', 2)]
- D
[('b', 3)]
Question 8
Which of the following can NOT be passed directly to Counter()?
- A
A string
- B
A dictionary
- C
A set
- D
A list
Question 9
What does Counter('aabb') + Counter('bccd') return?
- A
Counter({'a': 2, 'b': 3, 'c': 2, 'd': 1})
- B
Counter({'a': 2, 'b': 1, 'c': 2, 'd': 1})
- C
Counter({'a': 2, 'b': 2, 'c': 2})
- D
Counter({'a': 2, 'b': 2, 'c': 2, 'd': 0})
Question 10
What is the result of this code?
from collections import Counter
c1 = Counter(a=3, b=1)
c2 = Counter(a=1, b=2)
print(c1 - c2)
- A
Counter({'a': 2, 'b': -1})
- B
Counter({'a': 2})
- C
Counter({'b': -1})
- D
Counter({'a': 4, 'b': 3})
There are 11 questions to complete.
