Menu
Topics Index
...
`

Backspace


If you need explanation Read this topic

If you need Answer Take test on this topic

Backspace
What will be the output of the following program?
public class Backspace
{
    public static void main(String ats[])
    {
        int value = (int) Math.random();
        System.out.println("Value: " + value);
    }
}

A. Value: 0
B. Value: 1
C. Some other output
D. Compilation Error
E. Runtime Error
Topic: Java Collections - Utility Classes In Java

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 we have the random value concept...we can generate random numbers using the random() in Math class..random() function returns the double value between the 0.0 and 1.0..

so here we are calling the random() and this method will return the double..so here every time we will get the value between the 0.0 to 1.0 and we are converting it into int so it always become 0 only....

Posted by Uday Kumar    2014-12-29 09:06:45


a....the classMathcontains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions..........public static double random()Returns adoublevalue with a positive sign, greater than or equal to0.0and less than1.0. Returned values are chosen pseudorandomly with (approximately) uniform distribution from that range.When this method is first called, it creates a single new pseudorandom-number generator, exactly as if by the expressionhere using random() it will give value0.0 and now then....niw here explicit typ con version occurs... double value is cast to int... hence result is 0

Posted by Asma Mujtaba Khan    2014-12-29 09:13:13


A=0

Posted by Vikash Atri    2014-12-29 15:05:13


Answer is A... random() will return a double value  greater than or equal to 0.0 and less than 1.0. ...next on explicit typecasting to int  will drop the values next to dot and is prnted o always

Posted by Shashanka Mogaliraju    2014-12-29 19:08:19


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Asma Mujtaba Khan,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Asma Mujtaba Khan. 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    2014-12-30 04:34:38


© meritcampus 2019

All Rights Reserved.

Open In App