Considering the following dictionary: Num = {10: 'Ten', 100: 'Hundred', 10:…
2023
Considering the following dictionary: Num = {10: 'Ten', 100: 'Hundred', 10: 'Decimal'} print(Num) What shall be the output of print(Num)?
Answer: C. {10: 'Decimal', 100: 'Hundred'} — In Python, dictionary literals are evaluated by inserting key-value pairs one at a time, in order. A dict can never hold two entries for the same key: if a…
- A.
{10: 'Ten', 100: 'Hundred', 10: 'Decimal'}
- B.
{10: 'Ten', 100: 'Hundred'}
- C.
{10: 'Decimal', 100: 'Hundred'}
- D.
Error
Attempted by 1903 students.
Show answer & explanation
Correct answer: C
In Python, dictionary literals are evaluated by inserting key-value pairs one at a time, in order. A dict can never hold two entries for the same key: if a key that already exists is assigned again, its value is overwritten by the new one, but the key's position in the dictionary -- set by when it was first inserted -- does not change, because Python dictionaries preserve insertion order.
Num[10] = 'Ten' is inserted first, so the dictionary becomes {10: 'Ten'}.
Num[100] = 'Hundred' is inserted next, giving {10: 'Ten', 100: 'Hundred'}.
Num[10] = 'Decimal' assigns to the key 10 again. Since 10 already exists, its value is overwritten to 'Decimal' and its position is unchanged, giving {10: 'Decimal', 100: 'Hundred'}.
Running this exact literal in a Python interpreter reproduces {10: 'Decimal', 100: 'Hundred'} -- confirming that a repeated key updates the existing entry instead of creating a duplicate.