Java Programming - Java.lang Class
- Java.lang Class - General Questions
- Java.lang Class - Finding the output
- Java.lang Class - Pointing out the correct statements
Which statement is true given the following?
Double d = Math.random();
The Math.random() method returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0
- If x and y refer to instances of different wrapper classes, then the fragment x.equals(y) will cause a compiler failure.
- If x and y refer to instances of different wrapper classes, then x == y can sometimes be true.
- If x and y are String references and if x.equals(y) is true, then x == y is true.
- If x, y, and z refer to instances of wrapper classes and x.equals(y) is true, and y.equals(z) is true, then z.equals(x) will always be true.
- If x and y are String references and x == y is true, then y.equals(x) will be true.
Statement (4) describes an example of the equals() method behaving transitively. By the way, x, y,and z will all be the same type of wrapper. Statement (5) is true because x and y are referring to the same String object.
Statement (1) is incorrect—the fragment will compile. Statement (2) is incorrect because x == y means that the two reference variables are referring to the same object. Statement (3) will only be true if x and y refer to the same String. It is possible for x and y to refer to two different String objects with the same value.
The casting to an int is a smokescreen. Use a process of elimination to answer this question:
Option D is the correct answer, it is syntathecially correct and will consistently return a value less than d.
Option A and B are wrong because both the min() and max() methods require 2 arguments whereas here they are passed only one parameter.
Option C is wrong because it could return a value greater than d (if d was negative).
- The result is less than 0.0.
- The result is greater than or equal to 0.0..
- The result is less than 1.0.
- The result is greater than 1.0.
- The result is greater than or equal to 1.0.
(2) and (3) are correct. The result range for random() is 0.0 to < 1.0; 1.0 is not in range.