Question 49 of 511
Exceptions and File I/OhardMultiple ChoiceObjective-mapped

Quick Answer

The answer is error done because the `exec('1/0')` statement raises a `ZeroDivisionError`, which is caught by the bare `except:` clause, printing 'error', while the `else` clause is skipped entirely since an exception occurred, but the `finally` clause always executes unconditionally, printing 'done'. This demonstrates the strict execution order of the try-except-else-finally construct: the `try` block runs first; if an exception is raised, the matching `except` block runs and the `else` block is bypassed; regardless of outcome, the `finally` block executes last. On the Certified Associate Python Programmer PCAP exam, this pattern tests your understanding of exception handling flow, often appearing in questions that hide a raised exception inside `exec()` or a function call to mislead you. A common trap is assuming `else` runs when any exception occurs, but it only runs if the `try` block completes without an exception. Remember the mnemonic: “Try, except if bad, else if good, finally always.”

PCAP Exceptions and File I/O Practice Question

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.

What is the output of the following code?

try:

exec('1/0')

except:
    print('error')

else:

print('no error')

finally:

print('done')
Question 1hardmultiple choice
Full question →

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

error done

Option A is correct because the `exec('1/0')` raises a `ZeroDivisionError`, which is caught by the bare `except:` clause, printing 'error'. The `else` clause is skipped because an exception occurred, but the `finally` clause always executes, printing 'done'. Thus the output is 'error' followed by 'done'.

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.

  • error done

    Why this is correct

    Correct order.

    Related concept

    Read the scenario before looking for a memorised answer.

  • done error

    Why it's wrong here

    Except runs before finally.

  • error

    Why it's wrong here

    Finally also runs.

  • done

    Why it's wrong here

    Except also prints.

Common exam traps

Common exam trap: answer the scenario, not the keyword

Python Institute often tests the order of execution in exception handling, specifically that `finally` always runs after `except` (not before), and that `else` is skipped when an exception occurs, causing candidates to misorder the output or forget the `finally` block.

Detailed technical explanation

How to think about this question

The `try/except/else/finally` construct in Python follows a strict execution order: the `try` block runs first; if an exception occurs, the matching `except` block executes and the `else` block is skipped; the `finally` block always runs after the `try` (and possibly `except`/`else`) completes, even if an exception propagates. The bare `except:` catches all exceptions, including `ZeroDivisionError`, and the `exec()` function executes a string as Python code, so `1/0` triggers the division by zero error.

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 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

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.

Related practice questions

Related PCAP practice-question pages

Use these pages to review the topic behind this question. This is how one missed question becomes focused revision.

Practice this exam

Start a free PCAP practice session

Short sessions build daily habit. Longer sessions build exam-day stamina. Try a timed session to simulate real conditions.

FAQ

Questions learners often ask

What does this PCAP question test?

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: error done — Option A is correct because the `exec('1/0')` raises a `ZeroDivisionError`, which is caught by the bare `except:` clause, printing 'error'. The `else` clause is skipped because an exception occurred, but the `finally` clause always executes, printing 'done'. Thus the output is 'error' followed by 'done'.

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 →

How Courseiva writes practice questions · Editorial policy

Same concept, more angles

3 more ways this is tested on PCAP

These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.

Variation 1. What is the output of the following code? try: print(1/0) except: print('err') finally: print('fin')

medium
  • A.fin err
  • B.fin
  • C.err fin
  • D.err

Why C: Option C is correct because when a division by zero occurs, Python raises a ZeroDivisionError, which is caught by the bare except clause, printing 'err'. The finally clause always executes after the except block, printing 'fin'. Thus the output is 'err' followed by 'fin'.

Variation 2. What will be the output of the following code? try: print(1/0) except: print('error') else: print('no error') finally: print('done')

easy
  • A.no error\ndone
  • B.error\nno error\ndone
  • C.done
  • D.error\ndone

Why D: Option D is correct because the code raises a ZeroDivisionError when attempting 1/0, which is caught by the bare except clause, printing 'error'. The else clause is skipped because an exception occurred. The finally clause always executes, printing 'done'. Thus the output is 'error' followed by 'done'.

Variation 3. What is the output of the following code? try: raise ValueError('a') except ValueError as e: print(e.args[0]) finally: print('b')

hard
  • A.a b
  • B.b a
  • C.a\nb
  • D.b

Why C: Option C is correct because the `finally` block always executes after the `try` block, even when an exception is raised. The `except` block catches the `ValueError` and prints the first argument of the exception (`'a'`), then the `finally` block prints `'b'`. The output is `a` on one line and `b` on the next, matching `a\nb`.

Keep practising

More PCAP practice questions

Last reviewed: Jun 30, 2026

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.

Loading comments…

Sign in to join the discussion.

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.