ProgrammingHardGATE CS · Product-company placement

What is printed?

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

Correct answer: B. 5 -3 Counter({'a': 5}) 5

update adds counts (a: 5, b: 2), and subtract can go negative (b: -3). Unary + keeps only positive counts. elements() repeats each key by its positive count.

More Python Programming questions

Exams that ask this topic