Java Programming - Java.lang Class - Discussion

Discussion Forum : Java.lang Class - Finding the output (Q.No. 17)
17.

What will be the output of the program?

System.out.println(Math.sqrt(-4D));

-2
NaN
Compile Error
Runtime Exception
Answer: Option
Explanation:

It is not possible in regular mathematics to get a value for the square-root of a negative number therefore a NaN will be returned because the code is valid.

Discussion:
5 comments Page 1 of 1.

JBxquu said:   1 decade ago
NaN stands for "Not a Number". Math.sqrt() function can only handle positive numbers, and square root of negative numbers is not defined (its a complex number and Java doesn't provide much support for complex numbers) , so it prints NaN.
(1)

Ranjitha said:   8 years ago
Thank you @Jbxquu.

Pramit said:   7 years ago
What is D here?

Mani said:   1 decade ago
How NaN comes?

Sudharsan said:   1 decade ago
What is NAN?

Post your comments here:

Your comments will be displayed after verification.