Tolal:23 Click:
1
2
Collections Interview Questions And Answers
Page 1
Ques: 1 Consider the following program:
import myLibrary.*;
public class ShowSomeClass
{
// code for the class...
}
What is the name of the java file containing this program?
A. myLibrary.java
B. ShowSomeClass.java
C. ShowSomeClass
D. ShowSomeClass.class
E. Any file name with the java suffix will do
Ans:
(c)ShowSomeClass
Ques: 2 What is HashMap and Map?
Ans:
Map is Interface and Hashmap is class that implements this interface.
Ques: 3 What is the significance of ListIterator?
Ans:
Iterator : Enables you to cycle through a collection in the forward direction only, for obtaining or removing elements
ListIterator : It extends Iterator, allow bidirectional traversal of list and the modification of elements
Ques: 4 Difference between HashMap and HashTable? Can we make hashmap synchronized?
Ans:
1. The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls. (HashMap allows null values as key and value whereas Hashtable doesn’t allow nulls).
2. HashMap does not guarantee that the order of the map will remain constant over time.
3. HashMap is non synchronized whereas Hashtable is synchronized.
4. Iterator in the HashMap is fail-safe while the enumerator for the Hashtable isn't.
Ques: 5 Difference between Vector and ArrayList? What is the Vector class?
Ans:
Vector is synchronized whereas ArrayList is not. The Vector class provides the capability to implement a growable array of objects. ArrayList and Vector class both implement the List interface. Both classes are implemented using dynamically resizable arrays, providing fast random access and fast traversal. In vector the data is retrieved using the elementAt() method while in ArrayList, it is done using the get() method. ArrayList has no default size while vector has a default size of 10. when you want programs to run in multithreading environment then use concept of vector because it is synchronized. But ArrayList is not synchronized so, avoid use of it in a multithreading environment.
Ques: 6 What is an Iterator interface? Is Iterator a Class or Interface? What is its use?
Ans:
The Iterator is an interface, used to traverse through the elements of a Collection. It is not advisable to modify the collection itself while traversing an Iterator.
Ques: 7 What is the List interface?
Ans:
The List interface provides support for ordered collections of objects.
Ques: 8 How can we access elements of a collection?
Ans:
We can access the elements of a collection using the following ways:
1.Every collection object has get(index) method to get the element of the object. This method will return Object.
2.Collection provide Enumeration or Iterator object so that we can get the objects of a collection one by one.
Ques: 9 What is the Set interface?
Ans:
The Set interface provides methods for accessing the elements of a finite mathematical set. Sets do not allow duplicate elements.
Ques: 10 What’s the difference between a queue and a stack?
Ans:
Stack is a data structure that is based on last-in-first-out rule (LIFO), while queues are based on First-in-first-out (FIFO) rule.
Ques: 11 What is the Map interface?
Ans:
The Map interface is used associate keys with values.
Ques: 12 What is the Properties class?
Ans:
The properties class is a subclass of Hashtable that can be read from or written to a stream. It also provides the capability to specify a set of default values to be used.
Ques: 13 How can we use hashset in collection interface?
Ans:
This class implements the set interface, backed by a hash table (actually a HashMap instance). It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. This class permits the Null element.
This class offers constant time performance for the basic operations (add, remove, contains and size), assuming the hash function disperses the elements properly among the buckets.
Ques: 14
What are differences between Enumeration, ArrayList, Hashtable and Collections and Collection?
Ans:
Enumeration: It is series of elements. It can be use to enumerate through the elements of a vector, keys or values of a hashtable. You can not remove elements from Enumeration.
ArrayList: It is re-sizable array implementation. Belongs to 'List' group in collection. It permits all elements, including null. It is not thread -safe.
Hashtable: It maps key to value. You can use non-null value for key or value. It is part of group Map in collection.
Collections: It implements Polymorphic algorithms which operate on collections.
Collection: It is the root interface in the collection hierarchy.
Ques: 15 What is difference between array & arraylist?
Ans:
An ArrayList is resizable, where as, an array is not. ArrayList is a part of the Collection Framework. We can store any type of objects, and we can deal with only objects. It is growable. Array is collection of similar data items. We can have array of primitives or objects. It is of fixed size. We can have multi dimensional arrays.
Array: can store primitive
ArrayList: Stores object only
Array: fix size ArrayList: resizable
Array: can have multi dimensional
Array: lang ArrayList: Collection framework
Ques: 16 Can you limit the initial capacity of vector in java?
Ans:
Yes you can limit the initial capacity. We can construct an empty vector with specified initial capacity
public vector(int initialcapacity)
Ques: 17 What method should the key class of Hashmap override?
Ans:
The methods to override are equals() and hashCode().
Ques: 18 What is the difference between Enumeration and Iterator?
Ans:
The functionality of Enumeration interface is duplicated by the Iterator interface. Iterator has a remove() method while Enumeration doesn't. Enumeration acts as Read-only interface, because it has the methods only to traverse and fetch the objects, where as using Iterator we can manipulate the objects also like adding and removing the objects.
Ques: 19 What is an Iterator?
Ans:
Some of the collection classes provide traversal of their contents via a java.util.Iterator interface. This interface allows you to walk through a collection of objects, operating on each object in turn. Remember when using Iterators that they contain a snapshot of the collection at the time the Iterator was obtained; generally it is not advisable to modify the collection itself while traversing an Iterator.
Ques: 20 What is fail-fast property?
Ans:
At high level - Fail-fast is a property of a system or software with respect to its response to failures. A fail-fast system is designed to immediately report any failure or condition that is likely to lead to failure. Fail-fast systems are usually designed to stop normal operation rather than attempt to continue a possibly-flawed process.
When a problem occurs, a fail-fast system fails immediately and visibly. Failing fast is a non-intuitive technique: "failing immediately and visibly" sounds like it would make your software more fragile, but it actually makes it more robust. Bugs are easier to find and fix, so fewer go into production.
In Java, Fail-fast term can be related to context of iterators. If an iterator has been created on a collection object and some other thread tries to modify the collection object "structurally", a concurrent modification exception will be thrown. It is possible for other threads though to invoke "set" method since it doesn't modify the collection "structurally". However, if prior to calling "set", the collection has been modified structurally, "IllegalArgumentException" will be thrown.
Goto Page:
1
2
Collections Objective
Collections Objective Questions And Answers
Collections Interview Questions And Answers
Collections Interview Questions And Answers
R4R,Collections Objective, Collections Subjective, Collections Interview Questions And Answers,Collections,Collections Interview,Collections Questions ,Collections Answers