Top 40 Java.util Interview Questions You Must Prepare 19.Mar.2024

Vectors are dynamic arrays, it contains many legacy methods that are not part of collection framework, and hence these methods are not present in ArrayList. But both are used to form dynamic arrays.

binaryserach() method uses binary search to find a specified value. This method must be applied to sorted arrays.

TreeSet class uses set to store the values added by function add in ascending order using tree for storage

Output:

$ javac Output.java
$ java Output
[1, 3, 4, 8, 9]

An element in a LinkedList object can be changed by first using get() to obtain the index or location of that object and the passing that location to method set() along with its new value.

obj.addFirst(“D”) method is used to add ‘D’ to the start of a LinkedList object obj.

Output:

$ javac Linkedlist.java
$ java Linkedlist
[D, A, B, C]

push() and pop() are standard functions of the class stack, push() inserts in the stack and pop removes from the stack. 3 & 2 are inserted using push() the pop() is used which removes 2 from the stack then again push is used to insert 5 hence stack contains elements 3 & 5.

Output:

$ javac stack.java
$ java stack
[3, 5]

HashSet obj creates an hash object which implements Set interface, obj.size() gives the number of elements stored in the object obj which in this case is 3.

Output:

$ javac Output.java
$ java Output
[A, B, C] 3

Dictionary, Map & Hashtable all implement Map interface hence all of them uses keys to store value in the object.

Hashtable object obj contains values 3, 2, 8 when obj.contains(new Integer(5)) is executed it searches for 5 in the hashtable since it is not present false is returned.

Output:

$ javac hashtable.java
$ java hashtable
false

keySet() methods is used to get a set containing all the keys used in a map. This method provides set view of the keys in the invoking map.

trimTosize() is used to reduce the size of the array that underlines an ArrayList object.

Dictionary, Map & Hashtable all implement Map interface hence all of them uses keys to store value in the object.

When we add an element, the capacity of ArrayList object increases automatically, but we can increase it manually to specified length x by using function ensureCapacity(x);

Although obj.ensureCapacity(3); has manually increased the capacity of obj to 3 but the value is stored only at index 0, therefore obj.size() returns the total number of elements stored in the obj i:e 1, it has nothing to do with ensureCapacity().

Output:

$ javac Output.java
$ java Output
1

trimTosize() is used to reduce the size of the array that underlines an ArrayList object.

Output:

$ javac Output.java
$ java Output
2

Stack, Hashtable, Vector, Properties and Dictionary are legacy classes.

addElement() is used to add data in the vector, to obtain the data we use elementAt() and to first and last element we use firstElement() and lastElement() respectively.

Arrays.sort(array) method sorts the array into 1,2,3,4,5.

Output:

$ javac Array.java
$ java Array
12345

obj1.and(obj2) returns an BitSet object which contains elements common to both the object obj1 and obj2 and stores this BitSet in invoking object that is obj@Hence obj1 contains 3 & 4.

Output:

$ javac Bitset.java
$ java Bitset
{3, 4}

obj1 and obj2 are an object of class ArrayList hence it is a dynamic array which can increase and decrease its size. obj.add(“X”) adds to the array element X and obj.add(1,”X”) adds element x at index position 1 in the list, Both the objects obj1 and obj2 contain same elements i:e A & B thus obj1.equals(obj2) method returns true.

Output:

$ javac Arraylist.java
$ java Arraylist
true

AbstractMap, WeakHashMap, HashMap and TreeMap provide implementation of map interface.

before() returns true if the invoking Date object contains a date that is earlier than one specified by date, otherwise it returns false.

obj.entrySet() method is used to obtain a set that contains the entries in the map. This method provides set view of the invoking map.

Output:

$ javac Maps.java
$ java Maps
[A=1, B=2, C=3]

fill() method assigns a value to all the elements in an array, in other words it fills the array with specified value.

Bitset class creates a special type of array that holds bit values. This array can increase in size as needed.

Maps revolve around two basic operations – get() and put(). to put a value into a map, use put(), specifying the key and the value. To obtain a value, call get() , passing the key as an argument. The value is returned.

obj is an object of class ArrayList hence it is an dynamic array which can increase and decrease its size. obj.add(“X”) adds to the array element X and obj.add(1,”X”) adds element x at index position 1 in the list, Hence obj.add(1,”D”) stores D at index position 1 of obj and shifts the previous value stored at that position by 1.

Output:

$ javac Arraylist.java
$ java Arraylist
[A, D, B, C]

keySet() method returns a set containing all the keys used in the invoking map. Here keys are characters A, B & C. 1, 2, 3 are the values given to these keys.

Output:

$ javac Maps.java
$ java Maps
[A, B, C]

obj.length() returns the length allotted to object obj at time of initialization and obj.size() returns the size of current object obj, each BitSet element is given 16 bits therefore the size is 4 * 16 = 64, whereas length is still 5.

Output:

$ javac Bitset.java
$ java Bitset
5 64

obj.toString returns String equivalent of the hashtable, which can also be obtained by simply writing System.out.print(obj); as print system automatically coverts the obj to string equivalent.

Output:

$ javac hashtable.java
$ java hashtable
{A=3, C=8, B=2}

obj.keySet() returns a set containing all the keys used in properties object, here obj contains keys AB, BC, CD therefore obj.keySet() returns [AB, BC, CD].

Output:

$ javac properties.java
$ java properties
[AB, BC, CD]

obj.get(“B”) method is used to obtain the value associated with key “B”, which is 2.

Output:

$ javac Maps.java
$ java Maps
2

HashSet and TreeSet implements Set interface where as LinkedList and ArrayList implements List interface.

firstly elements 3, 2, 5 are entered in the vector obj, but when obj.removeAll(obj); is executed all the elements are deleted and vector is empty, hence obj.isEmpty() returns true.

Output:

$ javac vector.java
$ java vector
true

removeLast() and removeFirst() methods are used to remove elements in end and beginning of a linked list.

HashSet and TreeSet implements Set interface where as LinkedList and ArrayList implements List interface.

array was containing 5,4,3,2,1 but when method Arrays.fill(array, 1, 4, 8) is called it fills the index location starting with 1 to 4 by value 8 hence array becomes 5,8,8,8,1.

Output:

$ javac Array.java
$ java Array
58881

obj.elementAt(1) returns the value stored at index 1, which is 2.

Output:

$ javac vector.java
$ java vector
2

ArrayList class implements a dynamic array by extending AbstractList class.