COLLECTION Programming Questions & Answers

Explore model answers categorized by subjects like Java, HTML, DBMS, and more.

COLLECTION Programming Questions & Answers

How many bits are used to represent Unicode, ASCII, UTF-16, and UTF-8 characters?

Answer:
Unicode requires 16 bits and ASCII require 7 bits. Although the ASCII character set uses only 7 bits, it is usually represented as 8 bits. UTF-8 represents characters using 8, 16, and 18 bit patterns. UTF-16 uses 16-bit and larger bit patterns .

Which method of the Component class is used to set the position and size of a component?

Answer:

Method is that :

setBounds()

What is the difference between the >> and >>> operators?

Answer:

Basic diff are there :

> The >> operator : carries the sign bit when shifting right. 

> The >>> operator : zero-fills bits that have been shifted out.

What modifiers may be used with an inner class that is a member of an outer class?

Answer:
A non-local or inner class may be declared as public, protected, private, static, final, or abstract. 

How does Java handle integer overflows and underflows?

Answer:
It uses those low order bytes of the result that can fit into the size of the type allowed by the operation.

Which characters may be used as the second character of an identifier,but not as the first character of an identifier?

Answer:
The digits 0 through 9 may not be used as the first character of an identifier but they may be used after the first character of an identifier.

What is the Collections API?

Answer:
The Collections API is a set of classes and interfaces that support operations on collections of objects.

What is the List interface?

Answer:
The List interface is bassically provides support for ordered collections of objects.

What is the Vector class?

Answer:
The Vector class is bassically a provides the capability to implement a growable array of objects.  

What is an Iterator interface?

Answer:
The Iterator interface is bassically used to step through the elements of a Collection .

Which java.util classes and interfaces support event handling?

Answer:
The EventObject class and the EventListener interface support event Handaling.

What is the GregorianCalendar class?

Answer:
The GregorianCalendar is bassicallt provides support for traditional Western calendars 

What is the Local class?

Answer:
The Local class is bassically used to tailor program output to the conventions of a particular geographic, political, or cultural region .

What is the SimpleTimeZone class?

Answer:
The SimpleTimeZone class is bassically provides support for a Gregorian calendar 

What is the Map interface?

Answer:
The Map interface replaces the JDK 1.1 Dictionary class and is used associate keys with values.

What is the highest-level event class of the event-delegation model?

Answer:
The java.util.EventObject class is the highest-level class in the event-delegation class hierarchy.

What is the Collection interface?

Answer:
The java.util.EventObject class is the highest-level class in the event-delegation class hierarchy.

What is the Set interface?

Answer:
The Set interface is bassically a use for method which provides methods for accessing the elements of a finite mathematical set. Sets do not allow duplicate elements. 

What is the typical use of Hashtable?

Answer:
Hash table is very important that time When a program wants to store a key value pair, one can use Hashtable.

We trying to store an object using a key in a Hashtable. And some other object already exists in that location, then what will happen? The existing object will be overwritten? Or the new object will be stored elsewhere?

Answer:
The existing object will be overwritten and thus it will be lost.

What is the difference between the size and capacity of a Vector?

Answer:

There are many diff : 

> The size is the number of elements actually stored in the vector, While capacity is the maximum number of elements it can store at a given instance of time.

Can a vector contain heterogenous objects?

Answer:
Yes, A Vector can contain heterogenous objects. Because a Vector stores everything in terms of Object.

Can a ArrayList contain heterogenous objects?

Answer:
Yes, A ArrayList can contain heterogenous objects. Because a ArrayList stores everything in terms of Object.

What is an enumeration?

Answer:
An enumeration is an interface containing methods for accessing the underlying data structure from which the enumeration is obtained. It is a construct which collection classes return when We request a collection of all the objects stored in the collection. It allows sequential access to all the elements stored in the collection.

Considering the basic properties of Vector and ArrayList, where will you use Vector and where will you use ArrayList?

Answer:

The basic difference between a Vector and an ArrayList is that :

> Vector is synchronized while ArrayList is not. Thus whenever there is a possibility of multiple threads accessing the same instance, one should use Vector. While if not multiple threads are going to access the same instance then use ArrayList.

> Non synchronized data structure will give better performance than the synchronized one.

What is HashMap and Map?

Answer:

Basic diff are there :

> Map is Interface and Hashmap is class that implements this interface.

What is the significance of ListIterator?

Answer:

The Basic diff are there :

> Iterator : Enables we 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.

What is the difference b/w Iterator and ListIterator?

Answer:

The Basic diff are there : 

> 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.

Difference between HashMap and HashTable?

Answer:

Basic diff are there : 

> 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.

> HashMap does not guarantee that the order of the map will remain constant over time.

> HashMap is non synchronized whereas Hashtable is synchronized.

> Iterator in the HashMap is fail-safe while the enumerator for the Hashtable isn\'t. 

What is the difference between set and list?

Answer:

Th Basic diff are :

> A Set stores elements in an unordered way and does not contain duplicate elements, whereas a list stores elements in an ordered way but may contain duplicate elements.

Difference between Vector and ArrayList? What is the Vector class?

Answer:

The Basic diff are there :

> 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 we 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.

What is an Iterator interface? Is Iterator a Class or Interface? What is its use?

Answer:
The Iterator is bassically an interface, Which is used to traverse through the elements of a Collection. It is not advisable to modify the collection itself while traversing an Iterator.

How can we access elements of a collection?

Answer:

We have a many ways to access the elements of a collection which is followinf as :

> Every collection object has get(index) method to get the element of the object. This method will return Object.

> Collection provide Enumeration or Iterator object so that we can get the objects of a collection one by one.

What is the Set interface?

Answer:
The Set interface is bassically use for the methods which is provides methods for accessing the elements of a finite mathematical set. Sets do not allow duplicate elements.

What is the difference between a queue and a stack?

Answer:

There are many diff b/w stack and DS, But here basic diff are following as :

> 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.

What is the Map interface ?

Answer:
The Map interface bassically used associate keys with values.

What is the Properties class?

Answer:
The Properties class is bassically 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.

Which implementation of the List interface provides for the fastest insertion of a new element into the middle of the list?

Answer:

> Vector

> ArrayList

> LinkedList

>  None of the above

                ArrayList and Vector both use an array to store the elements of the list. When an element is inserted into the middle of the list the elements that follow the insertion point must be shifted to make room for the new element. The LinkedList is implemented using a doubly linked list; an insertion requires only the updating of the links at the point of insertion. Therefore, the LinkedList allows for fast insertions and deletions

How can we use hashset in collection interface?

Answer:
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 assuming the hash function disperses the elements properly among the buckets.

What are differences between Enumeration, ArrayList, Hashtable and Collections and Collection?

Answer:

Diff are there :

> Enumeration : It is series of elements. It can be use to enumerate through the elements of a vector, keys or values of a hashtable. We 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. We 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.

What is difference between array & arraylist?

Answer:

Many diff are there : 

> 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             V/s        ArrayList


> can store primitive     > Stores object only


> fix size                > Resizable


> can have multi 

  dimensional


> lang                    > Collection framework

Can you limit the initial capacity of vector in java?

Answer:

Yes, We can limit the initial capacity. We can construct an empty vector with specified initial capacity.

public vector(int initialcapacity)

What method should the key class of Hashmap override?

Answer:
The methods to override are equals() and hashCode().

What is the difference between Enumeration and Iterator?

Answer:

Basic diff are there : 

> 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.

> Enumeration is used when ever we want to make Collection objects as Read-only. 

Explain Java Collections Framework?

Answer:
Java Collections Framework is bassiclaly provides a well designed set of interfaces and classes that support operations on a collections of objects. 

Explain Enumeration Interface.

Answer:

The Enumeration interface bassically allows us to iterate through all the elements of a collection. Iterating through an Enumeration is similar to iterating through an Iterator. However, there is no removal support with Enumeration.

Basic methods are there :

> boolean hasMoreElements();

> Object nextElement(); 


What is the difference between Enumeration and Iterator interface?

Answer:

Basic diff are there : 

> The Enumeration interface allows us to iterate through all the elements of a collection.

> Iterating through an Enumeration is similar to iterating through an Iterator. However, there is no removal support with Enumeration. 

What are the two types of Set implementations available in the Collections Framework?

Answer:

> HashSet and 

> TreeSet 

         Are the two Set implementations available in the Collections Framework. 

What is the difference between HashSet and TreeSet?

Answer:

The Basic diff are there :


> HashSet Class implements java.util.Set interface to eliminate the duplicate entries and uses hashing for storage. Hashing is nothing but mapping between a key value and a data item, this provides efficient searching.


> The TreeSet Class implements java.util.Set interface provides an ordered set, eliminates duplicate entries and uses tree for storage. 


What are the two types of List implementations available in the Collections Framework?

Answer:

> ArrayList and 

> LinkedList 

           Are the two List implementations available in the Collections Framework. 

What is the difference between ArrayList and LinkedList?

Answer:

The Basic diff are there :

> The ArrayList Class implements java.util.List interface and uses array for storage. An array storage\'s are generally faster but we cannot insert and delete entries in middle of the list.To achieve this kind of addition and deletion requires a new array constructed. We can access any element at randomly. 


> The LinkedList Class implements java.util.List interface and uses linked list for storage.A linked list allow elements to be added, removed from the collection at any location in the container by ordering the elements.With this implementation we can only access the elements in sequentially.


What collection will you use to implement a queue?

Answer:
LinkedList 

Explain Map Interface.

Answer:
A Map is bassically a special kind of set with no duplicates. The key values are used to lookup, or index the stored data. The Map interface is not an extension of Collection interface, it has it\'s own hierarchy. Map does not allow duplicates in the collection. In Map implementations null is valid entry, but allowed only once. 

What are the two types of Map implementations available in the Collections Framework?

Answer:

> HashMap and 

> TreeMap 

           Are two types of Map implementations available in the Collections Framework. 

What is the difference between HashMap and TreeMap?

Answer:

The Basic diff are there :

> The HashMap Class : Its implements java.util.Map interface and uses hashing for storage. Indirectly Map uses Set functionality so, it does not permit duplicates. 

> The TreeMap Class : Its  implements java.util.Map interface and uses tree for storage. It provides the ordered map. 

Explain the functionality of Vector Class?

Answer:
Once array size is already set, We cannot change size of the array. To deal with this kind of situations in Java uses Vector, it grows and shrink it\'s size automatically. Vector allows to store only objects not primitives. To store primitives, convert primitives in to objects using wrapper classes before adding them into Vector.The Vector reallocates and resizes itself automatically. 

What does the following statement convey? Vector vt = new Vector(3, 10);

Answer:
vt is bassically an instance of Vector class with an initial capacity of 3 and grows in increment of 10 in each relocation .

How do we store a primitive data type within a Vector or other collections class?

Answer:
We need to wrap the primitive data type into one of the wrapper classes found in the java.lang package, like : > Integer, > Float, or > Double, as in: Integer in = new Integer(5); 

What is the difference between Vector and ArrayList?

Answer:
Vector and ArrayList are bassically very similar. Both of them represent a growable array. The main difference is that Vector is synchronized while ArrayList is not. 

How do I make an array larger?

Answer:
We cannot directly make an array larger. We must make a new (larger) array and copy the original elements into it,usually with System.arraycopy(). If we find yourself frequently doing this, the Vector class does this automatically for us, as long as our arrays are not of primitive data types. 

Which is faster, synchronizing a HashMap or using a Hashtable for thread-safe access?

Answer:
Because a synchronized HashMap requires an extra method call, a Hashtable is faster for synchronized access.

In which package would you find the interfaces amd claases defined in the Java Collection Framework

Answer:
java.util .

What method in the System class allows you to copy eleemnts from one array to another?

Answer:
System. arraycopy() 

When using the System.arraycopy() method,What exception is thrown if the destination array is smaller than the souce array?

Answer:
ArrayIndexOutofBoundsException 

What is the use of GregorianCalendar class?

Answer:
The GregorianCalendar class is bassically use for provides support for traditional Western calendars .

What is the use of SimpleTimeZone class?

Answer:
The SimpleTimeZone class is bassically use for provides support for a Gregorian calendar .

What is the use of ResourceBundle class

Answer:
The ResourceBundle class is bassically used to store locale-specific resources that can be loaded by a program to tailor the program\'s appearance to the particular locale in which it is being run. 

Which java.util classes and interfaces support event handling?

Answer:
The EventObject class and the EventListener interface support event processing.

What is the purpose of the enableEvents() method?

Answer:
The enableEvents() method is bassically used to enable an event for a particular object. Normally, an event is enabled when a listener is added to an object for a particular event. The enableEvents() method is used by objects that handle events by overriding their event-dispatch methods.

What is the difference between yielding and sleeping?

Answer:
When a task invokes its yield() method, it returns to the ready state. When a task invokes its sleep() method, it returns to the waiting state.

When a thread blocks on I/O, what state does it enter?

Answer:
A thread enters the waiting state when it blocks on I/O.

When a thread is created and started, what is its initial state?

Answer:
A Thread is in the ready state after it has been created and started.

What invokes a thread�=s run() method?

Answer:
After a thread is started, via its start() method or that of the Thread class, the JVM invokes the threads run() method when the thread is initially executed.

What method is invoked to cause an object to begin executing as a separate thread?

Answer:
The start() method of the Thread class is invoked to cause an object to begin executing as a separate thread.

What is the purpose of the wait(), notify(), and notifyAll() methods?

Answer:
The wait(),notify(), and notifyAll() methods are bassically used to provide an efficient way for threads to wait for a shared resource. When a thread executes an objects wait() method, it enters the waiting state. It only enters the ready state after another thread invokes the objects notify() or notifyAll() methods.

What happens when a thread cannot acquire a lock on an object

Answer:
If a thread attempts to execute a synchronized method or synchronized statement and is unable to acquire an objects lock, it enters the waiting state until the lock becomes available.

How does multithreading take place on a computer with a single CPU?

Answer:
The operating systems task scheduler allocates execution time to multiple tasks. By quickly switching between executing tasks, it creates the impression that tasks execute sequentially.

What happens when you invoke a threads interrupt method while it is sleeping or waiting?

Answer:
When a tasks interrupt() method is executed, the task enters the ready state. The next time the task enters the running state, an InterruptedException is thrown.

What state is a thread in when it is executing?

Answer:
An executing thread is in the running state.

What are three ways in which a thread can enter the waiting state?

Answer:
A Thread can enter the waiting state by invoking its sleep() method, by blocking on I/O, by unsuccessfully attempting to acquire an objects lock, or by invoking an objects wait() method. It can also enter the waiting state by invoking its (deprecated) suspend() method.

What method must be implemented by all threads?

Answer:
All tasks must implement the run() method, whether they are a subclass of Thread or implement the Runnable interface.

What are the two basic ways in which classes that can be run as threads may be defined?

Answer:
A thread class may be declared as a subclass of Thread, or it may implement the Runnable interface.

How can you store international / Unicode characters into a cookie?

Answer:
One way is, before storing the cookie URLEncode it. URLEnocder.encoder(str); And use URLDecoder.decode(str) when you get the stored cookie.