Courseiva

PCEP Practice Question: Functions, Tuples, Dictionaries and Exceptions

A dictionary student = {'name': 'John', 'age': 20}. To safely get the grade with a default of 'N/A', which code should be used?

⚠ Common exam trap

Python Institute often tests the distinction between safe dictionary access methods (`get()`) and direct indexing (`[]`), trapping candidates who think the `or` operator can short-circuit a `KeyError` or who invent non-existent methods like `fetch()`.

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

✓

student.get('grade', 'N/A')

The `get()` method of a dictionary safely retrieves the value for a given key, returning a default value (here `'N/A'`) if the key does not exist. This avoids raising a `KeyError` when the key `'grade'` is missing from the 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.

  • ✓

    student.get('grade', 'N/A')

    Why this is correct

    The get method returns the value for the given key, or the supplied default when the key is absent. Using student.get('grade', 'N/A') avoids a KeyError because 'grade' is not present, satisfying the requirement for safe retrieval with a fallback.

  • ✗

    student['grade']

    Why it's wrong here

    Indexing with `student['grade']` raises a KeyError because the key is absent, so no default is returned. It is tempting since square-bracket access is the standard way to read an existing key, and it would be correct when the key is guaranteed present and a missing key should signal an error.

  • ✗

    student.fetch('grade', 'N/A')

    Why it's wrong here

    `fetch` is not a dictionary method in Python, so calling it raises an `AttributeError` rather than returning `'N/A'`. The method name belongs to other languages' map APIs, which makes it tempting if you are translating from Java or JavaScript. Python's `dict.get('grade', 'N/A')` returns the default without raising.

  • ✗

    student['grade'] or 'N/A'

    Why it's wrong here

    Using 'or' returns the default only when the key's value is falsy, so a legitimate grade of 0 or empty string would wrongly yield 'N/A'. It also raises no KeyError, masking the missing key. dict.get('grade', 'N/A') returns the default precisely when the key is absent.

About these practice questions

Courseiva writes every PCEP question from scratch — 482 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or 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.