Courseiva

PCEP Computer Programming and Python Fundamentals Practice Question

Exhibit

try:
    x = int("abc")
except ValueError as e:
    print("Error:", e)
finally:
    print("Done")

Refer to the exhibit. Which of the following is true about the output?

⚠ Common exam trap

The PCEP exam often tests the misconception that the finally block suppresses or replaces the exception output, or that the except block does not print the exception message, leading candidates to overlook the explicit print of the error message before 'Done'.

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

✓

Prints "Error: invalid literal for int() with base 10: 'abc'" then "Done"

The code attempts to convert the string 'abc' to an integer using int('abc'), which raises a ValueError. The except block catches this exception and prints 'Error:' followed by the exception message, then the finally block always executes and prints 'Done'. Thus, the output is 'Error: invalid literal for int() with base 10: 'abc'' followed by 'Done'.

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 "Error:" then "Done" without the message

    Why it's wrong here

    The `finally` block always executes, so "Done" prints regardless, but the `except` clause only runs when an exception is raised inside the `try`. Here no exception occurs, so "Error:" never prints. This structure is correct when the `try` block genuinely risks raising an exception that must be caught and handled.

  • ✗

    Raises an unhandled exception

    Why it's wrong here

    Raising an unhandled exception would occur only if the code dereferenced a missing key or index, or divided by zero. The exhibit's operations complete without triggering such a fault, so execution proceeds normally. This option tempts candidates who expect error handling around dictionary lookups, which would be the correct concern in that scenario.

  • ✓

    Prints "Error: invalid literal for int() with base 10: 'abc'" then "Done"

    Why this is correct

    The int() call raises a ValueError because 'abc' cannot be parsed as base-10 integer, so execution jumps straight to the except block, printing the error message. The finally block then runs regardless, printing "Done". This satisfies the stem's requirement that both the error text and "Done" appear in that order.

  • ✗

    Prints only "Done"

    Why it's wrong here

    The loop's `else` clause runs only when the `for` completes without hitting `break`; here the `break` fires, so "Done" is skipped and only the loop body's output appears. It tempts because `else` on `for` genuinely prints after normal exhaustion, which would be correct had no `break` executed.

About these practice questions

This PCEP question is part of Courseiva's 482-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

Same concept, more angles

2 more ways this is tested on PCEP

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. Refer to the exhibit. What is the output?

medium
  • ✓ A.Error\nEnd
  • B.End
  • C.Error\nOk\nEnd
  • D.Ok\nEnd

Why A: The code attempts to print 'Ok' but raises a TypeError because you cannot concatenate a string and an integer with the + operator. The exception is caught by the bare except clause, which prints 'Error', and then the finally block always executes, printing 'End'. Thus the output is 'Error' followed by 'End' on separate lines.

Variation 2. Refer to the exhibit. What is the output of the code?

hard
  • A.15 15
  • B.13 15
  • ✓ C.15 13
  • D.8 8

Why C: The code defines a function `func` that attempts to modify a tuple element (`t[1] = 10`). Since tuples are immutable, this raises a `TypeError`. The `except` block catches the error and prints `x` (15) followed by `t[1]` (13), resulting in the output '15 13'. Therefore, the correct answer is C.

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This PCEP 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 PCEP exam.