Top 40 Java.util Interview Questions
Q1. Which Class Object Can Be Used To Form A Dynamic Array?
Vectors are dynamic arrays, it contains many legacy techniques that aren't a part of collection framework, and consequently those strategies 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 seek to find a certain fee. This approach have to be applied to taken care of arrays.
Q3. What Is The Output Of This Program?
1. Import Java.Util.*;
2. Class Output
3. Public Static Void Main(string Args[])
four. Treeset T = New
TreeSet class uses set to shop the values added by way of characteristic upload in ascending order the usage of tree for garage
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 may be modified by first the use of get() to reap the index or area of that object and the passing that region to technique set() along with its new value.
Q5. What Is The Output Of This Program?
1. Import Java.Util.*;
2. Class Linkedlist
three. Public Static Void Main(string Args[])
4. Linkedlist Ob
obj.AddFirst(“D”) method is used to feature ‘D’ to the begin 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[])
four. Stack Obj = New St
push() and pop() are standard functions of the magnificence stack, push() inserts within the stack and pop removes from the stack. Three & 2 are inserted the usage of push() the pop() is used which eliminates 2 from the stack however push is used to insert five therefore stack consists of elements three & five.
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.Length() gives the wide variety of elements saved inside 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 for this reason all of them makes use of keys to save value within the item.
Q9. What Is The Output Of This Program?
1. Import Java.Util.*;
2. Class Bitset
three. Public Static Void Main(string Args[])
4. Bitset Obj = New
Output:
$ javac Bitset.Java
$ java Bitset
zero, 1, three, four
Q10. What Is The Output Of This Program?
1. Import Java.Util.*;
2. Class Hashtable
three. Public Static Void Main(string Args[])
four. Hashtable Obj
Hashtable object obj contains values three, 2, eight while obj.Includes(new Integer(5)) is done it searches for five within the hashtable because it isn't present fake is back.
Output:
$ javac hashtable.Java
$ java hashtable
fake
Q11. Which Methods Can Be Used To Obtain Set Of All Keys In A Map?
KeySet() methods is used to get a hard and fast containing all the keys used in a map. This method affords set view of the keys inside the invoking map.
Q12. Which Method Is Used To Reduce The Capacity Of An Arraylist Object?
TrimTosize() is used to lessen the dimensions of the array that underlines an ArrayList object.
Q13. Which Is The Interface Of Legacy Is Implemented By Hash-desk And Dictionary Classes?
Dictionary, Map & Hashtable all enforce Map interface for this reason all of them uses keys to keep cost within the object.
Q14. Which Method Can Be Used To Increase The Capacity Of Arraylist Object Manually?
When we add an detail, the capability of ArrayList object increases routinely, but we can increase it manually to particular duration x through the use of feature ensureCapacity(x);
Q15. What Is The Output Of This Program?
1. Import Java.Util.*;
2. Class Output
3. Public Static Void Main(string Args[])
four. Arraylist Obj = N
Although obj.EnsureCapacity(3); has manually increased the ability of obj to 3 however the cost is saved handiest at index zero, consequently obj.Length() returns the full range of elements saved in the obj i:e 1, it has not anything 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[])
three. Arraylist Obj = New Arraylist();
four.
TrimTosize() is used to lessen the scale 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 statistics within the vector, to achieve the records we use elementAt() and to first and closing detail we use firstElement() and lastElement() respectively.
Q19. What Is The Output Of This Program?
1. Import Java.Util.*;
2. Class Array
three. Public Static Void Main(string Args[])
4. Int Array[] = New
Arrays.Kind(array) approach types the array into 1,2,three,four,five.
Output:
$ javac Array.Java
$ java Array
12345
Q20. What Is The Output Of This Program?
1. Import Java.Util.*;
2. Class Bitset
three. Public Static Void Main(string Args[])
4. Bitset Obj1 = New
obj1.And(obj2) returns an BitSet item which incorporates elements commonplace to both the object obj1 and obj2 and shops this BitSet in invoking item that is obj@Hence obj1 consists of three & 4.
Output:
$ javac Bitset.Java
$ java Bitset
3, 4
Q21. What Is The Output Of This Program?
1. Import Java.Util.*;
2. Class Arraylist
three. Public Static Void Main(string Args[])
four. Arraylist Obj1
obj1 and obj2 are an item of sophistication ArrayList for this reason it is a dynamic array which could increase and reduce its length. Obj.Add(“X”) adds to the array element X and obj.Add(1,”X”) provides detail x at index role 1 within the list, Both the objects obj1 and obj2 contain identical factors i:e A & B as a consequence obj1.Equals(obj2) technique returns actual.
Output:
$ javac Arraylist.Java
$ java Arraylist
actual
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?
Earlier than() returns true if the invoking Date item carries a date this is earlier than one special by means of date, otherwise it returns false.
Q24. What Is The Output Of This Program?
1. Import Java.Util.*;
2. Class Maps
three. Public Static Void Main(string Args[])
4. Treemap Obj = New T
obj.EntrySet() technique is used to reap a set that consists of the entries inside the map. This approach offers 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() technique assigns a fee to all of the factors in an array, in different words it fills the array with specific price.
Q26. Which Class Object Has Architecture Similar To That Of Array?
Bitset class creates a unique sort of array that holds bit values. This array can boom in length as wanted.
Q27. Which Method Is Used Add An Element And Corresponding Key To A Map?
Maps revolve around two basic operations – get() and placed(). To put a value right into a map, use put(), specifying the key and the cost. To attain a value, name get() , passing the key as an argument. The value is lower back.
Q28. What Is The Output Of This Program?
1. Import Java.Util.*;
2. Class Arraylist
three. Public Static Void Main(string Args[])
four. Arraylist Obj
obj is an item of class ArrayList consequently it's miles an dynamic array that can growth and reduce its size. Obj.Add(“X”) adds to the array detail X and obj.Add(1,”X”) provides element x at index position 1 in the listing, Hence obj.Upload(1,”D”) shops D at index role 1 of obj and shifts the preceding fee saved at that function with the aid of 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[])
four. Hashmap Obj = New H
keySet() technique returns a set containing all of the keys used in the invoking map. Here keys are characters A, B & C. 1, 2, three 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.Duration() returns the length allotted to item obj at time of initialization and obj.Size() returns the scale of present day object obj, each BitSet detail is given sixteen bits consequently the size is 4 * sixteen = 64, whereas period is still five.
Output:
$ javac Bitset.Java
$ java Bitset
five sixty four
Q31. What Is The Output Of This Program?
1. Import Java.Util.*;
2. Class Hashtable
3. Public Static Void Main(string Args[])
four. Hashtable Obj
obj.ToString returns String equal of the hashtable, which also can be received by means of without a doubt writing System.Out.Print(obj); as print machine routinely coverts the obj to thread equivalent.
Output:
$ javac hashtable.Java
$ java hashtable
A=3, C=eight, 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 hard and fast containing all the keys used in houses item, right here obj consists of keys AB, BC, CD consequently obj.KeySet() returns [AB, BC, CD].
Output:
$ javac houses.Java
$ java residences
[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[])
four. Hashmap Obj = New H
obj.Get(“B”) method is used to obtain the price related to 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
three. Public Static Void Main(string Args[])
4. Vector Obj = New
firstly elements 3, 2, five are entered inside the vector obj, however whilst obj.RemoveAll(obj); is achieved all the elements are deleted and vector is empty, for this reason obj.IsEmpty() returns real.
Output:
$ javac vector.Java
$ java vector
proper
Q36. Which Of These Methods Can Be Used To Delete The Last Element In A Linkedlist Object?
RemoveLast() and removeFirst() strategies are used to get rid of elements in cease and starting of a linked listing.
Q37. Which Of These Classes Implements Set Interface?
HashSet and TreeSet implements Set interface in which as LinkedList and ArrayList implements List interface.
Q38. What Is The Output Of This Program?
1. Import Java.Util.*;
2. Class Array
three. Public Static Void Main(string Args[])
4. Int Array[] = New
array changed into containing five,4,3,2,1 but when technique Arrays.Fill(array, 1, four, 8) is known as it fills the index location starting with 1 to four by fee eight hence array becomes 5,8,8,eight,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[])
four. Vector Obj = New
obj.ElementAt(1) returns the value saved at index 1, that is 2.
Output:
$ javac vector.Java
$ java vector
2
Q40. Which Standard Collection Classes Implements A Dynamic Array?
ArrayList magnificence implements a dynamic array with the aid of extending AbstractList magnificence.

