100% found this document useful (3 votes)
253 views6 pages

16 Questions Related To Collection

The document discusses questions related to Java collections. It provides answers about the differences between various collection types like ArrayList, LinkedList, HashMap, HashSet, TreeMap etc. It also discusses similarities and differences between collections like differences between Array and ArrayList, differences between add() and addAll() methods.

Uploaded by

Prashant kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (3 votes)
253 views6 pages

16 Questions Related To Collection

The document discusses questions related to Java collections. It provides answers about the differences between various collection types like ArrayList, LinkedList, HashMap, HashSet, TreeMap etc. It also discusses similarities and differences between collections like differences between Array and ArrayList, differences between add() and addAll() methods.

Uploaded by

Prashant kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Questions related to Collection

1. What is Collection and what is the difference between Collection and Collections?

Ans:
Collection is a group of similar types of objects.it is a predefined interface present
in java.util package.

2. What are the different Collection in JAVA?

Ans:
The following are the collections available in java
1. ArrayList.
2. LinkedList.
3. Stack.
4. Queue.
5. HashMap
6. HashSet.
7. Vector.
8. TreeMap.

3. What is the similarity between ArrayList and LinkedList?

Ans:
The similarities between ArrayList and LinkedList is both are “Dynamic “in nature.

4. What is the difference between ArrayList and LinkedList?

Ans:
Dissimilarities between ArrayList and LinkedList is ArrayList is “Continuous”
where LinkedList is “Discontinuous”.

5. What is the dissimilarities between Array and ArrayList?


Ans – Dissimilarities between Array and ArrayList is Array is “Static”
in nature where ArrayList is “Dynamic” in nature.

6. What is the similarities between Array and ArrayList?

Ans:
The similarities between Array and ArrayList is both are continuous /sequential in
nature.

7. Why manipulation slows in case of ArrayList class?

Ans:
Because a lot of shifting needs to be occurred.

8. Who is faster between Array and ArrayList& Why?

Ans:
Among Array And ArrayList Array is faster because ArrayList is dynamic in nature
means its size can be expanded sequentially.

9. Who is slower among Array, ArrayList&LinkedList?

Ans:
ArrayList always “slower” as comparision to Array and LinkedList.

10. Why manipulation fast in case of LinkedList class?

Ans:
Because no shifting needs to be occurred.

11. What are the ways to iterate the elements of collection?

Ans:
There are 2 ways to iterate the
i) by Iterator interface.
ii) by for-each loop.

12. State difference between Boolean add() & Boolean addAll()?

Ans:
Boolean add() used to insert an element in the collection Where Boolean addAll()
used to insert the specified collection element in the invoking collection.

13. State difference between Boolean remove() & Boolean removeAll()?

Ans:
Boolean remove() used to delete an element from the collection. Where Boolean
removeAll() used to delete all the elements of specified collection from the
invoking collection.

14. State the use of Boolean retainAll()?

Ans:
It is use to delete all the elements of invoking collection except the specified
collection.

15. State the use of intsize()?

Ans:
It returns the total no of elements in the collections.

16. State the difference between intsize() & void clear()?

Ans:
int size() retuens the total no of elements in the collection. Where void clear()
removes the total no. of element from the collection.

14. What is the difference between ArrayList and Vector?

Ans:
The difference between ArrayList and Vector is
i) ArrayList is not synchronized so it’s not suitable for use in multi-thread
environment. WHERE Vector is synchronized so suitable for use in multi-
thread environment.

ii) ArrayList is not synchronized but faster thenvector WHERE Vector is


synchronized and thread-safe it pays price of synchronization which make
it little slow.

15. What is the similarity between ArrayList and Vector?

Ans:
1. ArrayList and Vector are index based and backed up by an array internally.
2. ArrayList andVector maintains the insertion order of element .
3. ArrayList and Vector allows null and duplicate.

16. What is the difference between TreeSet and TreeMap?

Ans:
1. Major difference between TreeSet and TreeMap is that TreeSet implements
set interface while TreeMap implements Mapinterface in Java.
2. Second difference between TreeMap and TreeSet is the way they store
objects. TreeSet stores only one object while TreeMap uses two objects called
key and Value. Objects in TreeSet are sorted while keys in TreeMap remain in
sorted Order.
3. Third difference between TreeSet and TreeMap is that, former
implements NavigableSet while later implementsNavigableMap in Java.
4. Fourth difference is that duplicate objects are not allowed in TreeSet but
duplicates values are allowed in TreeMap.

17. What is the difference between HashSet and HashTable?

Ans:

18. What is the difference between Queue and Deque?

Ans:
Queue is a linear list of elements. With a queue, deletions can take place only one
end called front, and insertion can take place only the other hand called
rear. deQueue is a linear list of elements.
With a dequeue, added and removed at either end but not in the middle.

19. What is the difference between HashMap and HashTable?

Ans:
Hash map is not synchronized. it contain one null key and multiple null values.
Where Hash table is synchronized .it cannot contain any null key or null value.

20. What are advantages of Collection?

Ans:
1. We need not to learn multiple ad hoc collection APIs.
2. It provides a standard interface for collections that fosters software reuse and
also provides algorithms to manipulate them.
3. Reduces the effort required to design and implement APIs by eliminating the
need to produce ad hoc collections APIs.
4. It provides useful data structures and algorithms that reduces programming
effort due to which we need not to write them ourselves.
5. It provides high-performance implementations of useful data structures and
algorithms that increases the performance.
6. Helps in establishing a common language to pass collections back and forth that
provides interoperability between unrelated APIs.
7. Collection is resizable and can grow.

21. What are the ways traverse Collection objects?

Ans:
There are three ways to traverse collections:
1. using aggregate operations
2. with the for-each construct and
3. by using Iterator.

22. What is Iterator Interface?

Ans:
Iterator Interface provides the facility of iterating the element in forward direction.

23. Why deletion in LinkedList is fast than ArrayList?

Ans:
Deletion in linked list is fast because it involves only updating the next pointer in the
node before the deleted node and updating the previous pointer in the node after the
deleted node.

24. Which two method you need to implement for key Object in HashMap?

Ans:
The two method are 1.Map interface 2.AbstractMap class.

25. How do you decide when to use ArrayList and LinkedList?


Ans:

26. What will happen if we put a key object in a HashMap which is already there?

Ans:
HashMap never contains any duplicate key so it will replace with the old key.

27. What is use of ListIterator interface?

Ans:
It is use to traverse the element in background and forward direction.

28. What is List interface?

Ans:
List interface is the sub interface of collection, which contains methods to insert and
delete elements in index basis.

29. What is vector class?

Ans:
Creating a vector is nothing but creating an object of java.util.Vectorclass.Vectorecah
time double its size,whileArrayList grows 50% of its size each time.

30. What is priorityQueue class?

Ans:
The priorityQueue class provides the facility of using queue.it does not orders the
elements in FIFO order.

31. What is Deque class?

Ans:
Deque is a linear collection that supports element insertion and removal at both
ends. The java.util.Deque interface is a subtype of the java.util.Queue interface
.Deque is short for “Double ended Queue”.

32. What is ArrayDeque?

Ans:
ArrayDeque implements Deque interface.it available from jdk1.6.It is not thread
safe.It allows unlimited insertion of elements.

33. Difference between HashMap and TreeMap ?

Ans:
1. HashMap implements the Map interface WHERE TreeMap implements
NavigableMap interface
2. HashMap may have one null key WHERE TreeMap cannot have null key.
3. HashMap maintains no order WHERE TreeMap maintains ascending order.

34. Common algorithms list implemented Collections Framework ?


Ans:
Java Collections Framework also provided polymorphic algorithms to operate on the
collections. These are all reusable piece of functionality provided to make job easier
when working with collections. All operation as usual defied static to call directly from
Collections class. Below are algorithms list:
I. Sorting
II. Shuffling
III. Routine Data Manipulation
IV. Searching
V. Composition
VI. Finding Extreme Values

35. How you create read only collection?

Ans:
Read only List means a List where you cannot perform modification operations like add, remove or set.
You can only read from the List by using get method or by using Iterator of List.

36. What is the difference between List and set?

Ans:
List can contain duplicate elements whereas set contains unique elements only.

37. Why insertion and deletion in ArrayList slow compared to LinkedList?

Ans:
Because a lot of shifting has to be done internally.

38. Why Generics are used in Java?

Ans:
Using a parameterized type such as LinkedList<String> , instead of LinkedList ,
enables the compiler to perform more type checks and requires fewer dynamic casts.
This way errors are detected earlier, in the sense that they are reported at compile-
time by means of a compiler error message rather than at runtime by means of an
exception.

39. Is Iterator a Class or Interface? What is its use?

Ans:
Iterator is a interface which iterating the element in forward direction.

You might also like