From Dan Chisholm's Mock exam
Question: Which of the boolean variables will never be initialized to the value true?
class Math7 {
public static void main (
String[] args) {
double d1 = Math.random();
boolean b1 = (d1 < 0.0), b2 = (d1 <= 0.0), b3 = (d1 == 0.0);
boolean b4 = (d1 >= 0.0), b5 = (d1 < 1.0), b6 = (d1 <= 1.0);
boolean b7 = (d1 == 1.0), b8 = (d1 >= 1.0), b9 = (d1 > 1.0);
//System.out.println("d1:"+d1);
System.out.println("b1:"+b1);
System.out.println("b2:"+b2);
System.out.println("b3:"+b3);
System.out.println("b4:"+b4);
System.out.println("b5:"+b5);
System.out.println("b6:"+b6);
System.out.println("b7:"+b7);
System.out.println("b8:"+b8);
System.out.println("b9:"+b9);
}
}
I executed the above code and got results as
b1:false
b2:false
b3:false
b4:true
b5:true
b6:true
b7:false
b8:false
b9:false
My Question
1. As per specification the Random value produced will be greater than or equal to 0.0 and less than 1.0.
(b6) But here less than or equal to 1.0 is also is evaluated to true. Why is it so?
(b3) Why equals to zero is producing a false result?
Someone please explain this.
2. In the answers column of Dan Chisholm's Mock exam the answers provided are(b1 b7 b8 b9). I think they are wrong.