Machine-translated by Google. Questions are authored in English.
Top

Java.lang Class - Pointing out the correct statements Questions and Answers

1.

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.

 

Answer: D

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.

Please log in or register to report this question.

2.

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

Please log in or register to report this question.

3.

Which statement is true given the following?

Double d = Math.random();

Answer: B

Correct answer: 0.0 <= d < 1.0

Math.random() returns a double in the half-open interval from zero inclusive to one exclusive, and the assignment to a Double compiles because autoboxing converts the primitive to its wrapper. Zero is therefore a possible value and one is not - which matters whenever the result is scaled, since (int)(Math.random()*6)+1 gives 1 to 6 precisely because the upper bound is never reached.

Please log in or register to report this 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.

Answer: B

Correct answer: 2 and 3

Statements 2 and 3 state the two halves of the range: the result is greater than or equal to 0.0 and strictly less than 1.0. Statements 1, 4 and 5 all put it outside that interval. The asymmetry - inclusive at the bottom, exclusive at the top - is deliberate and is what makes the value safe to multiply by a range and truncate.

Please log in or register to report this question.

Loading…
Tags: Java.lang Class - Pointing out the correct statements Questions and Answers || Java.lang Class - Pointing out the correct statements MCQ Questions and Answers || Java.lang Class - Pointing out the correct statements GK Questions and Answers || Java.lang Class - Pointing out the correct statements GK MCQ Questions || Java.lang Class - Pointing out the correct statements Multiple Choice Questions and Answers || Java.lang Class - Pointing out the correct statements GK || GK on Java.lang Class - Pointing out the correct statements || Java Programming Questions and Answers || Java Programming MCQ Questions and Answers || Java Programming GK Questions and Answers || GK on Java Programming