Courseiva
Question 206 of 498
Computer Programming and Python FundamentalsmediumMultiple ChoiceObjective-mapped

PCEP Computer Programming and Python Fundamentals Practice Question

You are working on a Python application that interacts with an external API to fetch user data. The API returns JSON responses. Occasionally, the API returns a response with a missing key that your code assumes always exists, causing a KeyError. The application is critical and must continue functioning even if some data is incomplete. The data is processed in a loop over a list of user IDs. Your team lead suggests using the dictionary's get() method with a default value. However, the nested structure may have missing keys at multiple levels. What is the most robust way to handle this?

⚠ Common exam trap

The Python Institute often tests the distinction between catching exceptions at the loop level versus inside the loop, and candidates mistakenly choose wrapping the entire loop in a try-except (Option C) because they think it handles all errors, not realizing it terminates the loop on the first failure.

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

Use a try-except inside the loop to catch KeyError and other expected exceptions for each item.

It combines targeted exception handling with loop continuity. By placing a try-except inside the loop, you catch KeyError (and optionally other expected exceptions) for each individual user ID, log or handle the failure, and then continue processing the next item. This avoids crashing the entire loop while still allowing normal processing for valid data, which is more robust than blindly using .get() on deeply nested structures or catching all exceptions outside the loop.

Answer analysis

Option-by-option breakdown

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

  • Use .get() with defaults for every key access, including nested ones, to avoid exceptions.

    Why it's wrong here

    Nested .get() calls become complex and still can fail if a middle key is missing.

  • Check for key existence using 'in' for every key before accessing.

    Why it's wrong here

    Cumbersome for deeply nested data; missing key at any level stops processing.

  • Wrap the entire loop in a try-except that catches any exception and continues.

    Why it's wrong here

    Too broad; may hide genuine errors like network issues.

  • Use a try-except inside the loop to catch KeyError and other expected exceptions for each item.

    Why this is correct

    Precise exception handling allows logging and skipping only problematic items.

About these practice questions

Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Last reviewed: Jul 4, 2026

Question Discussion

Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.

Loading comments…

Sign in to join the discussion.

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.