Question 1
Which of the following is not a part of the collections module in Python?
Counter
OrderedDict
ArrayList
deque
Question 2
What is the output of the following code?
from collections import
Counterprint(Counter('gfg'))
Counter({'g': 1, 'f': 1})
Counter({'g': 2, 'f': 1})
Counter({'f': 2, 'g': 1})
{'g': 2, 'f': 1}
Question 3
What is the output of this code?
from collections import defaultdict
d = defaultdict(lambda: 'Not Present')
d['a'] = 10
print(d['a'], d['b'])
10 KeyError
10 Not Present
10 None
KeyError Not Present
Question 4
Which collections class maintains the order in which keys are inserted?
OrderedDict
defaultdict
Counter
deque
Question 5
What will be the final order of keys in this code?
from collections import OrderedDict
od = OrderedDict()
od['a'] = 1
od['b'] = 2
od['c'] = 3
od.move_to_end('b')
print(list(od.keys()))
['a', 'c', 'b']
['b', 'a', 'c']
['a', 'b', 'c']
['c', 'b', 'a']
Question 6
Given this code, what will be the output?
from collections import namedtuple
Point = namedtuple('Point', ['x', 'y'])
p = Point(1, y=3)
print(p.x + p.y)
4
3
TypeError
SyntaxError
Question 7
Which collection type is best for implementing a stack or queue efficiently?
list
deque
set
tuple
Question 8
What will be the result of this code?
from collections import Counter
c = Counter(a=2, b=3)
d = Counter(a=1, b=2)
print(c - d)
Counter({'a': 1, 'b': 1, 'c': 0})
Counter({'a': -1, 'b': -1})
Counter()
Counter({'a': 1, 'b': 1})
Question 9
Which of the following features is unique to ChainMap?
It chains multiple dictionaries into a single view
It maintains the insertion order of keys
It counts frequency of elements
It provides default values for missing keys
Question 10
What will the following code print?
from collections import ChainMap
dict1 = {'a': 1, 'b': 2}
dict2 = {'b': 3, 'c': 4}
cm = ChainMap(dict1, dict2)
print(cm['b'])
4
3
2
KeyError
There are 10 questions to complete.