ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
d = {}
for w in ['b', 'a', 'b']:
    d[w] = d.get(w, 0) + 1
print(d, max(d, key=d.get), sorted(d.values(), reverse=True))
  1. A. {'b': 2, 'a': 1} a [1, 2]
  2. B. {'b': 2, 'a': 1} b [1, 2]
  3. C. {'b': 2, 'a': 1} b [2, 1]
  4. D. {'a': 1, 'b': 2} b [2, 1]

Correct answer: C. {'b': 2, 'a': 1} b [2, 1]

The dict keeps insertion order, so 'b' comes first. max with key=d.get returns the key with the largest count, which is b. The sorted values are 2 and 1 in descending order.

More Python Programming questions

Exams that ask this topic