300+ [LATEST] Java.util Interview Questions and Answers

Q1. Which Class Object Can Be Used To Form A Dynamic Array?

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.

Q2. Which Methods Can Be Used To Search An Element In A List?

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

Q3. What Is The Output Of This Program? 1. Import Java.util.*; 2. Class Output { 3. Public Static Void Main(string Args[]) { 4. Treeset T = New

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]

Q4. Which Method Is Used To Change An Element In A Linkedlist Object?

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.

Q5. What Is The Output Of This Program? 1. Import Java.util.*; 2. Class Linkedlist { 3. Public Static Void Main(string Args[]) { 4. Linkedlist Ob

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]

Q6. What Is The Output Of This Program? 1. Import Java.util.*; 2. Class Stack { 3. Public Static Void Main(string Args[]) { 4. Stack Obj = New St

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]

Q7. What Is The Output Of This Program? 1. Import Java.util.*; 2. Class Output { 3. Public Static Void Main(string Args[]) { 4. Hashset Obj = New

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

Q8. Which Class Object Uses Key To Store Value?

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

Q9. What Is The Output Of This Program? 1. Import Java.util.*; 2. Class Bitset { 3. Public Static Void Main(string Args[]) { 4. Bitset Obj = New

Output:

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

Q10. What Is The Output Of This Program? 1. Import Java.util.*; 2. Class Hashtable { 3. Public Static Void Main(string Args[]) { 4. Hashtable Obj

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

Q11. Which Methods Can Be Used To Obtain Set Of All Keys In A Map?

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.

Q12. Which Method Is Used To Reduce The Capacity Of An Arraylist Object?

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

Q13. Which Is The Interface Of Legacy Is Implemented By Hash-table And Dictionary Classes?

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

Q14. Which Method Can Be Used To Increase The Capacity Of Arraylist Object Manually?

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);

Q15. What Is The Output Of This Program? 1. Import Java.util.*; 2. Class Output { 3. Public Static Void Main(string Args[]) { 4. Arraylist Obj = N

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

Q16. What Is The Output Of This Program? 1. Class Output { 2. Public Static Void Main(string Args[]) { 3. Arraylist Obj = New Arraylist(); 4.

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

Output:

$ javac Output.java
$ java Output
2

Q17. What Are Legacy Classes?

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

Q18. Which Methods Is Used To Add Elements In Vector At Specific Location?

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.

Q19. What Is The Output Of This Program? 1. Import Java.util.*; 2. Class Array { 3. Public Static Void Main(string Args[]) { 4. Int Array[] = New

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

Output:

$ javac Array.java
$ java Array
12345

Q20. What Is The Output Of This Program? 1. Import Java.util.*; 2. Class Bitset { 3. Public Static Void Main(string Args[]) { 4. Bitset Obj1 = New

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}

Q21. What Is The Output Of This Program? 1. Import Java.util.*; 2. Class Arraylist { 3. Public Static Void Main(string Args[]) { 4. Arraylist Obj1

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

Q22. Which Classes Provide Implementation Of Map Interface?

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

Q23. What Is A Method Of Class Date Which Is Used To Search Weather Object Contains A Date Before The Specified Date?

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

Q24. What Is The Output Of This Program? 1. Import Java.util.*; 2. Class Maps { 3. Public Static Void Main(string Args[]) { 4. Treemap Obj = New T

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]

Q25. Which Method Is Used To Make All Elements Of An Equal To Specified Value?

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

Q26. Which Class Object Has Architecture Similar To That Of Array?

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

Q27. Which Method Is Used Add An Element And Corresponding Key To A Map?

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.

Q28. What Is The Output Of This Program? 1. Import Java.util.*; 2. Class Arraylist { 3. Public Static Void Main(string Args[]) { 4. Arraylist Obj

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]

Q29. What Is The Output Of This Program? 1. Import Java.util.*; 2. Class Maps { 3. Public Static Void Main(string Args[]) { 4. Hashmap Obj = New H

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]

Q30. What Is The Output Of This Program? 1. Import Java.util.*; 2. Class Bitset { 3. Public Static Void Main(string Args[]) { 4. Bitset Obj = New

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

Q31. What Is The Output Of This Program? 1. Import Java.util.*; 2. Class Hashtable { 3. Public Static Void Main(string Args[]) { 4. Hashtable Obj

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}

Q32. What Is The Output Of This Program? 1. Import Java.util.*; 2. Class Properties { 3. Public Static Void Main(string Args[]) { 4. Properties Ob

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]

Q33. What Is The Output Of This Program? 1. Import Java.util.*; 2. Class Maps { 3. Public Static Void Main(string Args[]) { 4. Hashmap Obj = New H

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

Output:

$ javac Maps.java
$ java Maps
2

Q34. Which Classes Implements Set Interface?

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

Q35. What Is The Output Of This Program? 1. Import Java.util.*; 2. Class Vector { 3. Public Static Void Main(string Args[]) { 4. Vector Obj = New

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

Q36. Which Of These Methods Can Be Used To Delete The Last Element In A Linkedlist Object?

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

Q37. Which Of These Classes Implements Set Interface?

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

Q38. What Is The Output Of This Program? 1. Import Java.util.*; 2. Class Array { 3. Public Static Void Main(string Args[]) { 4. Int Array[] = New

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

Q39. What Is The Output Of This Program? 1. Import Java.util.*; 2. Class Vector { 3. Public Static Void Main(string Args[]) { 4. Vector Obj = New

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

Output:

$ javac vector.java
$ java vector
2

Q40. Which Standard Collection Classes Implements A Dynamic Array?

ArrayList class implements a dynamic array by extending AbstractList class.