Collection in Python MCQs Exercise II


Ques 1 Collection


What is the purpose of the Counter class in the collections module?
  a) It provides an ordered collection of unique elements.
  b) It counts the frequency of elements in a collection.
  c) It provides a mutable collection of key-value pairs.
  d) It represents an immutable collection of elements.


b is the correct option





Ques 2 Collection


What is the output of the following Python Code?

from collections import namedtuple
Person = namedtuple('Person', ['name', 'age', 'gender'])
person1 = Person('Alice', 25, 'Female')
print(person1.age)

  a) 'Alice'
  b) 25
  c) 'Female'
  d) ('Alice', 25, 'Female')


b is the correct option





Ques 3 Collection


What is the output of the following Python Code?

from collections import deque
my_deque = deque([1, 2, 3])
my_deque.append(4)
my_deque.popleft()
print(len(my_deque))

  a) 1
  b) 2
  c) 3
  d) 4


c is the correct option





Ques 4 Collection


What is the output of the following Python Code?

from collections import Counter
my_list = [1, 2, 3, 4, 5, 1, 2, 3, 1, 2]
my_counter = Counter(my_list)
print(my_counter.most_common(1))

  a) [(1, 3)]
  b) [(1, 1)]
  c) [(1, 2)]
  d) [(2, 3)]


a is the correct option