Q. Difference between TreeMap and HashMap ?
Ans. They are different the way they are stored in memory. TreeMap stores the Keys in order whereas HashMap stores the key value pairs randomly.
Q. What is the difference between List, Set and Map?
Ans. List – Members are stored in sequence in memory and can be accessed through index.
Set – There is no relevance of sequence and index. Sets doesn’t contain duplicates whereas multiset can have duplicates. Map – Contains Key , Value pairs.
Q. Which interface does java.util.Hashtable implement?
Q. What is an Iterator?
Ans. Iterator is an interface that provides methods to iterate over any Collection.
Q. Which interface provides the capability to store objects using a key-value pair?
Q. What is the use of hashcode in Java?
Ans. Hashcode is used for bucketing in Hash implementations like HashMap, HashTable, HashSet etc. The value received from hashcode() is used as bucket number for storing elements. This bucket number is the address of the element inside the set/map. when you do contains() then it will take the hashcode of the element, then look for the bucket where hashcode points to and if more than 1 element is found in the same bucket (multiple objects can have the same hashcode) then it uses the equals() method to evaluate if object are equal, and then decide if contain() is true or false, or decide if element could be added in the set or not.
Q. Difference between Map and HashMap ?
Ans. Map is an interface where HashMap is the concrete class.
Q. What is a Property class ?
Ans. The properties class is a subclass of Hashtable that can be read from or written to a stream.
Q. what is the difference between collections class vs collections interface ?
Ans. Collections class is a utility class having static methods for doing operations on objects of classes which implement the Collection interface. For example, Collections has methods for finding the max element in a Collection.
Q. Will this code give error if i try to add two heterogeneous elements in the arraylist. ? and Why ?
List list1 = new ArrayList<>();
Ans. If we don’t declare the list to be of specific type, it treats it as list of objects.
int 1 is auto boxed to Integer and “1” is String and hence both are objects.
Q. What is the difference between comparable and comparator in java.util pkg?
Ans. Comparable interface is used for single sequence sorting i.e.sorting the objects based on single data member where as comparator interface is used to sort the object based on multiple data members.
Q. Advantage of Collection classes over Arrays?
Ans. Collections are re-sizable in nature. We can increase or decrease the size as per recruitment.
Collections can hold both homogeneous and heterogeneous data’s.
Every collection follows some standard data structures.
Collection provides many useful built in methods for traversing,sorting and search.
Q. What are the Disadvantages of using Collection Classes over Arrays ?
Ans. Collections can only hold objects, It can’t hold primitive data types.
Collections have performance overheads as they deal with objects and offer dynamic memory expansion. This dynamic expansion could be a bigger overhead if the collection class needs consecutive memory location like Vectors.
Collections doesn’t allow modification while traversal as it may lead to concurrentModificationException.
Q. What is Comparable Interface?
Ans. It is used to sort collections and arrays of objects using the collections.sort() and java.utils. The objects of the class implementing the Comparable interface can be ordered.
Q. Explain Set Interface?
Ans. It is a collection of element which cannot contain duplicate elements. The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited.
Q. What is the difference between the size and capacity of a Vector?
Ans. The size is the number of elements actually stored in the vector, while capacity is the maximum number of elements it can store at a given instance of time.
Q. Can we use Ordered Set for performing Binary Search ?
Ans. We need to access values on the basis of an index in Binary search which is not possible with Sets.
Q. Difference between ArrayList and LinkedList ?
Ans. LinkedList and ArrayList are two different implementations of the List interface. LinkedList implements it with a doubly-linked list. ArrayList implements it with a dynamically resizing array.
Q. If you are given a choice to use either ArrayList and LinkedList, Which one would you use and Why ?
Ans. ArrayList are implemented in memory as arrays and hence allows fast retrieval through indices but are costly if new elements are to be inserted in between other elements.
LinkedList allows for constant-time insertions or removals using iterators, but only sequential access of elements
Retrieval – If Elements are to be retrieved sequentially only, Linked List is preferred.
- Insertion – If new Elements are to be inserted in between other elements , Array List is preferred.
- Search – Binary Search and other optimized way of searching is not possible on Linked List.
- Sorting – Initial sorting could be pain but lateral addition of elements in a sorted list is good with linked list.
- Adding Elements – If sufficiently large elements needs to be added very frequently ,Linked List is preferable as elements don’t need consecutive memory location.
Q. What are the pre-requisite for the collection to perform Binary Search ?
Collection should have an index for random access.
Collection should have ordered elements.
Q. Can you provide some implementation of a Dictionary having large number of words ?
Ans. Simplest implementation we can have is a List wherein we can place ordered words and hence can perform Binary Search.
Other implementation with better search performance is to use HashMap with key as first character of the word and value as a LinkedList.
Further level up, we can have linked Hashmaps like ,
a ( key ) -> hashmap (key-aa , value (hashmap(key-aaa,value)
b ( key ) -> hashmap (key-ba , value (hashmap(key-baa,value)
z( key ) -> hashmap (key-za , value (hashmap(key-zaa,value)
upto n levels ( where n is the average size of the word in dictionary.
Q. Which Java collection class can be used to maintain the entries in the order in which they were last accessed?
Q. Is it legal to initialize List like this ?
LinkedList<Integer> l=new LinkedList<int>();
Ans. No, Generic parameters cannot be primitives.
Thanks for reading , Share it with your friends for more Java Interview Questions.
Join with Us :
Official Site : www.guvi.in
GUVI Facebook Page : https://www.facebook.com/MyGUVI
GUVI Facebook Group : https://www.facebook.com/groups/533194816835403/