Courseiva
Computer Programming and Python FundamentalsmediumMultiple SelectObjective-mapped

PCEP Computer Programming and Python Fundamentals Practice Question

Which two of the following are correct ways to create a dictionary in Python? (Choose two.)

⚠ Common exam trap

The PCEP exam often tests the distinction between valid dictionary creation syntax and common mistakes like missing quotes around keys, using wrong brackets, or misunderstanding how the `dict()` constructor accepts arguments.

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

my_dict = dict(a=1, b=2)

The `dict()` constructor accepts keyword arguments, where each keyword becomes a string key and its argument becomes the corresponding value. This creates `{'a': 1, 'b': 2}`. Option D is correct because it uses the standard literal syntax with colon-separated key-value pairs inside curly braces, which is the most common way to define a dictionary.

Answer analysis

Option-by-option breakdown

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

  • my_dict = {a:1, b:2}

    Why it's wrong here

    Uses variables a and b as keys, not the strings 'a' and 'b'.

  • my_dict = dict(a=1, b=2)

    Why this is correct

    Valid dict constructor.

  • my_dict = dict(['a',1],['b',2])

    Why it's wrong here

    Invalid syntax; should be list of tuples or dict() with mapping.

  • my_dict = {'a':1, 'b':2}

    Why this is correct

    Standard dictionary literal.

  • my_dict = ["a":1, "b":2]

    Why it's wrong here

    List with colon syntax, invalid.

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

Same concept, more angles

1 more way this is tested on PCEP

These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.

Variation 1. Which TWO of the following are valid ways to create a dictionary with initial key-value pairs? (Select exactly 2)

medium
  • A.d = {'a': 1, 'b': 2}
  • B.d = ('a'=1, 'b'=2)
  • C.d = dict.fromkeys(['a', 'b', 'c']) # without value
  • D.d = dict(a=1, b=2)
  • E.d = dict(['a', 'b'], [1, 2])

Why A: It uses the standard literal syntax for creating a dictionary with initial key-value pairs. The curly braces `{}` with colon-separated keys and values are the most common and direct way to define a dictionary in Python.

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.