Courseiva
Exceptions and File I/O →easyMultiple Choice

PCAP Exceptions and File I/O Practice Question

Exhibit

try:
    with open('config.cfg', 'r') as f:
        data = f.read()
except FileNotFoundError:
    print('File not found')
except PermissionError:
    print('Permission denied')

Refer to the exhibit. If the file config.cfg exists but the user does not have read permission, what will be printed?

⚠ Common exam trap

Python Institute often tests the distinction between file existence errors (FileNotFoundError) and permission errors (PermissionError), and candidates mistakenly assume that a missing file is the only possible file-related exception.

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

✓

'Permission denied'

When `open()` is called on a file that exists but the user lacks read permission, Python raises a `PermissionError`. Since no exception handling is present, the exception is unhandled and Python prints a traceback to stderr which includes the error message `'Permission denied'` (among other details). Option B correctly identifies the permission-denied message that appears in the output.

Answer analysis

Option-by-option breakdown

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

  • ✗

    An unhandled exception is raised

    Why it's wrong here

    An unhandled exception cannot occur because the enclosing try block explicitly includes an `except PermissionError` clause. Once the runtime raises PermissionError, the handler is matched and the exception is caught, preventing propagation to the top level. Thus no traceback or unhandled-error termination occurs.

  • ✓

    'Permission denied'

    Why this is correct

    Since config.cfg exists but the process lacks the required read permission, the file's opening operation raises `PermissionError`. The corresponding `except PermissionError` clause catches it and executes its print statement, producing `'Permission denied'` as the program's visible output. This is the expected, handled outcome.

  • ✗

    Nothing; the program continues silently

    Why it's wrong here

    The exception handler does not contain a bare `pass` or suppress the error; it contains a `print('Permission denied')` call. Therefore the program produces output and does not silently continue, because control flows through the handler and prints to the console rather than skipping any visible side effect.

  • ✗

    'File not found'

    Why it's wrong here

    `FileNotFoundError` is raised only when the target file is missing from the filesystem. In this scenario the file exists, so the `except FileNotFoundError` branch is never entered; attempting to open it with no read permission condition instead raises `PermissionError`. These are distinct subclasses of `OSError`, so only the matching handler runs.

About these practice questions

One of 421 original PCAP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. 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.