Search Interview Questions | ![]() ![]() Click here and help us by providing the answer. ![]() Click Correct / Improve and please let us know. |
|
| ||||
Interview Questions and Answers - Order By Rating | ||||
![]() ![]() | ||||
| ||||
Ans. Without composition - No, as it's the core of dependency injection. With Inheritance - Yes, through interface implementation. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. Yes, Runtime Polymprohism requires either Class inheritance or Interface implementation. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. Inheritance , Runtime Polymorphism and Composition. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. set1.stream().filter(p->p.element1.equals("Hello")).findAny().isPreent(); or set1.stream().map(A::getElement1).anyMatch("Hello"::equals) | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. Step 1 - We can create a Registry Class having Map of all created objects as key and References list as value. Step 2 - Whenever we create an object , we should update the Registry in the constructor to add the new object. Step 3 - Whenever we assign a new reference to the object , we need to update the entry in Map. Similarly if the reference get's removed ( end of scope etc ), we need to remove the entry of reference from the list. Step 4 - We can have threaded code to monitor the Map to see if any object looses all it's references and should call the method to destroy object and clean the memory. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. Loose Coupling Facilitates designing complex applications by making them modular. | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
| ||||
Ans. Generics are a facility of generic programming that were added to the Java programming language in 2004 within J2SE5.0. They allow "a type or method to operate on objects of various types while providing compile-time type safety. | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
| ||||
Ans. Interfaces don't have member elements and method definitions that could cause diamond problem. With Java 8, Interfaces have default method definitions. This could have created diamond problem but Java introduced a compile time check for "duplicate default methods" in case same method is derived from multiple interfaces and no definition is overridden by the class. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. Because when we say y = x, the value of x is copied to another memory location and then y points to that new memory location. Unlike object references that once assigned point to same object in memory, value is copied in case of variable assignment. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. final when assigned to object references doesn't make them immutable. It means that the references cannot be de-referenced. final when applied to variables means that the value cannot be changed. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. final keyword have meaning only to referenced and not the value. It means that the specified reference cannot be dereferenced. It doesn't control the value assigned to the memory that's being referenced. This is the reason that final object references doesn't mean that the object is immutable but means that the reference cannot be changed to point to new object. In case of primitive types too, when we assign a reference to another, values are passed and not the object reference, and hence a new placeholder is created in memory with the same value. That is why final to that context means that you cannot change the assigned memory and there is no way we can have that memory place have another value. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. No but we have it in StringBuilder. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. public class LinkedList { Node start = null; Node head = null; class Node { Integer body; Node nextNode; Node(Integer value) { body = value; } } private void addNodeToEnd(Integer value) { if (start == null) { start = new Node(value); head = start; head.nextNode = null; return; } while (head.nextNode != null) { head = head.nextNode; } head.nextNode = new Node(value); } private void deleteNode(Integer value) { head = start; while (head.nextNode != null) { if(head.nextNode.body == value){ head.nextNode = head.nextNode.nextNode; } head = head.nextNode; } } private void traverse() { head = start; while (head != null) { System.out.println(head.body); head = head.nextNode; } } public static void main(String[] args) { LinkedList ll = new LinkedList(); ll.addNodeToEnd(5); ll.addNodeToEnd(10); ll.addNodeToEnd(15); ll.traverse(); ll.deleteNode(10); ll.traverse(); } } | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. public class LinkedList { Node start = null; Node head = null; class Node{ Integer body; Node nextNode; Node(Integer value){ body = value; } } private void insertInMiddle(Integer value){ head = start; if(start == null) { start = new Node(value); head = start; head.nextNode = null; return; } while(head.body < value){ if(head.nextNode == null || head.nextNode.body >= value){ Node newNode = new Node(value); newNode.nextNode = head.nextNode; head.nextNode = newNode; break; } head = head.nextNode; } } private void traverse(){ head = start; while(head != null){ System.out.println(head.body); head = head.nextNode; } } public static void main(String[] args){ LinkedList ll = new LinkedList(); ll.insertInMiddle(5); ll.insertInMiddle(10); ll.insertInMiddle(15); ll.insertInMiddle(7); ll.traverse(); } } | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. public class LinkedList { Node start = null; Node head = null; class Node { Integer body; Node nextNode; Node(Integer value) { body = value; } } private void addNodeToEnd(Integer value) { if (start == null) { start = new Node(value); head = start; head.nextNode = null; return; } while (head.nextNode != null) { head = head.nextNode; } head.nextNode = new Node(value); } private void traverse() { head = start; while (head != null) { System.out.println(head.body); head = head.nextNode; } } public static void main(String[] args) { LinkedList ll = new LinkedList(); ll.addNodeToEnd(5); ll.addNodeToEnd(10); ll.addNodeToEnd(15); ll.traverse(); } } | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. Each has it's own advantages Batching requires less resources but may result in loosing whole batch in case of failure whereas concurrency even though is little more expensive in terms of resources but would result in minimal loss in case of failure. In case messages are to be consumed in a particular order, batching them in that order and then consuming them makes better sense. if incoming messages are not continuous , it makes more sense to do concurrency as we need not wait for all messages to form a batch and flush. Though time sensitivity can be added but that would add unnecessary complexity. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. if statements nested within another if or else blocks are called nested if statements. if(condition1) { if(condition2){ } } | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
![]() | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. 5.0 operation between two ints generate int only. so 7/5 generates 1 and not 1.4. Multiplication between int and float generates float and hence 1 * 5.0 = 5.0 | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. never initialise local to null,yes it is bad pratice | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. We can have duplicate keys within MultiMap whereas we cannot have duplicate keys within Java Util Map. We can have a Map with value as a "collection of values" instead of single value to have a similar function as Multimap. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. return is used within a method to return control out of the method. It may be followed by a value which is returned from the method to calling method immediately preceding the point of call. continue statement is used within a loop to start next iteration without executing the remaining statements in the loop. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. It will result in never ending loop | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. 1 -> 2 -> 4 -> 3 -> 5 -> 6 | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. 1 -> 2 -> 3 -> 6 -> 4 - > 5 | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. In Breadth first algorithm, all the adjacent nodes of the starting node is visited first and then the same rule is followed while moving inwards whereas In Depth first algorithm, all the nodes of a single traversal path are visited first till a cycle or an end is found. For example , given the following entries of adjacent nodes 1,2 1,3 1,6 2,4 2,5 3,6 The Breadth first path would be 1,2,3,6,4,5 and Depth first path would be 1,2,4,5,3,6 | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
| ||||
Ans. Initialize an ArrayList that will hold LinkedLists i.e every element of the arraylist will be a linked list. Such collection could be used in algorithms that require first random access and then sequential traversal. For example - Storing traversal paths for a graph wherein we can start from any vertex. Implementing dictionary with each arraylist element holding staring with character and then linked list holding duplicate words. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. Strings are immutable in Java and not final. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. Being final in the sense of objects or object reference means that the reference cannot be reassigned to a different object whereas being immutable means that the object contents cannot be changed. Strings in Java are immutable. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. Yes, it will compile without error. final in this context means that the reference hm cannot be assigned to a new hash map but didn't restrict from changing the state of collection already held by hm. | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
![]() ![]() | ||||