|
Page 2 of 2
* Answer for question no.
1: Choice C is the correct
answer.
The Gen class is a generic class, T is the name of a type parameter to
which we can assign any type. Here, we pass Integer as the type
argument value for T. Gen i1=new Gen(new
Integer(10));
The return type of the get() method is Integer, but we can receive it
in an int variable because Integer unboxes into int. So the value
received in i is 10. Thus, the output is 10. Hence, choice D is
incorrect. There are no compiler errors or exceptions generated here,
so choices A and B are incorrect.
For more information - Generics Tutorial
* Answer for question no.2:
Choice C is the correct answer.
The code snippet given in choice C is the most appropriate
implementation of the hashCode() method that can be inserted at line
15. The variable "area" is an insignificant variable here; its value
can be computed from the other two variables - length and width. Also,
the fact that "area" is a final variable indicates that its value
cannot be changed later. Hence, it should not be included in the
computation of the hash code value for the objects of this class. Thus,
choices A and B are inappropriate.
The code snippet in choice D returns "long" instead of "int", hence it
will not even compile. Obviously, choice D is incorrect. Only code
snippet in choice C uses both significant variables and excludes
"area", which can be computed from the other two variables. Hence,
choice C is the most appropriate implementation. Note that the question
asks for the "most appropriate" implementation, and not merely a legal
implementation.
While implementing the equals() and hashCode() methods in your class,
you should only consider significant variables from that class in these
methods. The variable, which can be computed from the other variables
of the class, should not be included in the implementation of these
methods.
Besides API documentation, you can read more about the equals and
hashCode() methods of Object class in Joshua Bloch's book Effective Java: Chapter 3
Related
Test
your Java language skills - Java Quiz 1
Test
your Java language skills - Java
Quiz Two
Get
your JSP / Servlet skills certified
A
guide to the Sun Certified Java Programmer exam for J2SE 5.0
|
Comment by Niklas on 2007-02-01 04:49:27 As the Gen class in question 1 is written it is not using generics. It will give a compilation error since the class "T" is unknown. You have to specify a formal type parameter like this: class Gen { .. } Then when using the class you have to provide this type paremeter Gen i1 = new Gen(new Integer(10)); Then it will print out the result "10". :-) | Comment by Niklas on 2007-02-01 04:52:35 The code in my comment looks just like the original post.. all my angle brackets has been eaten by HTML. And I guess that is what has happened in the original post as well. :-) It should look like this: class Gen<T> { .. } Gen<Integer> i1 = new Gen<Integer>(new Integer(10)); | Comment by Sanjeev on 2008-08-02 04:34:25 This is the right approach. |
|