Java Programming - Java.lang Class - Discussion


What will be the output of the program?


[A]. -2
[B]. NaN
[C]. Compile Error
[D]. Runtime Exception

Answer: Option B


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.

Mani said: (Jan 27, 2014)  
How NaN comes?

Sudharsan said: (May 26, 2014)  
What is NAN?

Jbxquu said: (Jun 7, 2014)  
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.

Ranjitha said: (Oct 27, 2017)  
Thank you @Jbxquu.

Pramit said: (Jun 14, 2018)  
What is D here?

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.