What will be the output of the following Python code ? D1={'A':5, 'B':7,…

2023

What will be the output of the following Python code ? D1={'A':5, 'B':7, 'C':9} D2={'B':5, 'D':10} D1.update(D2) print(D1)

  1. A.

    {'A':5, 'B':5, 'C':9, 'D':10} / {'A':5, 'B':5, 'C':9, 'D':10}

  2. B.

    {'A':5, 'B':5, 'C':9, 'B':5, 'D':10} / {'A':5, 'B':5, 'C':9, 'B':5, 'D':10}

  3. C.

    {'A':5, 'C':9, 'D':10} / {'A':5, 'C':9, 'D':10}

  4. D.

    {'B':7, 'D':10, 'A':5, 'C':9} / {'B':7, 'D':10, 'A':5, 'C':9}

Attempted by 1293 students.

Show answer & explanation

Correct answer: A

Key insight: D1.update(D2) copies key-value pairs from D2 into D1. For keys that already exist in D1, their values are overwritten by the values from D2. Any keys present only in D2 are added to D1.

  • Start with the given dictionaries:

    D1 = {'A': 5, 'B': 7, 'C': 9}

    D2 = {'B': 5, 'D': 10}

  • Apply update:

    D1.update(D2) changes the value of 'B' in D1 from 7 to 5 (overwritten), and adds the new key 'D' with value 10.

  • Final result printed by print(D1):

    {'A': 5, 'B': 5, 'C': 9, 'D': 10}

  • Note: The important part is the key-value pairs after update; dictionary ordering is insertion-based in modern Python but does not affect which keys/values are present.

Explore the full course: Rssb Senior Computer Instructor