Java Programming - Java.lang Class - Discussion

14. 

What will be the output of the program?

public class SqrtExample 
{
    public static void main(String [] args) 
    {
        double value = -9.0;
        System.out.println( Math.sqrt(value));
    }
}

[A]. 3.0
[B]. -3.0
[C]. NaN
[D]. Compilation fails.

Answer: Option C

Explanation:

The sqrt() method returns NaN (not a number) when it's argument is less than zero.


Saad said: (Feb 4, 2013)  
"NaN" stands for "not a number". "Nan" is produced if a floating point operation has some input parameters that cause the operation to produce some undefined result. For example, 0.0 divided by 0.0 is arithmetically undefined. Taking the square root of a negative number is also undefined.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.