Courseiva
Computer Programming and Python FundamentalshardMultiple ChoiceObjective-mapped

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 exception object e is printed, which includes the message.

  • Raises an unhandled exception

    Why it's wrong here

    ValueError is caught by the except block.

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

    Why this is correct

    The error message is printed, then finally runs.

  • Prints only "Done"

    Why it's wrong here

    Error is raised and caught, so error message is printed.

About these practice questions

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