Difficulty: Easy
Correct Answer: NaN
Explanation:
Introduction / Context:
Java’s Math library follows IEEE 754 floating-point rules. The square root of a negative real number is not defined in the reals, so Math.sqrt returns the special floating-point value NaN (Not-a-Number) rather than throwing an exception.
Given Data / Assumptions:
Math.sqrt(double).
Concept / Approach:
For negative inputs, many Math functions are defined to return NaN. Java does not use exceptions for these domain errors in the Math class; instead, it propagates NaN, which prints as the string "NaN".
Step-by-Step Solution:
Verification / Alternative check:
Double.isNaN(Math.sqrt(-4)) returns true. To compute square roots of negative numbers as complex values, use a complex number library instead of Math.sqrt.
Why Other Options Are Wrong:
Common Pitfalls:
Expecting exceptions from Math for domain errors; in Java, NaN is the standard signal value.
Final Answer:
NaN
Discussion & Comments