Courseiva
Functions, Tuples, Dictionaries and ExceptionsmediumMultiple SelectObjective-mapped

PCEP Practice Question: Functions, Tuples, Dictionaries and Exceptions

Which TWO of the following are valid ways to merge two dictionaries in Python 3.5+? (Assume dict1 = {'a':1} and dict2 = {'b':2})

⚠ Common exam trap

The PCEP exam often tests the version-specific availability of operators like `|` and the existence of methods like `merge()`, exploiting candidates who assume all modern syntax works in older Python versions or that methods from other languages (e.g., JavaScript's `Object.assign`) exist in Python.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

dict1.update(dict2)

`dict1.update(dict2)` merges `dict2` into `dict1` in-place, updating existing keys and adding new ones. Option E is correct because `{**dict1, **dict2}` uses dictionary unpacking to create a new merged dictionary, available since Python 3.5.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • dict1 | dict2

    Why it's wrong here

    The | operator is available only in Python 3.9 and later; it is not a general solution for Python 3.5+.

  • dict1.update(dict2)

    Why this is correct

    The update method merges dict2 into dict1, modifying dict1 in place.

  • dict1 + dict2

    Why it's wrong here

    The + operator is not defined for dictionaries; it would raise a TypeError.

  • dict1.merge(dict2)

    Why it's wrong here

    There is no built-in method called merge for dictionaries in Python.

  • {**dict1, **dict2}

    Why this is correct

    Dictionary unpacking creates a new dictionary containing keys from both, with later keys overriding earlier ones.

About these practice questions

This PCEP question is part of Courseiva's 498-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This PCEP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCEP exam.