Courseiva
Exceptions and File I/O →easyMultiple Choice

PCAP Exceptions and File I/O Practice Question

Exhibit

Refer to the exhibit.

Exhibit:
    try:
        with open('config.json', 'r') as f:
            data = json.load(f)
    except FileNotFoundError:
        print('Missing config file')
    except json.JSONDecodeError:
        print('Invalid JSON')
    except Exception:
        print('Unexpected error')

Consider the following code:

import json

try:
    with open('config.json') as f:

data = json.load(f) except FileNotFoundError:

print("Missing config file")

except json.JSONDecodeError:

print("Invalid JSON")
except:
    print("Unexpected error")

If the file 'config.json' exists but contains invalid JSON, what is printed?

⚠ Common exam trap

Python Institute often tests the distinction between file existence errors (`FileNotFoundError`) and content parsing errors (`json.JSONDecodeError`), trapping candidates who assume any file problem results in a generic crash or a missing-file message.

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

✓

Invalid JSON

When the file exists but contains invalid JSON, attempting to parse it raises a json.JSONDecodeError. A program designed to handle this exception will catch it and print 'Invalid JSON', as indicated by option D. Options A and B are incorrect because the error is specifically a JSON parsing error, not an unexpected error or an unhandled crash. Option C is incorrect because the file does exist, so no file-not-found error occurs.

Answer analysis

Option-by-option breakdown

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

  • ✗

    Unexpected error

    Why it's wrong here

    An "unexpected error" would be an exception type that was not anticipated by the code. The question scenario explicitly says the program catches the specific exception (JSONDecodeError), so the exception is expected and handled. A generic "unexpected error" message implies no matching except block, which contradicts the premise that the error is caught.

  • ✗

    The program crashes with an unhandled exception.

    Why it's wrong here

    The program does not crash because the JSONDecodeError is caught by an except clause. In Python, an unhandled exception would terminate the execution and print a traceback, but because the exception is handled, control flow continues normally. The existence of a try/except block for this error means the program remains alive and executes the appropriate error-handling branch.

  • ✗

    Missing config file

    Why it's wrong here

    A missing config file would raise FileNotFoundError (or OSError with errno.ENOENT) when trying to open the file. In this scenario, the file exists and the open() call succeeds; the problem occurs after the file is read, when parsing its contents. Therefore, the failure is not about file absence but about the content being malformed JSON.

  • ✓

    Invalid JSON

    Why this is correct

    Invalid JSON is correct because the file exists and is readable, but its content cannot be parsed by json.load() or json.loads(). The json module raises json.JSONDecodeError, a subclass of ValueError, when the data does not conform to JSON syntax. The program's except block catches this exception, confirming the specific outcome is "Invalid JSON" rather than any file-system error.

About these practice questions

Courseiva writes every PCAP question from scratch — 421 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 PCAP 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 PCAP exam.