Topics Index

Pythagoras Is Correct

If you need explanation Read this topic

If you need Answer Take test on this topic

Pythagoras Is Correct
What will be the output of the following program?
public class Pythagoras {

    public static void main(String[] args) {
        double sinTheta = Math.sin(30);
        double cosTheta = Math.cos(30);
        double tanTheta = Math.tan(30);
        double sum = sinTheta * sinTheta + cosTheta * cosTheta;
        System.out.print(sum + " " + isEqual(tanTheta, sinTheta / cosTheta) + " ");
        System.out.print(isEqual(1 / 2, sinTheta) + " ");
        System.out.print(isEqual(Math.sqrt(3) / 2, cosTheta) + " ");
        System.out.print(isEqual(1 / Math.sqrt(3), tanTheta) + " ");

    public static boolean isEqual(double one, double two) {
        return (Math.abs(one - two) < 0.0001);

A. 1.0 true false true true 
B. 1.0 true false false false 
C. 1.0 true true true true 
D. 1.0 false false false false 
E. Some other output or Compilation error or Runtime error
Topic: Java Math Class - java.lang.Math

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.

b is ans here sym value comes as 1 and thern si/cos val is equal to tan value so true. and in isequal methods dats type conflicts comes so false appears

Posted by Karteek Paruchuri    2013-09-24 09:10:38

B is the except first SOP all the Sop returns false value..only the first SOp gives 1.0 true. remaining all returns false.

Posted by Shabaaz Shaikz    2013-09-24 09:15:03

answer:B as values differ by decimal precisions in case of remaining print statements

Posted by Krishna Chaitanya    2013-09-24 09:17:28

answer c is the correct answer as we get 1.0 true and after that also while comparing the value are less than 0.0001 so it return true

Posted by Ram Reddy Bitla    2013-09-25 01:13:58

Although the correct answer is B, the explanations given above are not correct. The  parameter for sin, cos and tan is in Radians and not in degrees. 30 radians is not same as 30 degrees and hence the values differ a lot, printing false.

Posted by Merit Campus    2013-09-25 07:32:27

No Winner for this dose.

Posted by Merit Campus    2013-09-25 07:32:47

© meritcampus 2019

All Rights Reserved.

Open In App