Courseiva

PCEP Computer Programming and Python Fundamentals Practice Question

A script uses 'import math' then calls 'math.sqrt(-1)'. What is the outcome?

⚠ Common exam trap

The trap here is that candidates mistakenly think `math.sqrt()` can handle negative numbers by returning a complex number or NaN, confusing it with `cmath.sqrt()` or the behavior of some other languages' math libraries.

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

✓

ValueError

`math.sqrt()` in Python's math module does not support negative arguments; it raises a `ValueError` when given a negative number, as the function is designed for real numbers only. The error message is 'math domain error', indicating the input is outside the domain of the mathematical function.

Answer analysis

Option-by-option breakdown

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

  • ✓

    ValueError

    Why this is correct

    math.sqrt() raises ValueError for negative arguments because it cannot return a real square root; it does not return a complex number or NaN. The constraint in the stem is the negative input -1, which triggers this specific exception rather than TypeError or ZeroDivisionError.

  • ✗

    NaN

    Why it's wrong here

    math.sqrt raises a ValueError for negative arguments rather than returning NaN; NaN arises from operations like math.sqrt of a float NaN or 0.0/0.0. It is tempting because NaN is the IEEE 754 result for invalid floating-point operations, but Python's math module raises instead.

  • ✗

    A complex number

    Why it's wrong here

    math.sqrt returns a float and raises ValueError on negative input; complex results require cmath.sqrt. It is tempting because mathematically the square root of -1 is imaginary, but Python's math module deliberately rejects negative arguments instead of returning a complex type.

  • ✗

    AttributeError

    Why it's wrong here

    Raises ValueError, since math.sqrt cannot accept negative reals; AttributeError means the attribute does not exist on the module. It tempts because misspelling a function name, such as math.sqrtt, genuinely produces AttributeError, so that error type is plausible when the module itself is fine.

About these practice questions

One of 482 original PCEP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. 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 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.