Exercise :: Java.lang Class - Pointing out the correct statements
- Java.lang Class - General Questions
- Java.lang Class - Finding the output
- Java.lang Class - Pointing out the correct statements
1. | Which statement is true given the following? Double d = Math.random(); |
|||||||
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 |
2. | Which two statements are true about wrapper or String classes?
|
|||||||
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. |
3. | Which of the following will produce an answer that is closest in value to a double, d, while not being greater than 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). |
4. | What two statements are true about the result obtained from calling Math.random()?
|
|||||||
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. |