Test your Java language skills – Java Quiz 3

A Java quiz that tests your knowledge of Generics, equals and the
hashCode
method. If you are looking for a user contributed database of Java
questions, check out the Java
Interview Questions Bank
or check out Java
Quiz One and Java
Quiz Two .

These questions are courtesy of Whizlabs
Software
and will help you prepare for the Sun Certified Java
Programmer Exam 5.0 (SCJP).

Question No. 1 –
What is the result of compiling and running the following code?


import java.util.*;

class Gen {
        T obj;
        Gen(T o){
                obj=o;
        }

        T get() {
                return obj;
        }
}

class Generics19{
        public static void main(String[] args){
                Gen i1=new Gen(new Integer(10));
                int i=i1.get();
                System.out.println(i);
        }
}

A.Compiler error
B.Exception
C.Prints 10
D.Prints 0

Question No. 2 – Select the most
appropriate implementation of the hashCode() method
that can be inserted at line 15 in the following code. Assume that the
equals() method is implemented correctly in this class.

1.      public class TestHash
2. {
3. private int length;
4. private int width;
5. private final double area;
6.
7. // only one constructor
8. public TestHash(int l, int w)
9. {
10. length = l;
11. width = w;
12. area = length * width;
13. }
14.
15. // what should be inserted here?
16.
17. // equals method not shown
18. }
A. public int hashCode()
{
int hash = 7;
hash = 31 * hash + length;
hash = 31 * hash + width;
long bits = Double.doubleToLongBits(area);
return hash;
}

B. public int hashCode()
{
int hash = 7;
hash = 31 * hash + length;
hash = 31 * hash + width;
long bits = Double.doubleToLongBits(area);
hash = 31 * hash + (int)(bits (bits >>> 32));
return hash;
}

C. public int hashCode()
{
int hash = 7;
hash = 31 * hash + length;
hash = 31 * hash + width;
return hash;
}

D. public long hashCode()
{
int hash = 7;
hash = 31 * hash + length;
return hash;
}

Answers On Page 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

The following two tabs change content below.
Content Team

Content Team

The IndicThreads Content Team posts news about the latest and greatest in software development as well as content from IndicThreads' conferences and events. Track us social media @IndicThreads. Stay tuned!
Content Team

Content Team

The IndicThreads Content Team posts news about the latest and greatest in software development as well as content from IndicThreads' conferences and events. Track us social media @IndicThreads. Stay tuned!

  • Asdasdaasda

    Question 1 answer is wrong……It does not compile. The “T” class doesn't exist. You cannot create an object of type “T” named obj , because the type T does not exist….

  • GUEST

    When we compile Attacker.java, the Sufferer is compiled.

  • Niklas

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

  • Niklas

    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’. 🙂