info@meritcampus.com    +91-85006-22255
...
`

Rounding Function2380
What will be the output of the following program?
`public class Absolute{    public static void main(String[] args) {        int i = 12;        double l = -10.01;        double d = 95.530;        float f = 0.6201f;        System.out.print("i: " + Math.abs(i));        System.out.print(" s: " + Math.abs(l));        System.out.print(" d: " + Math.abs(d));        System.out.print(" f: " + Math.abs(f));    }}`

 A. i: 12 s: -10.01 d: 95.53 f: 0.62 B. i: 12 s: 10.01 d: 95.53 f: 0.6201 C. i: 12 s: 10 d: 95.53 f: 0.62 D. Compilation Error E. Runtime Error
Topic:

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

B

Posted by Valli Prince    2014-12-11 12:43:24

Ans is C ,,,there r for method for abs in Mth class,,  double abs(double d),, float abs(float f) ,,int abs(int i),, long abs(long lng)

Posted by Raju VarshnEy    2014-12-11 12:43:35

Ans is B

here we have the Absolute value Concept means if we give negative value than it will return the positive value..if we pass positive value than it will return the that value only...

here we create the local variables i,l,f,d..here l is assigned with the negative value as -10.01 and f is assigned with the 0.6201f...here we wrote f Bcz to tell the System treat this value as flaot...Every decimal value is by default double in java..if we did not write f than it raise an error..Bcz double is greate type than float
Coming to Execution:

here we Math.abs()--this method returns Positive values if we pass negative value..it will return positive if we pass the positive value..

Math.abs(i)-- here i Positive value so it will return that value only
Math.abs(l)--> here l is negative value so it will apply negation to that value and returns
Math.abs(d)--here d Positive value so it will return that value only
Math.abs(f)--here f Positive value so it will return that value only

Posted by Uday Kumar    2014-12-11 12:45:46

ans b

Math.abs() method gives the absolute value of the argument. The argument can be int, float, long, double, short, byte.

Use abs(double a), abs(float a), abs(int a), abs(long a) API methods of Math to get the absolute value of a double, a float, an int or a long number.

coming to this program here int ,double,float type variables initialized.

int i=12,double l=-10.01,double d=95.530,float f=0.6201f

Math.abs(i)-To find absolute value of int, use static int abs(int i) method.

It returns the same value if the agrument is non negative value, otherwise negation of the negative value.
sop prints 12.

Math.abs(l)-To find absolute value of double, use static double abs(double i) method.

It returns the same value if the agrument is non negative value, otherwise negation of the negative value.
sop prints 10.01.

Math.abs(d)-To find absolute value of double, use static double abs(double i) method.

sop prints 95.53

Math.abs(f)-To find absolute value of float, use static float abs(float i) method.

It returns the same value if the agrument is non negative value, otherwise negation of the negative value.
sop prints 0.6201.

Posted by Maheshwari Natarajan    2014-12-11 16:29:50

This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Uday Kumar, Maheshwari Natarajan,  for 'Best Comment' and Uday Kumar 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    2014-12-14 01:39:24