PCAP Exceptions and File I/O Practice Question
Exhibit
try:
with open('data.txt') as f:
print(f.read())
except FileNotFoundError:
try:
with open('backup.txt') as f:
print(f.read())
except FileNotFoundError:
print('No file found')Refer to the exhibit. If both data.txt and backup.txt do not exist, what is the output?
⚠ Common exam trap
Python Institute often tests the distinction between output produced before an unhandled exception and the exception itself, tricking candidates into thinking the program crashes without any output.
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
✓
'No file found'
The code uses a try-except block to catch FileNotFoundError when attempting to open 'data.txt'. In the except block, it prints 'No file found' and then attempts to open 'backup.txt'. Since both files do not exist, the except block executes and prints 'No file found' before the second open attempt raises another FileNotFoundError, which is unhandled and terminates the program. However, the question asks for the output, which is the print statement executed before the error.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Prints contents of backup.txt
Why it's wrong here
This option assumes the program attempts to open backup.txt and read its contents successfully. However, the question states that both data.txt and backup.txt do not exist, so the inner open() call raises FileNotFoundError before any read occurs; thus, no file contents can possibly be printed.
- ✗
Nothing
Why it's wrong here
The program's control flow reaches the print statement inside the inner except block because the inner open('backup.txt') raises FileNotFoundError. That print statement outputs the string 'No file found', so the program does produce visible output; 'Nothing' is incorrect because an except handler actively executes and generates text.
- ✗
FileNotFoundError
Why it's wrong here
A FileNotFoundError is indeed raised when the inner open('backup.txt') executes, but it is caught by the matching except FileNotFoundError block immediately below it. The handler consumes the exception and runs its own code, so the error does not propagate out of the try block, does not reach the interpreter, and therefore no FileNotFoundError is shown to the user.
- ✓
'No file found'
Why this is correct
Because neither data.txt nor backup.txt exists, the outer try raises FileNotFoundError, and then the inner try also raises FileNotFoundError. The inner except FileNotFoundError block catches this second exception and executes print('No file found'), making that literal string the exact output displayed by the program.
Go deeper
Related to this question
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 →
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.