Java.lang Class - Pointing out the correct statements


What two statements are true about the result obtained from calling Math.random()?

1The result is less than 0.0.

2.The result is greater than or equal to 0.0..

3.The result is less than 1.0.

4.The result is greater than 1.0.

5.The result is greater than or equal to 1.0.

Answer: B

(2) and (3) are correct. The result range for random() is 0.0 to < 1.01.0 is not in range.

Enter details here


Which of the following will produce an answer that is closest in value to a double, d, while not being greater than d?

Answer: D

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).

Enter details here


Which two statements are true about wrapper or String classes?

1.If x and y refer to instances of different wrapper classes, then the fragment x.equals(y) will cause a compiler failure.

2.If x and y refer to instances of different wrapper classes, then x == y can sometimes be true.

3.If x and y are String references and if x.equals(y) is true, then x == y is true.

4.If xy, 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.

5.If x and y are String references and x == y is true, then y.equals(x) will be true.


Answer: D

Statement (4) describes an example of the equals() method behaving transitively. By the way, xy,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.

Enter details here


Which statement is true given the following?

Double d = Math.random();

Answer: B

The Math.random() method returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0

Enter details here