Ads Area

Java Collections Top interview Questions for freshers

  Java  Collections Top interview Questions for freshers 

1. Which interface is part of the collection framework but does not extend the Collection interface? Why? 

The Map interface is a part but does not extend the Collection interface. This is because they are not compatible. Collection requires an object as a parameter, but the map takes a key-value pair.

2. Which method of the Collections class is used to return an integer hash code? 

hashCode() does the needful.

3. Name the class of Java collection framework that is used when you do not need duplicates?

Set is used to avoid duplicate values.

4. Why does Collection only extend Iterable and not Cloneable and Serializable?

This is because Serializable and Cloneable need not be extended because of their very specialized behavior.

5. What is enumeration?


What is a stack? Give  one of its advantages. 

Stack is a collection operation that works on the Last In First Out principle. It helps to manage data in a LIFO method that is not possible by Array or LinkedList.

 Is there a vector in java? Explain.

Yes. In Java, a vector is similar to an array. The elements can be accessed using the index value. It can contain legacy methods even if they are not part of the Collection framework.

 Is it possible to convert an array to LinkedList? Which types of the array are convertible.

Yes, it is possible by the Array.aslist() method. Any array (String, int, char) can be converted.

 Mention the features of an ArrayList. (Questions on ArrayList are also commonly asked as java collection interview questions)

ArrayList permits is an implementation of List Interface that contains duplicate values and is an ordered collection. The size can be increased dynamically.

 What is the critical difference between a list and a set?

Set does not allow duplicates and is an unordered collection that allows only one null element. Whereas duplicates are allowed in the list, which is an ordered collection. The list also allows null elements.

Mention the linked lists supported by Java.

Java supports only two types of linked lists:

  1. Singly Linked List: Each node of the list stores data and reference to the next node,
  2. Doubly Linked List: One node references two nodes- previous and next.

 How is an ArrayList different from a vector?

The ArrayList methods are not synchronized, and when resized, its size increases by half and not double as in the case of the vector.

 How to create a linked list and a vector in Java?

Linkedlist obj = new Linkedlist();

Vector obj = new Vector(size,increment);


Set – Java Collection Interview Questions

Let us see some java collection interview questions on Sets:

1. Name the three general-purpose Set implementations provided by Java.

The three implementations of the Set are:

  1. HashSet
  2. TreeSet
  3. LinkedHashSet

2. Why is only one null key allowed in HashSet?

As there is no null check in the add method, so one null key is allowed. However, it can’t be duplicated.

3. How does the HashSet class store elements?

It is an instance of HashMap and uses a hashing technique to store information as a hash code.

4. Name a set interface apart from the general-purpose set implementations. Explain.

EnumSet is also a class implementing the Set interface. It can be used with enum types, not synchronized, and cannot have null keys.

5. What does the Collections.emptySet() return?

This method returns a serializable set that is empty and immutable.

6. HashSet features are very commonly asked as java collection interview questions.

How is it different from a HashMap?

HashSet is an unordered collection of unique elements, and HashMap is a collection of key-pair values.

7. What do you know about a SortedSet?

It is an interface present in Collection framework that a TreeSet implements. This set provides total ordering on its elements as per their natural ordering.

In Java, questions on collections are most common amongst the interviewers. In this blog, we would be covering frequently asked java collection interview questions for experienced candidates and freshers to crack their following interview. If you are new to Java, then you should first check out what is java is and the resources  to learn it.

Generic Java Collection Interview Questions

Let us explore some generic java collection interview questions : 

1. Which interface is part of the collection framework but does not extend the Collection interface? Why? 

The Map interface is a part but does not extend the Collection interface. This is because they are not compatible. Collection requires an object as a parameter, but the map takes a key-value pair.

2. Which method of the Collections class is used to return an integer hash code? 

hashCode() does the needful.

3. Name the class of Java collection framework that is used when you do not need duplicates?

Set is used to avoid duplicate values.

4. Why does Collection only extend Iterable and not Cloneable and Serializable?

This is because Serializable and Cloneable need not be extended because of their very specialized behavior.

5. What is enumeration?

 is the traversal of legacy elements only on the collection. It is not fail-fast. More so, it is an interface that defines the methods in which you can obtain one element at a time in a collection.

Here's How to Land a Top Softwar

6. Can you sort a collection in Java? How?

Yes. It can be simply done by calling Collections.sort() or Collections.reverseOrder() method.

7. Define Java Generics. Note that one of the most asked topics for java collection interview questions is generics.

It is a crucial Java feature added to remove the risk of ClassCastException at runtime and provide compile-time code safety.

8. List down methods to make collection thread-safe. 

 All these methods take collection as parameter and return thread safe and synchronized collection :

  • Collections.synchronizedList(list);
  • Collections.synchronizedSet(set);
  • Collections.synchronizedMap(map);

9. Name the data structure that keeps the lowest or highest element at the head for accessing and removing it in constant time. 

PriorityQueue in Java is the mentioned data structures . It provides the facility of the queue but not in the First In First Out manner but rather elements are ordered based on custom comparator or as per their natural ordering.

10. What is meant by fail-safe? 

The opposite of fail-fast. It means that they will not fail if the underlying collection on which iteration takes place is modified.


List – Java Collection Interview Question

Here are some java collection interview questions on the List interface:

1. Give a difference between LinkedList and ArrayList in Java?

Only ArrayList allows searching via index.

2. Mention the main classes implementing the List interface.

LinkedList- elements cannot be accessed via index

ArrayList- elements can be accessed with an index

Vector- Similar to array and can be indexed

Stack- Last In First Out method data structure.

3. What is a stack? Give  one of its advantages. 

Stack is a collection operation that works on the Last In First Out principle. It helps to manage data in a LIFO method that is not possible by Array or LinkedList.

4. Is there a vector in Java ? Explain.

Yes. In Java, a vector is similar to an array. The elements can be accessed using the index value. It can contain legacy methods even if they are not part of the Collection framework.

5. Is it possible to convert an array to LinkedList? Which types of the array are convertible.

Yes, it is possible by the Array.aslist() method. Any array (String, int, char) can be converted.

6. Mention the features of an ArrayList. (Questions on ArrayList are also commonly asked as java collection interview questions)

ArrayList permits is an implementation of List Interface that contains duplicate values and is an ordered collection. The size can be increased dynamically.

7. What is the critical difference between a list and a set?

Set does not allow duplicates and is an unordered collection that allows only one null element. Whereas duplicates are allowed in the list, which is an ordered collection. The list also allows null elements

8. What is a LinkedHashSet?

It is a subclass of the HashSet class that implements the set interface. It maintains a doubly-linked list across it all elements and is a well-ordered version of HashSet.

9. Name two methods of the HashSet class.

boolean add(Object o)

Adds the given element to the set if not present already

boolean contains(Object o):

Checks if the specified element is present in the set.

10. Differentiate between Map and Set.

Set extends the collection interface, whereas the map does not. The map can have duplicate values and multiple null values.

conclusions:

Here we provide java collection Questions for freshers to carck the interview .

FAQ

tricky java collection interview questions?


Many experienced Java programmers think the ‘finally’ block will always execute. However, that reasoning is challenged here by putting a return statement in the ‘try’ or ‘catch’ block or calling System.exit() from the ‘try’ or ‘catch’ block.

The solution for this mind-bender is that the ‘finally’ block will execute successfully, even if you put a return statement in the ‘try’ or ‘catch’ block. However, the ‘finally’ block will fail to execute if you call System.exit() from the ‘try’ or ‘catch’ block.


Post a Comment

0 Comments

1st