ProgrammingHardTCS NQT · Infosys

What is printed?

What is printed?
from collections import Counter
c = Counter()
c['a'] += 2
c.update('ab')
print(c['a'], c['b'], c['z'], sorted(c.elements()))
  1. A. 3 1 0 ['a', 'a', 'b']
  2. B. 3 1 0 ['a', 'a', 'a', 'b']
  3. C. 2 1 0 ['a', 'a', 'a', 'b']
  4. D. 3 1 1 ['a', 'a', 'a', 'b']

Correct answer: B. 3 1 0 ['a', 'a', 'a', 'b']

Counter's missing keys read as zero, and += and update both add to the count. The count for a is 2 + 1 = 3, and elements() expands each count, which gives a three times.

More Python Programming questions

Exams that ask this topic