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"))- A. 5 0 Counter({'a': 5}) 5
- B. 5 -3 Counter({'a': 5}) 5
- C. 5 -3 Counter({'a': 5, 'b': -3}) 5
- 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
- What is printed? python from collections import ChainMap defaults = {"color": "red", "size": 1} user = {"size": 3} cm = ChainMap(user…
- What is printed? python import bisect grades = "FDCBA" cut = [60, 70, 80, 90] print([grades[bisect.bisect(cut, s)] for s in [59, 60, 85…
- What is printed? python x = [1, 2, 3] y = [x, x] y[0] = y[0] + [4] y[1] += [5] print(x, y)
- What is printed? python def f(a, b=[]): b = b + [a] f.defaults = (b,) return b print(f(1), f(2), f(3, []), f(4))
- What is printed? python it = (x x for x in range(5)) first = next(it) rest = [v for v in it if v % 2 == 0] again = sum(1 for in it)…
- What is printed? python def g(): for i in range(3): try: yield i finally: print(f"f{i}", end=" ") for v in g(): if v == 1: break…