The correct answer is 'Permission denied' because when Python’s `open()` function attempts to access a file that exists but the user lacks read permission, it raises a `PermissionError` exception, and since the code contains no try-except block to handle it, the program terminates and prints the exception’s string representation. This behavior stems from Python’s default error-handling mechanism: unhandled exceptions halt execution and display the error type and message. On the Certified Associate Python Programmer PCAP exam, this scenario tests your understanding of file operation exceptions and the importance of exception handling—a common trap is assuming the program will silently fail or return None. Remember that `PermissionError` is a subclass of `OSError`, so catching the parent class can also handle permission issues. Memory tip: think “no permission, no exception handler—Python prints the error banner.”
PCAP Exceptions and File I/O Practice Question
This PCAP practice question tests your understanding of exceptions and file i/o. Match the stated requirement to the specific cloud service, access model, or configuration option — many options are valid in isolation but not for this scenario. After answering, compare your reasoning against the explanation and wrong-answer breakdown below. Once you have made your selection, read the full explanation to reinforce the concept and understand why each distractor is designed to mislead on exam day.
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?
try:
with open('config.cfg', 'r') as f:
data = f.read()
except FileNotFoundError:
print('File not found')
except PermissionError:
print('Permission denied')
A
An unhandled exception is raised
Why wrong: PermissionError is explicitly handled.
B
'Permission denied'
Correctly caught by PermissionError.
C
Nothing; the program continues silently
Why wrong: Exception is caught and printed.
D
'File not found'
Why wrong: File exists, so FileNotFoundError is not raised.
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 the `open()` function is called on a file that exists but the user lacks read permission, Python raises a `PermissionError` exception. The code does not include a try-except block to handle this exception, so the program terminates with an unhandled exception and prints the error message 'Permission denied' (the string representation of the exception). Option B is correct because that is the default behavior of Python when such an exception is not caught.
Key principle: Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.
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
PermissionError is explicitly handled.
✓
'Permission denied'
Why this is correct
Correctly caught by PermissionError.
Related concept
Read the scenario before looking for a memorised answer.
✗
Nothing; the program continues silently
Why it's wrong here
Exception is caught and printed.
✗
'File not found'
Why it's wrong here
File exists, so FileNotFoundError is not raised.
Common exam traps
Common exam trap: answer the scenario, not the keyword
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.
Detailed technical explanation
How to think about this question
Under the hood, the `open()` function in Python calls the underlying OS system call (e.g., `open()` on POSIX systems), which returns an error code (EACCES) when permissions are insufficient. Python's C-level wrapper converts this into a `PermissionError` (a subclass of `OSError`). In real-world scenarios, such as reading configuration files in restricted directories (e.g., /etc/shadow), failing to handle this exception can crash a service, making proper exception handling critical for robust applications.
KKey Concepts to Remember
Read the scenario before looking for a memorised answer.
Find the constraint that changes the correct option.
Eliminate answers that are true in general but not in this case.
TExam Day Tips
→Watch for words such as best, first, most likely and least administrative effort.
→Review why wrong options are wrong, not only why the correct option is correct.
Key takeaway
Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.
Real-world example
How this comes up in practice
A company's IT admin needs to give a contractor read-only access to production logs without sharing account credentials. Using role-based access control (RBAC) and temporary scoped permissions — not a permanent shared password — is the correct pattern. Questions like this test whether you can apply least-privilege access across cloud identity services.
What to study next
Got this wrong? Here's your next step.
Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.
Exceptions and File I/O — This question tests Exceptions and File I/O — Read the scenario before looking for a memorised answer..
What is the correct answer to this question?
The correct answer is: 'Permission denied' — When the `open()` function is called on a file that exists but the user lacks read permission, Python raises a `PermissionError` exception. The code does not include a try-except block to handle this exception, so the program terminates with an unhandled exception and prints the error message 'Permission denied' (the string representation of the exception). Option B is correct because that is the default behavior of Python when such an exception is not caught.
What should I do if I get this PCAP question wrong?
Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.
What is the key concept behind this question?
Read the scenario before looking for a memorised answer.
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 →
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.
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.
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.
Sign in to join the discussion.