1) Is Collection is an Interface or Class?
A) A collection is an Interface.
2) What is the difference between collection and collections?
|1) Collection is an interface in java.util package used to represent a group of as a single entity.||1) Collections is a class in java.util package. This class consists exclusively of static methods that operate on or return Collections.|
3) What is the difference between Arrays and Collections?
|1) Arrays are fixed in size.||1) Collections are growable in nature.|
|2) Arrays can hold only homogeneous data.||2) Collections can hold both homogeneous and heterogeneous elements.|
|3) With respect to memory, arrays concept is not recommended to use.||3) With respect to memory, collections are recommended to use.|
|4) With respect to performance, arrays are recommended to use.||4) With respect to performance, collections are not recommended to use.|
|5) Arrays concept is not implemented based on some data structure, hence ready made method support is not available.||5) Every collection class is implemented based on some data structure, hence for every requirement ready made method support is available.|
|6) Arrays can hold both primitives and object type.||6) The collection can hold only object types and not primitives.|
4) What is the Collections API?
A) The Collections API is a set of classes and interfaces that support operations on the collection of objects.
5) What is Cursor in Java?
A) If we want to receive objects one by one from the Collection then we should use Cursor in Java.
6) How many Cursors are there in Java?
A) There are three cursors available in Java. They are:
3) List Iterator.
7) What is an Iterator?
A) The Iterator interface is used to step through the elements of a Collection. Iterators let you process each element of a collection. Iterators are a generic way to go through all the elements of a collection no matter how it is organized. An iterator is an interface implemented a different way for every Collection.
8) How do you traverse through a collection using its Iterators?
A) To use an Iterator to traverse through the contents of a collection, follow these step:
a) Obtain an Iterator to the start of the collection by calling the collection iterator() method.
b) Set up a loop that makes a call to hasNext(). Have the loop iterate as long as hasNext() returns true.
c) Within the loop, obtain each element by calling next().
9) How do you remove elements during Iterator?
A) Iterator also has a method remove() when remove is called, the current element in the iteration is deleted.
10) What is the List Iterator?
A) List Iterator is just like Iterator, except that it allows us to access the collection in either the forward or backward direction and lets us modify an element.
11) What is an Enumeration?
A) The Enumeration interface defines the methods by which you can enumerate ( obtain one at a time ) the elements from legacy collection objects. We can create Enumeration object by using elements method.
Example: Enumeration e = v.elements();
12) What are the methods present in Enumeration Interface?
A) There are three methods in Enumeration Interface. They are:
13) What are the methods present in Iterator?
A) There are four methods present in Iterator. They are:
4) forEachRemaining( Consumer<? superE> action).
14) What are the methods present in List Iterator?
A) There are nine methods present in List Iterator. They are:
1) add( E e).
9) set( E e).
15) How Enumeration can be converted into an Iterator?
A) An Enumeration can be converted into an Iterator by using the Enumeration.asIterator() method.
16) What is the List Interface?
A) The List interface provides support for ordered collections of objects. Lists may contain duplicate elements.
17) What is the main implementation of the List interface?
A) The main implementation of the List interface are as follows:
- Array List: Resizable-array implementation of the List interface. The best all-around implementation of the List interface.
- Vector: Synchronized resizable-array implementation of the List interface with additional “Legacy methods”.
- Linked List: Doubly-linked list implementation of the List interface. May provide better performance than the Array list implementation if elements are frequently inserted or deleted within the list. Useful for queues and double-ended queues (deques ).
18) What is the advantage of Array List over arrays?
A) Some of the advantages Array List has over arrays are:
- It can grow dynamically.
- It provides more powerful insertion and search mechanisms than arrays.
19) Difference between Array List and Vector?
Array List Vector
1) Array List is Not synchronized by default. 1) Vector List is synchronized by default.
2) Array List can use the only Iterator to access the elements. 2) Vector list can use Iterator and Enumeration Interface to access the elements.
3) The Array List increases its array size by 50 percent if it runs out of a room. 3) A Vector defaults to doubling the size of its array if it runs out of a room.
4) Array List has no default size. 4) While vector has a default size of 10.
20) How to obtain Array from an Array List?
A) An array can be obtained from an Array List using toArray() method on Array List.
List a = new ArrayList();
Object a = a.toArray();
21) How do you decide, when to use Array List and when to use Linked List?
A) If you need to support random access, without inserting or removing elements from any place other than the end, then Array List offers the optimal collection. If however, you need to frequently add and remove elements from the middle of the list and only access the list elements sequentially, then linked list offers the better implementation.
22) What is the Set Interface?
A) The Set Interface provides methods for accessing the elements of a finite mathematical set. Sets do not allow duplicate elements. Contains no methods other than those inherited from a collection. It adds the restriction that duplicate elements are prohibited. Two Set objects are equal if they contain the same elements.
23) What are the main implementations of the Set Interface?
A) The main implementation of the Set interface is as follows:
- Linked HashSet
24) What is a HashSet?
A) A HashSet is an unsorted, unordered Set. It uses the hash code of the object being inserted (so the more efficient your hashcode() implementation, the better access performance you’ll get). Use this class when you want a collection with no duplicates and you don’t care about the order when you iterate through it.
25) What is a TreeSet?
A) TreeSet is a Set implementation that keeps the elements in sorted order. The elements are sorted according to the natural order of elements or by the comparator provided at creation time.
26) What is an EnumSet?
A) An EnumSet is a specialized set for use with enum types, all of the elements in the EnumSet type that is specified, explicitly or implicitly, when the set is created.
27) What is the difference between HashSet and TreeSet?
|1) HashSet is under Set interface i.e. it does not guarantee for either sorted order or sequence order.||1) TreeSet is under Set i.e. it provides elements in a sorted order(acceding order).|
28) What is a Map?
A) A map is an object that stores associations between keys and values (key/value pairs). Given a key, you can find its value. Both keys and values are objects. The key must be unique, but the values may be duplicated. Some maps can accept a null key and null values, others cannot.
29) What is the main Implementation of the Map Interface?
A) The main implementation of the List Interface is as follows:
30) What is the difference between HashMap and when to use TreeMap?
|1) HashMap lets you have null values as well as one null key.||1) Hashtable does not allow null values as key and value.|
|2) The iterator in the HashMap is fall-safe (If you change the map while iterating, you’ll know).||2) The enumerator for the HashTable is not fall-safe.|
|3) HashMap is unsynchronized.||3) HashTable is synchronized.|
31) How does a Hashtable internally maintain the key-value pairs?
A) TreeMap actually implements the SortedMap interface which extends the Map interface. In a Treemap, the data will be sorted in acceding order of keys according to the natural order for the key’s class, or by the comparator provided at creation time. Treemap is based on the Red-Black tree data structure.
32) What are the different collection views that Maps provide?
A) A map provides three Collection Views.
- Key Set – allow a map’s contents to be viewed as a set of keys.
- Values collection – allow a map’s contents to be viewed as a set of values.
- Entry Set – allow a map’s contents to be viewed as a set of key-value mappings.
33) What is a Keyset view?
A) The keyset is a set returned by the KeySet() method of the Map Interface, it is a set that contains all the keys present in the Map.
34) What is a values Collection Views?
A) Values Collection Views is a collection returned by the Values() method of the Map Interface, it contains all the objects present as values in the map.
35) What is an Entry Set View?
A) Entry Set View is a set that is returned by the entryset() method in the map and contains objects of type map. Entry, each of which has both Key and Value.
36) How do you sort an ArrayList (or any list) of user-defined objects?
A) Create an implementation of the java.lang.Comparable interface that knows how to order your objects and pass it to java.util.Collections.sort(List, comparator).
37) What is the Comparable Interface?
A) Comparable Interface is used to order the Objects of user-defined Class. It provides only a single sorting sequence.
38) What is the Comparator?
A) A comparator is an interface present in java.util package. The comparator object reference can be used to define our own customized sorting order.
39) What is Sorted Map?
A) Sorted Map is a sub-interface of Map. If we want to represent a group of key-value pairs according to the sorting order of keys. In Sorted Map we can perform sorting only based on the keys but not values.
40) Which interface provides the capability to store objects using a key-value pair?
41) What is the use of descending keyset()?
A) The descending keyset() method returns a NavigableSet in which the order of the elements is reversed compared to the original key set.
42) What is the use of descendingMap()?
A) The descendingMap() method returns a NavigableMap which is a view of the original Map. The order of the elements in this view map is reverse of the order of the original map.
43) What is the use of headMap()?
A) The headMap() method returns a view of the original NavigableMap which only contains elements that are less than the given element.
44) What is the difference between HashSet and LinkedHashSet?
A) HashSet does not maintain Insertion order of Objects. LinkedHashSet maintains Insertion order of objects.
45) What is the difference between the List and Set?
A) A list can contain duplicate elements whereas Set contains unique elements only.
46) What is Stack?
A) A stack is a child class of vector. If we want to represent object according to last in first out order then we should go far, Stack.
47) What is ceiling key?
A) The ceilingKey() method returns the least(smallest) key in the Map that is greater than or equal to the element passed as a parameter to the ceilingKey() method.
48) What is the PollFirstEntry?
A) The PollFirstEntry() method returns and removes the first entry (key + values) in the NavigableMap or null if the map is empty.
49) What is the use of PollLastEntry?
A) The PollLastEntry returns and removes the last element in the map or null if the map is empty.
50) What is the difference between HashMap and WeakHashMap?
|1) Object is not eligible for garbage collection if it is associated with Hashmap. Even though it does not have any external references.||1) If an Object is not having any external references then it is always eligible for garbage collector even though it is associated with WeakHashMap.|
Also read: Java multithreading interview questions
Also, read: jcpenney kiosk