Menu
Topics Index
...
`

Boolean Values2994


If you need explanation Read this topic

If you need Answer Take test on this topic

Boolean Values2994
What will be the output of the following program?
public class UsingNaN {
    public static void main(String args[]) {
        Float flo = (float) Math.sqrt(20);
        System.out.println(flo);
        boolean b = isNaN(flo);
        System.out.print(b);
    }
    static public boolean isNaN(float k) {
        return (k == k);
    }
}

A. 4.472136
false
B. 4.472136
true
C. Compilation Error
D. 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.

Answer is B
    4.472136
    true

here in the program it contains one main() method and isNan(float) method which takes an float argument and returns boolean value to the calling part

in the first statement         Float f=(float)Math.sqrt(20);

here the math function it is used to find out the  square root of a particular value and it is taken in type conversion using (float) before assigning it to the flaot variable "flo"
where it can take decimal part also because     Math.sqrt(20); return double value where it cannot be assigned to float variable unless we exclusively type cast it.son to complete the execution of the statement we type cast it with (float)before assining

and printing it by     System.out.println(f);           // it prints 4.472136

and then after we are using a user defined method called as isNan() method which expects a float value as argument and return boolean value to the calling part

    isNan(f)               //here the value sent is taken in float variable k
and the it compared using "==" equal to operator since the value is same and comapred with the same variable it return true to the boolean value true to the variable b

    boolean b=true;

and then it is printed as true;

Posted by Vicky Maduri    2015-02-19 04:52:06


ANS IS B

Here first square root of 20 is found by sqrt method of Math class,after it it typecasted to float and stored to flo variable.

After that isNaN() function is defined in which equality is returned by boolean value.

As float value flo is equal to itself. So, it returns true.

Output:
4.472136
True

Posted by Mânïshå Mùlchåndânï    2015-02-19 17:28:09


b

Posted by Shubham Bansal    2015-02-19 19:18:42


This dose is now closed and the winners are Vicky Maduri,  for 'First Correct Comment', Mânïshå Mùlchåndânï,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Gopinath Manchikanti. 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-02-20 04:55:08


© meritcampus 2019

All Rights Reserved.

Open In App