Find Correct Statements

Question 1 :

Which statement is true given the following?

Double d = Math.random();


A). 0.0 < d <= 1.0
B). 0.0 <= d < 1.0
C). Compilation fail
D). Cannot say.
Answer : Option B

Explanation :

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


Question 2 :

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 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.
5. If x and y are String references and x == y is true, then y.equals(x) will be true.


A). 1 and 2
B). 2 and 3
C). 3 and 4
D). 4 and 5
Answer : Option D

Explanation :

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.


Question 3 :

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


A). (int)Math.min(d);
B). (int)Math.max(d);
C). (int)Math.abs(d);
D). (int)Math.floor(d);
Answer : Option D

Explanation :

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


Question 4 :

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

1. The 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.


A). 1 and 2
B). 2 and 3
C). 3 and 4
D). 4 and 5
Answer : Option B

Explanation :

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