Java – Interview Questions And Answers Collection

Java Q&A Collection

Looking out for some handy Java Interview Questions and Answers? Find them below.

Q.  Which interface does java.util.Hashtable implement?

Ans. Java.util.Map
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? 

Ans. java.util.map
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 a 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 the object is equal, and then decide if contain() is true or false, or decide if the element could be added in the set or not.
Q.  Difference** between Map and HashMap? **

Ans. A 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 that implement the Collection interface. For example, Collections have methods for finding the max element in a Collection.
Q.  Will this code give a **error if I try to add two heterogeneous elements in the ArrayList? and Why? List list1 = new ArrayList<>(); list1.add(5); list1.add("5"); 
**
Ans. If we don't declare the list to be of a specific type, it treats it as a 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. A comparable interface is used for single sequence sorting i.e.sorting the objects based on a single data member where a 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. Every collection follows some standard data structures. The collection provides many useful built-in methods for traversing, sorting, and search.

So, these are some of the most frequently asked Java Interview Questions. We do have a few more options for you below. But before you get busy finding solutions to those questions, if your quest for Java Interview questions is still sizzling for more, then feel free to find them here with a click. Programmer working on web development code. Engineer programming in python, PHP, javascript on computer. Vector illustration professional software developers concept

So, let’s continue here with more Java Questions!


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 locations like Vectors. Collections don'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 is implemented in memory as arrays and hence allows fast retrieval through indices but is 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. 1. Insertion - If new Elements are to be inserted in between other elements, Array List is preferred. 2. Search - Binary Search and another optimized way of searching are not possible on Linked List. 3. Sorting - Initial sorting could be a pain but lateral addition of elements in a sorted list is good with a linked list. 4. Adding Elements - If sufficiently large elements need 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? **

Ans. The collection should have an index for random access. The collection should have ordered elements.
Q.  Can you provide some implementation of a Dictionary having **large number of words? 

Ans. The simplest implementation we can have is a List wherein we can place ordered words and hence can perform Binary Search. Another implementation with better search performance is to use HashMap with key as the first character of the word and value as a LinkedList. Further level up, we can have linked Hashmaps like hashmap { 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) } up to n levels ( where n is the average size of the word in the dictionary.
Q.  Which Java collection class can be used to maintain the entries in the order in which they were last accessed? 
Ans. LinkedHashMap
For more such Java Interview Questions, click here.
Q.  Is it legal to initialize a List like **this? LinkedList l=new LinkedList(); 

Ans. No, Generic parameters cannot be primitives.   Thanks for reading, Share it with your friends for more Java Interview Questions.

Oh, Yes! You ought to be learning Java in 2021. The top 5 reasons being-

  1. Job Opportunities
  2. The language that stood the test of time 
  3. Wide range of development tools
  4. n number of Java dependent applications
  5. Mobile Application dependence

Java, as a beginner-friendly programming language, has a globally known Code Portability. With open-source library support, this highly demanded commonly used programming language has rich community support and a competitive edge! Above all, Java is designed to work on every operating system. So, no matter what operating system you have Mac, Android, or Windows, you can go ahead and program in Java. In these corona times, when work from home is the only key to survival, this work anywhere phenomenon is a great go-to option!

Learn Java In Tamil - Free Course 

That’s all Folks. We hope, this questionnaire was useful. So, keep learning with GUVI!

You may also like...

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x