Menu
Topics Index
...
`

Boolean Values2995


If you need explanation Read this topic

If you need Answer Take test on this topic

Boolean Values2995
What will be the output of the following program?
public class Sample2 {
    public static void main(String args[]) {
        Double flo = Math.sqrt(25);
        System.out.println(flo);
        boolean b1 = isNaN(flo);
        System.out.println(b1);
        boolean b = Sample2.isNaN(flo);
        System.out.print(b);
    }
    static public boolean isNaN(double k) {
        return (k != k);
    }
}

A. 5.0
false
false
B. 5.0
false
true
C. 5.0
true
true
D. Compilation Error or Runtime Error
Topic: Java Double Class And Java Float Class

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Ans is A.
Here in the program we are calculating the square root of 25 and storing it into variable flo.. Here 'flo' is of type Double and Math.sqrt() will return value in the type double.. But as we have the concept of autoboxing there wil be no error in storing the double value into Double variable..

Then we are calling a static method isNaN() directly which returns false as (k!=k) is false and then we are calling the isNaN() method on its class name which works perfectly and returns false..

So the output will be
5.0
false
false

Posted by Gopinath Manchikanti    2015-03-05 14:41:14


Ans is A here we have the sqrt() which will return the double value as a return value, here we are calling the sqrt() which is static method in the Math class, here we are passing the 25 so here it will return 5.0 than we are printing so it will print 5.0, next we are calling the isNan() by passing the return value means 5.0 in that method we are checking k!=k so here it will always return false Bcz we are comparing the received value k!=k is always false so it will return false,next we are calling isNan() with the class name, Bcz isNan() is static method so we can invoke it by using the class name or we can invoke directly from a static method

Posted by Uday Kumar    2015-03-05 14:43:53


ANS IS A

Here,the float case is used.

--Here,float class is used to find square root of 25 is found. 5.0 will be printed.

--After that isNaN() method is used which returns true when it has the arguement which is not number.

As 5.0 is a number so it returns false.

--After that static isNaN() method I used with the variable flo directed which too returns false.

OUTPUT:
5.0
False
False

Posted by Mânïshå Mùlchåndânï    2015-03-05 16:03:08


This dose is now closed and the winners are Gopinath Manchikanti,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Mânïshå Mùlchåndânï for the 'Popular Comment'. The 'lucky liker' is Shubham Bansal. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-03-06 02:18:10


© meritcampus 2019

All Rights Reserved.

Open In App