ProgrammingHardTCS NQT · Infosys

What is printed?

What is printed?
words = 'the quick the lazy'.split()
counts = {}
for w in words:
    counts[w] = counts.get(w, 0) + 1
print(counts, max(counts, key=counts.get))
  1. A. {'quick': 1, 'the': 2, 'lazy': 1} the
  2. B. {'the': 2, 'quick': 1, 'lazy': 1} the
  3. C. {'the': 2, 'quick': 1, 'lazy': 1} quick
  4. D. {'the': 2, 'lazy': 1, 'quick': 1} the

Correct answer: B. {'the': 2, 'quick': 1, 'lazy': 1} the

A dict keeps keys in the order they were first inserted, so the, quick and lazy come in that order. max with key=counts.get returns the key with the largest count, which is the.

More Python Programming questions

Exams that ask this topic