Question 101 of 510
Computer Programming and Python FundamentalsmediumMultiple ChoiceObjective-mapped

Quick Answer

The correct answer is the try-except block that catches ImportError and calls sys.exit(1) after printing a custom message. This works because Python’s import statement raises an ImportError when a module is missing, and wrapping it in a try-except allows you to intercept that exception and respond with a controlled exit rather than an unhandled crash. On the PCEP exam, this tests your understanding of exception handling fundamentals—specifically that you can catch built-in exceptions like ImportError to make scripts robust. A common trap is forgetting to import sys before using sys.exit, or attempting to catch a generic Exception instead of the specific ImportError, which would still work but is less precise. Remember the mnemonic: “Try the import, catch the error, exit with honor.”

PCEP Computer Programming and Python Fundamentals Practice Question

This PCEP practice question tests your understanding of computer programming and python fundamentals. The scenario asks you to isolate a root cause — eliminate options that address a different problem before choosing. 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.

A system administrator is writing a Python script to monitor disk usage. The script uses the psutil library (not part of PCEP scope, but the scenario is generic). The administrator writes:

import psutil

disk = psutil.disk_usage('/')

print(disk.free)

But the script fails with an ImportError because psutil is not installed. The administrator decides to handle this gracefully: if the module is missing, the script should print a custom error message and exit without crashing. Which code snippet achieves this?

Question 1mediummultiple choice
Study the full Python automation breakdown →

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

try: import psutil except ImportError: print('psutil not installed. Please install.') sys.exit(1)

Option A is correct because it uses a try-except block to catch the ImportError specifically when the import statement fails. This allows the script to print a custom error message and then call sys.exit(1) to terminate gracefully with a non-zero exit code, which is the standard way to signal failure in a script. The other options either do not handle the missing module correctly or fail to exit the script properly.

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.

  • try: import psutil except ImportError: print('psutil not installed. Please install.') sys.exit(1)

    Why this is correct

    Catches ImportError and exits gracefully.

    Related concept

    Read the scenario before looking for a memorised answer.

  • import psutil if not psutil: print('psutil not installed.') sys.exit(1)

    Why it's wrong here

    ImportError occurs before the if statement.

  • try: import psutil except ImportError: print('psutil not installed. Please install.')

    Why it's wrong here

    Does not exit; subsequent code will crash on NameError.

  • try: import psutil except: print('Module missing.') raise

    Why it's wrong here

    Re-raises the exception, causing crash anyway.

Common exam traps

Common exam trap: answer the scenario, not the keyword

Python Institute often tests the distinction between handling an exception with a graceful exit versus merely printing a message and continuing, or re-raising the exception, which still causes a crash; candidates may overlook the need for sys.exit(1) or mistakenly think a bare except or a falsy check on the module name is sufficient.

Detailed technical explanation

How to think about this question

The import statement in Python is executed at runtime, and if the module is not found, Python raises an ImportError. Using a try-except block around the import is a common pattern for optional dependencies, allowing the script to degrade gracefully. The sys.exit(1) call raises a SystemExit exception, which can be caught by an outer try block but typically terminates the interpreter with the given exit code; exit code 0 means success, and any non-zero value indicates an error, which is useful for scripting and automation tools that check return codes.

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 small business has 20 workstations on the 192.168.1.0/24 network and one public IP from its ISP. The router uses PAT (NAT overload) so all 20 devices share one public address using different source ports. NAT questions test whether you understand the four address terms and which direction each translation applies.

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 PCEP 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 PCEP 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 PCEP question test?

Computer Programming and Python Fundamentals — This question tests Computer Programming and Python Fundamentals — Read the scenario before looking for a memorised answer..

What is the correct answer to this question?

The correct answer is: try: import psutil except ImportError: print('psutil not installed. Please install.') sys.exit(1) — Option A is correct because it uses a try-except block to catch the ImportError specifically when the import statement fails. This allows the script to print a custom error message and then call sys.exit(1) to terminate gracefully with a non-zero exit code, which is the standard way to signal failure in a script. The other options either do not handle the missing module correctly or fail to exit the script properly.

What should I do if I get this PCEP 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

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