Menu
Topics Index
...
`

Print formated statment And Math


If you need explanation Read this topic

If you need Answer Take test on this topic

Print formated statment And Math
What will be the output of the following program?
public class MathsIsFun {
    public static void main(String[] args) {
        float x = (float) Math.cos(45);
        float y = (float) Math.sin(45);
        double result = (x * x) + (y * y);
        System.out.format("%.3f, ", x);
        System.out.format("%.3f, ", y);
        System.out.format("%.3f, ", x * x);
        System.out.format("%.3f, ", y * y);
        System.out.print(result);
    }
}

A. 0.525, 0.851, 0.276, 0.724, 1.0
B. 0.707, 0.707, 0.500, 0.500, 0.99994
C. 0.707, 0.707, 0.500, 0.500, 1.0
D. 0.154, -0.988, 0.024, 0.976, 1.0
E. Compilation Error or Runtime Error
Topic: Java Formatting Numbers

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.... as the first parameter, format, is a format string specifying how the objects in the second parameter, args, are to be formatted and the value of cos 45 is 0.525 and sin 45 is 0.851

Posted by Sai Abhishek    2014-03-04 12:55:09


Ans is A since Math.cos(45) degree value is 0.525 and sin(45) is 0.276  x*x muliply value 0.276 similarly y*y gives .724 since format specifier is %3f i.e 3digits after decimal and value sin45 *cos45 gives 1 so answer is A

Posted by Bharath Yelchuri    2014-03-04 12:59:26


Ans A:Math.cos(45) degree value is 0.525 and sin(45) is 0.276....here formate is Three places after decimal point so (%.3f)....finaly result z is 1.0 ...cos(45) and sin(45) value is 1...

Posted by Ashok Reddi    2014-03-04 13:34:29


ans:A..Math.cos(45) value is stored in x and it will be printed in the format of 3 digits after decimal..so it is like 0.525 ******and then sin(45) value is stored in y and then printed in th format of 3 digits after decimal point.so it is like 0.851.and then it will be like x*X muliply value 0.276...similarlly format next o.724 for the value of y*y.after at last the value is stored   in result that is 1.because we know  ...cos squre(45) and sin squre(45) value is 1..

Posted by Sapparapu Pradeep Kumar    2014-03-04 17:47:45


Congratulations Sai Abhishek Gogu. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-03-05 06:17:08


© meritcampus 2019

All Rights Reserved.

Open In App