Andhra University 2007 B.E Computer Science s - Question Paper
Wednesday, 01 May 2013 05:50Web
Page 2 of 3
What is the fundamental property that distinguishes Sets from other kinds of Collections?
Answer:
A Set cannot contain duplicate elements. Adding an item to the set has no effect if that item is already in the set. (Note that exactly what it means to say that 2 items are the identical depends on the kind of set. For HashSet, 2 items are tested for equality using the equals() method. For a TreeSet, the test for equality uses the compareTo() method.)
ques. 7:
What is the essential difference in functionality ranging from a TreeMap and a HashMap?
Answer:
The key/value pairs in a TreeMap are sorted so that the keys are in ascending order. (For this reason, it must be possible to compare the keys in a TreeMap, using a compareTo() method. Either the keys must implement the Comparable interface or a Comparator must be given to do the comparison.)
ques. 8:
discuss what is meant by a hash code.
Answer:
The hash code of an object is an integer that tells where that object should be stored in a hash table. A hash table is an array of linked lists. When an object is stored in a hash table, it is added to 1 of these linked lists. The object's hash code is the index of the position in the array where the object is stored. All objects with the identical hash code go into the identical linked list. In Java, every object obj has a method obj.hashCode() that is used to calculate hash codes for the object. If the object is to be stored in a hash table of size N, then the hash code that is used for the object is Math.abs(obj.hashCode())%N.
ques. 9:
replace the subsequent Date class so that it implements the interface Comparable
class Date {
int month; // Month number in range one to 12.
int day; // Day number in range one to 31.
int year; // Year number.
Date(int m, int d, int y) { // Convenience constructor.
month = m;
day = d;
year = y;
}
}
Answer:
The interface Comparable
class Date implements Comparable
Earning: Approval pending. |