Java Interview Questions | Page 7


Question 61:

Does Hashtable allows null key or value?

In Java, Hashtable does not allow null key or value.

Question 62:

Can two objects which are not equal can have same hash code?

Yes, two object which are not equal by equals() method can still return same hash code.

HASH CODE

Question 63:

What is the difference between ArrayList and Vector?

  • Synchronization :- ArrayList is not thread safe whereas vector is thread safe.
  • Performance :- As vector is thread-safe the performance is slower than ArrayList.
  • Data Growth :- When the size exceeds in Vector, it defaults to doubling the size of it, whereas the ArrayList increases its size by 50%.
Question 64:

What is the difference between ArrayList and LinkedList?

  • ArrayList is an index-based data structure; searching element from ArrayList is pretty fast. ArrayList provides O(1) performance for get(index).
    On the otherhand LinkedList doesnot provide index based search.
  • Insertion are easy and fast in LinkedList as compared to ArrayList because there is no risk of resizing array and copying content to new array. Whereas ArrayList also needs to update its index except the end of array.
  • Removal is easy and fast in LinkedList as compared to ArrayList.
  • LinkedList has more memory overhead as compared to ArrayList.
Question 65:

Consider the following statements:-

final Map oMap = new HashMap();
    oMap.put("1",1);
    oMap.put("2",2);
    oMap.put("3",3);

Is it correct? How?

Yes. Because final marks to reference, not to the object. You can't make the reference point to a different hash map. But you can do anything to that object, including adding and removing things.


Question 66:

Can static methods be synchronized?

Yes. As static methods are class methods and have only one copy of static data for the class, only one lock for the entire class is required. Every class in java is represented by java.lang.Class instance. The lock on this instance is used to synchronize the static methods.

Question 67:

Can two threads call two different static synchronized methods of the same class?

 

No. The static synchronized methods of the same class always block each other as only one lock per class exists. So no two static synchronized methods can execute at the same time.

Question 68:

Why Runtime exceptions were categorized as unchecked exceptions?

Runtime exceptions represent problems that are detected by the runtime system.
This includes arithmetic exceptions, pointer exception and indexing exceptions.
Runtime exceptions can occur anywhere in the program and in a typical program can be very numerous.
Having to add runtime exceptions in every method would reduce a program's clarity.
Thus the compiler does not require that you catch or specify runtime exception.

NOTE:- If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client can't do anything to recover from the exception, make it an unchecked exception.

Question 69:

If you are implementing equals() method, which other method you need to override?

HashCode method. As per Java specification overriding one and not other will break the contract between them.

Refer http://modernpathshala.com/Article/2/hashcode-and-equals-method-in-java to understand relationship between hashcode and equals method in detail.

Question 70:

What is the contract between hashCode and equals method?

  • Whenever hashCode is invoked on the same object more than once during the execution of java application, the hashCode method must consistently return the same integer, provided no information used in equals comparison on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals() method thn calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal acording to equals method, then calling the hashcode method on each of the two objects must produce distinct integer result.
Share interview questions

Comments

Comments
comments powered by Disqus

Navigation

Social Media