Courseiva
Question 191 of 498
Computer Programming and Python FundamentalseasyMultiple ChoiceObjective-mapped

PCEP Computer Programming and Python Fundamentals Practice Question

You are a junior developer at a small startup. Your team has a Python script that automates daily data processing. The script reads a CSV file, processes each row, and writes results to a new file. Recently, the script started crashing with a 'ValueError: invalid literal for int()' error. The error occurs on a line that converts a field to an integer using int() on a string value. The CSV file comes from an external source that sometimes contains non-numeric values like 'N/A' or empty strings. Which course of action is best to handle this robustly without stopping the entire process?

⚠ Common exam trap

The PCEP exam often tests the misconception that logging or pre-processing (like regex) is sufficient to prevent runtime errors, when in fact only exception handling can gracefully recover from an exception that has already been raised.

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

Wrap the conversion in a try-except block and handle the exception appropriately for each row.

Wrapping the conversion in a try-except block allows the script to catch the ValueError for each row individually, log or handle the problematic row (e.g., skip it or use a default value), and continue processing the remaining rows without crashing. This is the standard Pythonic approach for handling expected but unpredictable data quality issues in external input, as it separates error handling from the main logic and preserves the robustness of the batch process.

Answer analysis

Option-by-option breakdown

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

  • Wrap the conversion in a try-except block and handle the exception appropriately for each row.

    Why this is correct

    Exception handling allows the script to continue processing other rows.

  • Add logging before the conversion to print the problematic value.

    Why it's wrong here

    Logging doesn't prevent the exception from crashing the script.

  • Use a regex to replace all non-digit characters before conversion.

    Why it's wrong here

    Replacing indiscriminately may alter valid numeric strings like '-5'.

  • Contact the external source to ensure no missing values are sent.

    Why it's wrong here

    External sources are not always responsive; the script should be resilient.

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

Last reviewed: Jul 4, 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 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.