Courseiva
Exceptions and File I/O →hardMultiple Choice

PCAP Exceptions and File I/O Practice Question

What is the output of the following code?

try:

raise ValueError('a') except ValueError as e:

print(e.args[0])

finally:

print('b')

⚠ Common exam trap

Python Institute often tests the misconception that `finally` runs before `except` or that `print()` outputs on the same line, leading candidates to choose options with incorrect order or missing newlines.

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

✓

a\nb

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

Answer analysis

Option-by-option breakdown

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

  • ✗

    a b

    Why it's wrong here

    The output is not "a b" on a single line because the code uses two separate print() calls. Each call to print() appends a newline by default, so 'a' is followed by a line break before 'b' is written. To get the literal text a b in one line, the code would need a separator, which is not present here.

  • ✗

    b a

    Why it's wrong here

    "b a" is impossible because Python's exception-handling order is strictly defined: once the exception is raised, the matching except clause executes before the finally clause. Therefore print('a') runs and completes first, and only then does the finally block execute print('b'). The finally block cannot jump ahead of the except handler.

  • ✓

    a\nb

    Why this is correct

    "a\nb" is the exact output. The raise ValueError in the try block transfers control to the except ValueError handler, whose print('a') writes 'a' plus a newline. After that handler finishes, the finally block unconditionally runs and print('b') writes 'b' plus a newline, yielding two lines: first 'a', second 'b'.

  • ✗

    b

    Why it's wrong here

    The output is not just b because the except block also contains a print call that is guaranteed to run before cleaning up in finally. Even though the finally block always executes, it does so after the except suite has already printed 'a'. Therefore any single-line output that omits 'a' ignores the except block's execution.

About these practice questions

This PCAP question is part of Courseiva's 421-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. 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.