Exercise: Objects And Collections

Questions for: Objects And Collections

Which of the following statements about the hashcode() method are incorrect?
  1. The value returned by hashcode() is used in some collection classes to help locate objects.
  2. The hashcode() method is required to return a positive int value.
  3. The hashcode() method in the String class is the one inherited from Object.
  4. Two new empty String objects will produce identical hashcodes.
A:
1 and 2
B:
2 and 3
C:
3 and 4
D:
1 and 4
Answer: B

(2) is an incorrect statement because there is no such requirement.

(3) is an incorrect statement and therefore a correct answer because the hashcode for a string is computed from the characters in the string.

Which statement is true for the class java.util.HashSet?
A:
The elements in the collection are ordered.
B:
The collection is guaranteed to be immutable.
C:
The elements in the collection are guaranteed to be unique.
D:
The elements in the collection are accessed using a unique key.
Answer: C

Option C is correct. HashSet implements the Set interface and the Set interface specifies collection that contains no duplicate elements.

Option A is wrong. HashSet makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time.

Option B is wrong. The set can be modified.

Option D is wrong. This is a Set and not a Map.

class Test1 
{
    public int value;
    public int hashCode() { return 42; }
}
class Test2 
{
    public int value;
    public int hashcode() { return (int)(value^5); }
}
which statement is true?
A:
class Test1 will not compile.
B:
The Test1 hashCode() method is more efficient than the Test2 hashCode() method.
C:
The Test1 hashCode() method is less efficient than the Test2 hashCode() method.
D:
class Test2 will not compile.
Answer: C

The so-called "hashing algorithm" implemented by class Test1 will always return the same value, 42, which is legal but which will place all of the hash table entries into a single bucket, the most inefficient setup possible.

Option A and D are incorrect because these classes are legal.

Option B is incorrect based on the logic described above.

What will be the output of the program?
public static void main(String[] args) 
{
    Object obj = new Object() 
    {
        public int hashCode() 
        {
            return 42;
        }
    }; 
    System.out.println(obj.hashCode()); 
}
A:
42
B:
Runtime Exception
C:
Compile Error at line 2
D:
Compile Error at line 5
Answer: A

This code is an example of an anonymous inner class. They can be declared to extend another class or implement a single interface. Since they have no name you can not use the "new" keyword on them.

In this case the annoynous class is extending the Object class. Within the {} you place the methods you want for that class. After this class has been declared its methods can be used by that object in the usual way e.g. objectname.annoymousClassMethod()

What will be the output of the program?
TreeSet map = new TreeSet();
map.add("one");
map.add("two");
map.add("three");
map.add("four");
map.add("one");
Iterator it = map.iterator();
while (it.hasNext() ) 
{
    System.out.print( it.next() + " " );
}
A:
one two three four
B:
four three two one
C:
four one three two
D:
one two three four one
Answer: C

TreeSet assures no duplicate entries; also, when it is accessed it will return elements in natural order, which typically means alphabetical.

Ad Slot (Above Pagination)
Quiz