ProgrammingVery hardCAT · MBA placement

What is printed?

What is printed?
from collections import Counter
c = Counter(a=1)
c.subtract({'a': 3, 'b': 1})
print(c, c['a'] + c['b'])
  1. A. Counter({'a': -2, 'b': -1}) -3
  2. B. Counter({'a': -2}) -3
  3. C. Counter({'a': 2, 'c': 1}) 3
  4. D. Counter({'b': -1, 'a': -2}) -3

Correct answer: D. Counter({'b': -1, 'a': -2}) -3

subtract keeps negative counts and adds missing keys. a goes from 1 to -2 and b from 0 to -1, and the repr lists the largest count first, so b appears before a.

More Python Programming questions

Exams that ask this topic