Tuesday, April 29, 2008

Interview Questions

1. What is the contract of hashCode in Java and why should we follow it ? Here I am waiting for a story about general contract of hashCode, for example as it is described in javadocs ..... hashCode method must consistently return the same ........ The violation of this contract could have an effect on correct work of HashMap.
2. What kind of collection should I choice to implement predictable iteration order. Here I am waiting for a story about LinkedHashMap, LinkedHashSet and oyher coolections where iteration has a predictable order.
3. What interfaces are implemented by an Object of Array type.
(The answer from java lang specification (10.7 Array Members) - "Every array implements the interfaces Cloneable and java.io.Serializable")
4. What will be printed in console
public static void main(String[] args) {
String s1 = "string";
String s2 = "string";
System.out.println(s1.toString().equals(s2.toStrin g()));
System.out.println(s1.toString() == s2.toString());
}
(since toString returns the same string (this) and SUN JRE caches strings in both cases we should see "true")

No comments: