🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: The collections Module: all 10 questions

Quiz: The collections Module · Question 3 of 10

What does this print? (code)

What does this print?

from collections import defaultdict
d = defaultdict(int)
for ch in "aab":
    d[ch] += 1
print(dict(d))
  1. A.{'a': 1, 'b': 1}
  2. B.{'a': 2, 'b': 1}
  3. C.An error
  4. D.{'aab': 1}
Answer & explanation

Correct answer: {'a': 2, 'b': 1}

int() gives 0 for each new key, which is then increased.

Can you get the rest right without peeking?

Take the timed Quiz: The collections Module quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →