Core Java - Interview Questions and Answers for 'Collection' | Search Interview Question - javasearch.buggybread.com
Javasearch.buggybread.com

Search Interview Questions


 More than 3000 questions in repository.
 There are more than 900 unanswered questions.
Click here and help us by providing the answer.
 Have a video suggestion.
Click Correct / Improve and please let us know.
Label / Company      Label / Company / Text

   



Interview Questions and Answers - Order By Newest

   next 30
 Q41. What will be the output of this code ?

Set mySet = new HashSet();
mySet.add("4567");
mySet.add("5678");
mySet.add("6789");
for(String s: mySet){
System.out.println(s);
}
Ans. It will print 4567,5678 and 6789 but Order cannot be predicted.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   set   hashset   coding   code

Try 1 Question(s) Test


 Q42. What will be the output of this code ?

Set mySet = new HashSet();
mySet.add("4567");
mySet.add("5678");
mySet.add("6789");
System.out.println(s.get(0));
Ans. This will give compile time error as we cannot retrieve the element from a specified index using Set. Set doesn't maintain elements in any order.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   set   hashset   coding   code

Try 1 Question(s) Test


 Q43. Which of the following collection maintain its elements in Natural Sorted order ?

a. HashMap
b. TreeMap
c. LinkedHashMap
d. LinkedMap
Ans. TreeMap

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     treemap   collections   java   sorted collections   map


 Q44. If we try to add duplicate key to the HashMap, What will happen ?

a. It will throw an exception.
b. It won't add the new Element without any exception.
c. The new element will replace the existing element.
d. Compiler will identify the problem and will throw an error.
Ans. The new element will replace the existing element.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     hashmap   map   collections   java


 Q45. Which type of memory is cleaned / recovered by garbage collection - stack or heap ?Core Java
Ans. Heap memory.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     memory management  stack  heap  garbage collection     Asked in 2 Companies


 Q46. Java doesn't provide exclusive access to memory like C/C++ and other lower level languages ? What are the advantanges and disadvantages ?Core Java
Ans. Yes, doesn't provide exclusive access as we cannot allocate and deallocate memory exclusively as Java internally manages it. The advantage of this is that it relieves the coder for such tasks and helps protect from many bugs that may get introduced with imperfect coding. Moreover as java garbage collector collects all unclaimed memory or objects, it helps the application from memory leaks.

On the flip side , as coder doesn't have extensive excess to memory , it is upto java to decide on the state for programming construct and data storage and hence may introduce some security risks. For example - Java keeps string literals in string pool and there is no exclusive way to remove it and hence may stay and sensitive data in string pool may introduce security issues. Moreover when we overwrite a value or object for a variable / reference, it is upto java to purge those values and hence it may stay in memory for a while till java decide that it is no longer referenced and hence should be removed and hence makes it vulnerable for inappropriate access.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     disadvantages of garbage collection  advantages and disadvantages of java memory management  java for security applications  java with sensitive data  memory management


 Q47. What is meant by "Vector is synchronized and ArrayList isn't " ?Core Java
Ans. It means that only 1 thread can access have access to Vector at a time and no parallel access is allowed whereas Array List allows parallel access by multiple threads.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     vectors  arraylist  collections  list  synchronization  synchronized


 Q48. Which collection stores element in insertion order ?Core Java
Ans. LinkedList maintains the insertion order in collection.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     collections     Asked in 1 Companies


 Q49. Which interface does java.util.Hashtable implement?Core Java
Ans. Java.util.Map

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   hashtable   map      basic        rare


Frequently asked.
  Q50. What is an Iterator?Core Java
Ans. Iterator is an interface that provides methods to iterate over any Collection.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   iterator     Asked in 11 Companies      basic        frequent


 Q51. Which interface provides the capability to store objects using a key-value pair?Core Java
Ans. java.util.map

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   map      basic        frequent


 Q52. What is a finalize method ?Core Java
Ans. finalize() method is called just before an object is destroyed.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   oops   finalize   garbage collection   basic interview question     Asked in 1 Companies      basic        frequent

Try 1 Question(s) Test


Usually asked only to fresh graduates and less experienced developers.
  Q53. What is a daemon thread? Give an Example ?
Ans. These are threads that normally run at a low priority and provide a basic service to a program or programs when activity on a machine is reduced. garbage collector thread is daemon thread.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   threads   multi threading   operating system   daemon thread   garbage collection     Asked in 10 Companies      intermediate        frequent

Try 2 Question(s) Test


  Q54. What is Comparable Interface?Core Java
Ans. It is used to sort collections and arrays of objects using the collections.sort() and java.utils. The objects of the class implementing the Comparable interface can be ordered.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   comparable interface     Asked in 7 Companies      intermediate        frequent

Try 1 Question(s) Test


 Q55. Explain Set Interface?Core Java
Ans. It is a collection of element which cannot contain duplicate elements. The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   set interface   set


 Q56. What is the difference between the size and capacity of a Vector?Core Java
Ans. 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.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   vectors


 Q57. Can we use Ordered Set for performing Binary Search ?
Ans. We need to access values on the basis of an index in Binary search which is not possible with Sets.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   sets   set   collections   binary search


 Q58. Which Java collection class can be used to maintain the entries in the order in which they were last accessed?
Ans. LinkedHashMap

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   hashmap   linkedhashmap


 Q59. Is it legal to initialize List like this ?


LinkedList l=new LinkedList();
Ans. No, Generic parameters cannot be primitives.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   list   linkedlist   generics   yes-no


 Q60. Which of the following syntax are correct ?a. LinkedList<Integer> l=new LinkedList<int>();b. List<Integer> l=new LinkedList<int>();c. LinkedList<Integer> l=new LinkedList<Integer>();d. List<Integer> l = new LinkedList<Integer>();Core Java
Ans. All are correct.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   generics   linkedlist   list   collections      basic


Very Frequently asked. Favorite question in walkins and telephonic interviews. Usually among first few questions. Asked in different variants. Must know for intermediate and expert professionals.Among Top 10 frequently asked questions.
  Q61. What is rule regarding overriding equals and hashCode method ?Core Java
Ans. A Class must override the hashCode method if its overriding the equals method.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   hashcode  hash code   equals   collections     Asked in 44 Companies      intermediate        frequent

Try 1 Question(s) Test


 Q62. What is the difference between Collection and Collections ?Core Java
Ans. Collection is an interface whereas Collections is a utility class.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections     Asked in 1 Companies


 Q63. How can we reverse the order in the TreeMap ?
Ans. Using Collections.reverseOrder()

Map tree = new TreeMap(Collections.reverseOrder());

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   map   treemap

Try 1 Question(s) Test


 Q64. TreeMap orders the elements on which field ?
Ans. Keys

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   map   treemap


 Q65. How TreeMap orders the elements if the Key is a String ?
Ans. As String implements Comparable, It refers to the String compareTo method to identify the order relationship among those elements.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   comparable interface   treemap   compareto


 Q66. Can we add heterogeneous elements into TreeMap ?
Ans. No, Sorted collections don't allow addition of heterogeneous elements as they are not comparable.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   comparable interface   treemap


 Q67. Will it create any problem if We add elements with key as user defined object into the TreeMap ?
Ans. It won't create any problem if the objects are comparable i.e we have that class implementing Comparable interface.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   treemap   comparable interface


  Q68. Can we have null keys in TreeMap ?Core Java
Ans. No, results in exception.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   treemap     Asked in 6 Companies      Basic        frequent


 Q69. Can value be null in TreeMap ?Core Java
Ans. Yes.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   treemap   yes-no


 Q70. Which interface TreeMap implements ?
Ans. TreeMap implements NavigableMap, SortedMap, Serializable and Clonable.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   treemap


previous 30   next 30

Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked: