Interview Questions and Answers - Order By Newest Q161. What will happen if there are no collections in java ? Core Java
Ans. Collections in Java is nothing but a library implementation for data structures and algorithm. If it's not available , we might have to include some other library or provide our own implementation. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  collections Q162. If you override hashcode to always return true, how a hash based collection will behave? Core Java
Ans. At that bucket, it will form a linked list depending on what equals method evaluates for that object. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  hashcode  collections Asked in 2 Companies Q163. Difference between List and ArrayList ? Core Java
Ans. List is an interface whereas ArrayList is an implementation of List. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  list  arraylist  list vs arraylist  difference between  collections Asked in 2 Companies basic Q164. difference between ArrayList and array ? Core Java
Ans. ArrayList is a variable length collection class whereas arrays are fixed length primitive structure.
We can use generics with arraylist but not with arrays.
We can store primitive data types within arrays but can't with ArrayList. In ArrayList that needs to be converted to Wrapper objects.
Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  arraylist  arrays  collection classes  collections basic   frequent Q165. Can we override behavior of a collection class ? How ? Core Java
Ans. Yes. We can do that.
1. We can create own own implementation class extending the collection class and then override the behavior method.
2. We can override the behavior at the time of instantiation of class as following
List<MyType> list = new ArrayList<MyType>() {
public boolean add(MyType mt) {
super.add(mt);
Collections.sort(list, comparator);
return true;
}
}; Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  collections Q166. Write a program to make an array act as a set. Core Java
Ans. public Set convertArrayToList(T array[]) {
Set set = new HashSet<>(Arrays.asList(array));
return set;
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  arrays  collections set Asked in 1 Companies Q167. How can we create an arraylist of unique values ? Core Java
Ans. We can put the value in a set to enforce uniqueness and then dum those value into an arraylist. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  arraylist  design  collections Q168. What is the different between collection and Stream Api ? Core Java
Ans. data under collection are actually stored in memory so that they can be retrieved when needed whereas data in streams are not stored and hence we need to construct it again when needed. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  collection classes  stream api  collection vs stream Asked in 1 Companies a. interface b. abstract class c. final class d. util classAns.d. util class
Q170. What will be the output of exceuting main method ?
public static void main(String[] args){
List list = new ArrayList();
list.add(1);
list.add(2);
list.add(3);
System.out.println(list);
} Core Java
a. 1,2,3 b. Order cannot be determined c. compilation error d. 3,2,1Ans.a. 1,2,3
Q171. What will be the output upon executing main method ?
public static void main(String[] args){
Set set = new HashSet();
set.add(1);
set.add(2);
set.add(3);
System.out.println(set);
} Core Java
a. 1,2,3 b. Order cannot be determined c. Compilation error d. 3,2,1Ans.b. Order cannot be determined
Q172. Which of the following methods are used by Java Garbage Collection Mechanism ? Core Java
a. final b. finally c. finalize d. All of the aboveAns.c. finalize
Q173. Which of the following about Garbage collection is false ? Core Java
a. We can call Garbage collection explicitly b. Garbage Collection guarantees that the application will not run out of memory c. finalize method is used by Java for Garbage Collection d. Garbage Collection Mechanism delete unclaimed objects that are no longer requiredAns.b. Garbage Collection guarantees that the application will not run out of memory
Q174. Which of the following collections stores its elements in natural sorting order ? Core Java
a. HashMap b. LinkedHashMap c. TreeMap d. EnumMapAns.c. TreeMap
Q175. Which of following stores its elements in natural Sorting Order ? Core Java
a. AbstractSet b. HashSet c. LinkedHashSet d. TreeSetAns.d. TreeSet
Q176. What kind of thread is Garbage collection thread ? Core Java
a. Daemon Thread b. User Thread c. System Thread d. Active ThreadAns.a. Daemon Thread
Q177. What will be the output of this code
ArrayList list = new LinkedList();
list.add(3);
list.add(2);
list.add(1);
System.out.println(list); Core Java
a. 1,2,3 b. 3,2,1 c. Order cannot be determined d. Compilation ErrorAns.d. Compilation Error
Q178. Which of following memory segment is cleaned by Garbage Collection Mechanism ? Core Java
a. Stack b. Heap c. Code d. CacheAns.b. Heap
Q179. Which of the following collection classes store their elements as Key Value pairs ? Core Java
a. Set b. List c. Map d. QueueAns.c. Map