Exhibit
try:
x = 1 / 0
except ZeroDivisionError:
print('A')
except:
print('B')
else:
print('C')
finally:
print('D')This PCAP practice question tests your understanding of exceptions and file i/o. Read the scenario carefully and evaluate each option against the stated constraints before committing to an answer. 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.
try:
x = 1 / 0
except ZeroDivisionError:
print('A')
except:
print('B')
else:
print('C')
finally:
print('D')Refer to the exhibit. What is the output?
try:
x = 1 / 0
except ZeroDivisionError:
print('A')
except:
print('B')
else:
print('C')
finally:
print('D')Answer choices
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
A D
Option D is correct because the code defines a custom exception class `MyError` that inherits from `Exception`, and the `try` block raises `MyError` with the message 'A'. The first `except` clause catches `MyError` and prints 'A', then the `else` clause is skipped because an exception was raised, and the `finally` clause always executes, printing 'D'. Thus the output is 'A D'.
Key principle: Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.
Common exam traps
Python Institute (PCAP) often tests the order of `except` clauses and the behavior of `else` versus `finally` — the trap here is that candidates forget the `else` block is skipped when an exception is raised, and mistakenly think the `finally` block is optional or conditional.
Detailed technical explanation
In Python's exception handling, the `else` clause executes only if the `try` block completes without raising an exception, while the `finally` clause executes unconditionally. Custom exceptions like `MyError` should inherit from `Exception` (or a subclass) to be caught by `except Exception`; inheriting from `BaseException` would catch system-exiting exceptions like `SystemExit`, which is not recommended. This pattern is critical in resource management (e.g., file I/O) where cleanup must occur regardless of errors.
Key takeaway
Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.
Real-world example
A cloud solutions architect for a retail company is evaluating services for a new workload. The correct answer here reflects best practice for the specific scenario described — not a general cloud recommendation. Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option. Cloud exam questions reward reading the constraint carefully: the same technology can be right or wrong depending on the use case.
What to study next
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 — study guide chapter
Learn the concepts, then practise the questions
Exceptions and File I/O practice questions
Targeted practice on this topic area only
All PCAP questions
521 questions across all exam domains
Certified Associate Python Programmer PCAP study guide
Full concept coverage aligned to exam objectives
PCAP practice test guide
How to use practice tests most effectively before exam day
Related practice questions
Use these pages to review the topic behind this question. This is how one missed question becomes focused revision.
Practise PCAP questions linked to Modules and Packages.
Practise PCAP questions linked to Strings.
Practise PCAP questions linked to Object-Oriented Programming.
Practise PCAP questions linked to Exceptions and File I/O.
Practise PCAP questions linked to PCAP fundamentals.
Practise PCAP questions linked to PCAP scenario.
Practise PCAP questions linked to PCAP troubleshooting.
Practice this exam
Short sessions build daily habit. Longer sessions build exam-day stamina. Try a timed session to simulate real conditions.
FAQ
Exceptions and File I/O — This question tests Exceptions and File I/O — Read the scenario before looking for a memorised answer..
The correct answer is: A D — Option D is correct because the code defines a custom exception class `MyError` that inherits from `Exception`, and the `try` block raises `MyError` with the message 'A'. The first `except` clause catches `MyError` and prints 'A', then the `else` clause is skipped because an exception was raised, and the `finally` clause always executes, printing 'D'. Thus the output is 'A D'.
Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.
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 →
Keep practising
Last reviewed: Jul 4, 2026
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.