🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Python Dictionaries: all 15 questions

Quiz: Python Dictionaries · Question 4 of 15

What does this print? (code)

What does this print?

d = {}
for ch in "aab":
    d[ch] = d.get(ch, 0) + 1
print(d)
  1. A.{'a': 2, 'b': 1}
  2. B.{'a': 1, 'b': 1}
  3. C.{'aab': 1}
  4. D.{'a': 2}
Answer & explanation

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

Each character is counted: a twice, b once.

Can you get the rest right without peeking?

Take the timed Quiz: Python Dictionaries quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →