Interview Questions and Answers - Order By Newest Ans. A weak reference is a reference that gets removed on the next garbage collection cycle. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  weak reference  garbage collection Asked in 1 Companies   rare Q152. What are we doing here
list.stream().collect(Collectors.groupingBy(ClassA::getElement1,
Collectors.averagingDouble(ClassA::getelement2))); Core Java
Ans. We are averaging elements of a collection by element1 grouped by element1
* list is the reference of collection
* element1 is the member element returned by getElement1 method of ClassA
* element2 is the member element returned by getElement2 method of ClassA Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java 8  collections  java 8 streams  collectors Q153. What will the following code return
list.stream().collect(Collectors.groupingBy(ClassA::getElement1,
Collectors.averagingDouble(ClassA::getElement2))); Core Java
Ans. Map<DataType1,DataType2> where DataType1 is the data type of element1 ( returned by getelement1 method ) and DataType2 is the data type of element2 ( returned by getElement2 method ) Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java 8  collections  java 8 streams Q154. What is the difference between TreeSet and TreeMap ? Core Java
Ans. TreeSet contains only values as elements whereas TreeMap contains Key value pairs. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  reeMap  TreeSet  Collections  sorted collection Asked in 1 Companies   rare Q155. How to sort objects based on one of the field ? Core Java
Ans. Using comparable and comparator and sorted collections like TreeSet or TreeMap.
or
use stream api from java 8 onwards which internally refers to comparable and comparator through lambda expressions Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  sort  sorting  comprator  comparable  treeset  treemap  sorting collections Asked in 1 Companies Basic   frequent Q156. Which memory segment is cleaned by Garbage Collection - stack or heap ? Core Java
Ans. Heap as objects are stored ink heap. 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 Q157. What are the different ways to avoid multi Threading related problems in Java ? Core Java
Ans. Synchronization,
Concurrent classes,
Volatile keyword,
Implementing concurrent Lock interface,
Immutable classes Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  multithreading  threads  ways to avoid thread related problems  synchronization  volatile  concurrent collections Intermediate Q158. Why Concurrent Collection Classes are fail-fast in Java ? Core Java
This question is still unanswered. Can you please provide an answer. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  concurrent collection classes   fail fast   fail-fast  collections  collection classes expert Q159. Does Java SE has Immutable collections ? Core Java
Ans. Yes wef from Java 9 Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java 9  java9  immutable  immutability collections Q160. Isnt the use of HashTable and ConcurrentHashMap the same, i.e providing synchronized map collection ? Core Java
Ans. Both provide the thread safety but the actual difference come when talk about performance. CHM gives best performance when no of writer threads are less in number. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  synchronized collections  hashtable  ConcurrentHashMap Asked in 1 Companies 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