ProgrammingModerateTCS NQT · Infosys pattern

What is printed?

What is printed?
from collections import Counter
c = Counter("mississippi")
print(c.most_common(2), c["z"], sum(c.values()))
  1. A. [('s', 4), ('i', 4)] 0 11
  2. B. [('i', 4), ('s', 4)] KeyError 11
  3. C. [('i', 4), ('p', 2)] 0 11
  4. D. [('i', 4), ('s', 4)] 0 11

Correct answer: D. [('i', 4), ('s', 4)] 0 11

i and s both occur 4 times; most_common breaks ties by first-insertion order, and 'i' is seen before 's'. A missing key in a Counter returns 0 instead of raising KeyError. The counts add up to the string length, 11.

More Python Programming questions

Exams that ask this topic