Core Java Interview Question Answer
This is a novel series of sharing meat Java interview enquiry together with reply on Finance domain together with to a greater extent than oft than non on big Investment bank.Many of these Java interview questions are asked on JP Morgan, Morgan Stanley, Barclays or Goldman Sachs. Banks to a greater extent than oft than non asked meat Java interview questions from multi-threading, collection, serialization, coding together with OOPS blueprint principles. Anybody who is preparing for whatever Java developer Interview on whatever Investment depository fiscal establishment tin ship away hold out benefited from these laid of meat Java Interview questions together with answers. I receive got collected these Java questions from my friends together with I idea to part alongside you lot all.
I hope this volition hold out helpful for both of us. It's also beneficial to practise to a greater extent than or less programming interview questions because, inward almost all Java interview, at that topographic point are at to the lowest degree 1 or 2 coding questions appear. Please part answers for unanswered Java interview questions together with allow us know how skilful these Java interview questions are?
If you lot are seriously preparing for Java Interviews together with aid interviews, hence I also advise taking a aspect at Java Programming Interviews Exposed past times Markham. This is the Java specific mass modeled on their before best seller, also most programming interviews. This mass contains questions non but from Java but also from related technology stack e.g. JUnit, Maven, Design Patterns, JVM Internals, Android together with Best practices. Questions are skilful together with answers are crisp together with real well explained, which makes it interesting read every bit well.
By the way apart from blogs together with articles, you lot tin ship away also convey payoff of to a greater extent than or less books, which are specially written for clearing whatever programming interviews together with to a greater extent than or less focused on Java programming, 2 books which comes inward minds are Cracking the Coding Interview together with Programming Interviews Exposed. Both books are focused on programming inward full general together with a lot of other related topics e.g. information structures, algorithms, database, SQL, networking together with behavioral questions, but also contains Java concepts.
Question 1: What’s incorrect using HashMap inward the multi-threaded environment? When get() method acquire to the infinite loop? (answer)
Well, zippo is wrong, it depending upon how you lot use. For example, if you lot initialize the HashMap past times but i thread together with hence all threads are solely reading from it, hence it's perfectly fine. One illustration of this is a Map which contains configuration properties. The existent job starts when at to the lowest degree i of that thread is updating HashMap i.e. adding, changing or removing whatever primal value pair. Since put() performance tin ship away displace re-sizing together with which tin ship away farther Pb to infinite loop, that's why either you lot should work Hashtable or ConcurrentHashMap, after is better.
Question 2. Does non overriding hashCode() method has whatever performance implication? (answer)
This is a skilful enquiry together with opened upward to all, every bit per my cognition a piteous hashcode component subdivision volition number inward the frequent collision inward HashMap which eventually growth the fourth dimension for adding an object into HashMap. From Java 8 onwards though collision volition non comport on performance every bit much every bit it does inward before versions because after a threshold the linked listing volition hold out replaced past times the binary tree, which volition give you lot O(logN) performance inward the worst instance every bit compared to O(n) of linked list.
This number is fixed inward Java 1.7, where master copy grapheme array is non referenced anymore, but that alter also made the creation of substring chip costly inward price of time. Earlier it was inward the attain of O(1), which could be O(n) in worst instance on Java 7.
Question 5: Can you lot write critical department code for the singleton? (answer)
This meat Java enquiry is a follow-up of the previous enquiry together with expecting the candidate to write Java singleton using double checked locking. Remember to work the volatile variable to brand Singleton thread-safe. Here is the code for critical department of a thread-safe Singleton pattern using double checked locking idiom:
There is a deviation when looking at exception handling. If your tasks throw an exception together with if it was submitted alongside executing this exception volition acquire to the uncaught exception handler (when you lot don't receive got provided i explicitly, the default i volition but impress the stack delineate to System.err). If you lot submitted the chore alongside submit whatever thrown exception, checked exception or not, is hence component subdivision of the task's render status. For a chore that was submitted alongside submitting together with that terminates alongside an exception, the Future.get() volition re-throw this exception, wrapped inward an ExecutionException.
Question 10: Can you lot write code for iterating over HashMap inward Java 4 together with Java 5? (answer)
Tricky i but he managed to write using piece together with a for loop. Actually, at that topographic point are 4 ways to iterate over whatever Map inward Java, i involves using keySet() together with iterating over primal together with hence using get() method to recollect values, which is chip expensive. Second method involves using entrySet() together with iterating over them either past times using for each loop or piece alongside Iterator.hasNext() method. This i is a amend approach because both primal together with value object are available to you lot during Iteration together with you lot don't demand to telephone yell upward get() method for retrieving the value, which could give the O(n) performance inward instance of huge linked listing at i bucket. See my postal service 4 ways to iterate over Map inward Java for detailed explanation together with code examples.
Question eleven : When do you lot override hashCode() together with equals()? (answer)
Whenever necessary particularly if you lot desire to do equality depository fiscal establishment check based upon line of piece of work organisation logic rather than object equality e.g. 2 employee object are equal if they receive got the same emp_id, despite the fact that they are 2 dissimilar objects, created past times dissimilar component subdivision of the code. Also overriding both these methods are must if you lot desire to work them every bit primal inward HashMap. Now every bit component subdivision of the equals-hashcode contract inward Java, when you lot override equals, you lot must override hashcode every bit well, otherwise your object volition not break invariant of classes e.g. Set, Map which relies on equals() method for functioning properly. You tin ship away also depository fiscal establishment check my postal service 5 tips on equals inward Java to empathize subtle number which tin ship away arise piece dealing alongside these 2 methods.
Question 12:. What volition hold out the job if you lot don't override hashCode() method? (answer)
If you lot don't override equals method, hence the contract betwixt equals together with hashcode volition non work, according to which, 2 objects which are equal to equals() must receive got the same hashcode. In this case, to a greater extent than or less other object may render dissimilar hashCode together with volition hold out stored inward that location, which breaks invariant of HashMap class because they are non supposed to allow duplicate keys. When you lot add together the object using put() method, it iterate through all Map.Entry object nowadays inward that bucket location, together with update value of the previous mapping, if Map already contains that key. This volition non piece of work if hashcode is non overridden.
Question xiii : Is it amend to synchronize critical department of getInstance() method or whole getInstance() method? (answer)
The reply is an solely critical section, because if nosotros lock the whole method that every fourth dimension person calls this method, it volition receive got to hold off fifty-fifty though nosotros are non creating whatever object. In other words, synchronization is solely needed, when you lot create an object, which happens solely once. Once an object has created, at that topographic point is no demand for whatever synchronization. In fact, that's real piteous coding inward price of performance, every bit synchronized method trim performance upward to 10 to 20 times. Here is UML diagram of Singleton pattern:
By the way, at that topographic point are several ways to create a thread-safe singleton inward Java, which you lot tin ship away also get upward every bit component subdivision of this enquiry or whatever follow-up.
Question 14: Where does equals() together with hashCode() method comes inward the movie during the get() operation? (answer)
This meat Java interview enquiry is a follow-up of previous Java enquiry together with the candidate should know that i time you lot get upward hashCode, people are most probable ask, how they are used inward HashMap. When you lot furnish a primal object, commencement it's hashcode method is called to calculate bucket location. Since a bucket may incorporate to a greater extent than than i entry every bit linked list, each of those Map.Entry object is evaluated past times using equals() method to come across if they incorporate the actual primal object or not.
Questions 15: How do you lot avoid deadlock inward Java? (answer)
You tin ship away avoid deadlock past times breaking circular hold off condition. In lodge to do that, you lot tin ship away brand organisation inward the code to impose the ordering on acquisition and liberate of locks. If lock volition hold out acquired inward a consistent lodge together with released inward but reverse order, at that topographic point would non hold out a province of affairs where i thread is holding a lock which is acquired by other together with vice-versa. See the detailed reply for the code illustration together with to a greater extent than detailed explanation.
Question 16: What is the deviation betwixt creating String every bit new() together with literal? (answer)
When nosotros create the string with new() Operator, it’s created inward heap together with non added into string puddle piece String created using literal are created inward String puddle itself which exists in PermGen expanse of heap.
does non pose the object str inward String pool, nosotros demand to call String.intern() method which is used to pose them into String puddle explicitly. It's solely when you lot create String object every bit String literal e.g. String s = "Test" Java automatically pose that into String pool. By the way at that topographic point is a grab hither Since nosotros are passing arguments every bit "Test", which is a String literal, it volition also create to a greater extent than or less other object every bit "Test" on string pool. This is the i point, which has gone unnoticed until knowledgeable readers of weblog suggested it. To larn to a greater extent than most the deviation betwixt String literal together with String object, see this article.
Here is a overnice picture which shows this deviation quite well:
Question 17: What is Immutable Object? Can you lot write Immutable Class? (answer)
Immutable classes are Java classes whose objects tin ship away non hold out modified i time created. Any alteration inward Immutable object results inward the novel object. For example, String is immutable inward Java. Mostly Immutable classes are also in conclusion inward Java, inward lodge to forestall subclasses from overriding methods, which tin ship away compromise Immutability. You tin ship away accomplish the same functionality past times making fellow member every bit non-final but private together with non modifying them except inward constructor.
Apart from obvious, you lot also demand to brand certain that, you lot should non let on the internals of Immutable object, particularly if it contains a mutable member. Similarly, when you lot convey the value for the mutable fellow member from customer e.g. java.util.Date, use clone() method keep a divide re-create for yourself, to forestall the gamble of malicious customer modifying mutable reference after setting it.
The Same precaution needs to hold out taken piece returning value to a mutable member, render to a greater extent than or less other divide re-create to the client, never render master copy reference held past times Immutable class. You tin ship away come across my post How to create an Immutable class inward Java for measuring past times measuring guide together with code examples.
Question 18: Give the simplest way to uncovering out the fourth dimension a method takes for execution without using whatever profiling tool? (answer)
Read the scheme fourth dimension but before the method is invoked together with instantly after method returns. Take the fourth dimension difference, which volition give you lot the fourth dimension taken past times a method for execution.
To pose it inward code…
Remember that if the fourth dimension taken for execution is likewise small, it mightiness present that it is taking null milliseconds for execution. Try it on a method which is big enough, inward the sense the i which is doing considerable sum of processing
Question 19: Which 2 method you lot demand to implement to work an Object every bit primal inward HashMap? (answer)
In lodge to work whatever object every bit Key inward HashMap or Hashtable, it must implement equals and hashcode method in Java. Read How HashMap industrial plant inward Java for a detailed explanation on how equals and hashcode method is used to pose together with acquire an object from HashMap.
Question 20: How would you lot forestall a customer from direct instantiating your concrete classes? For example, you lot receive got a Cache interface together with 2 implementation classes MemoryCache together with DiskCache, How do you lot ensure at that topographic point is no object of this 2 classes is created past times customer using new() keyword.
I exit this question for you lot to practise together with think most before I give the answer. I am certain you lot tin ship away figure out the correct way to do this, every bit this is i of the of import determination to continue command of classes inward your hand, slap-up from a maintenance perspective.
I am also real grateful to my readers who receive got generously contributed several skilful questions from Java Interviews for both beginners together with experienced developers alike. I receive got already answered many of these questions inward this weblog together with you lot tin ship away easily uncovering a relevant postal service past times using the search box at the top correct corner of this page.
More questions:
133+ Java Interview Questions from Last v years (read here)
50+ Java Multithreading Questions from Last iii years (see here)
50+ Programmer Phone Interview Questions alongside answers (link)
This is a novel series of sharing meat Java interview enquiry together with reply on Finance domain together with to a greater extent than oft than non on big Investment bank.Many of these Java interview questions are asked on JP Morgan, Morgan Stanley, Barclays or Goldman Sachs. Banks to a greater extent than oft than non asked meat Java interview questions from multi-threading, collection, serialization, coding together with OOPS blueprint principles. Anybody who is preparing for whatever Java developer Interview on whatever Investment depository fiscal establishment tin ship away hold out benefited from these laid of meat Java Interview questions together with answers. I receive got collected these Java questions from my friends together with I idea to part alongside you lot all.
I hope this volition hold out helpful for both of us. It's also beneficial to practise to a greater extent than or less programming interview questions because, inward almost all Java interview, at that topographic point are at to the lowest degree 1 or 2 coding questions appear. Please part answers for unanswered Java interview questions together with allow us know how skilful these Java interview questions are?
If you lot are seriously preparing for Java Interviews together with aid interviews, hence I also advise taking a aspect at Java Programming Interviews Exposed past times Markham. This is the Java specific mass modeled on their before best seller, also most programming interviews. This mass contains questions non but from Java but also from related technology stack e.g. JUnit, Maven, Design Patterns, JVM Internals, Android together with Best practices. Questions are skilful together with answers are crisp together with real well explained, which makes it interesting read every bit well.
20+ Core Java Interview Questions Answers
These Java interview questions are the mix of easy, tough together with tricky Java questions e.g. Why multiple inheritance is non supported inward Java is i of the tricky questions inward java. Most questions are asked on Senior together with experienced marker i.e. 3, 4, v or vi years of Java sense e.g. How HashMap industrial plant inward Java, which is most pop on experienced Java interviews. By the way, late I was looking at answers together with comments made on Java interview questions given inward this postal service together with I flora to a greater extent than or less of them quite useful to include into the primary postal service to do goodness all.By the way apart from blogs together with articles, you lot tin ship away also convey payoff of to a greater extent than or less books, which are specially written for clearing whatever programming interviews together with to a greater extent than or less focused on Java programming, 2 books which comes inward minds are Cracking the Coding Interview together with Programming Interviews Exposed. Both books are focused on programming inward full general together with a lot of other related topics e.g. information structures, algorithms, database, SQL, networking together with behavioral questions, but also contains Java concepts.
Question 1: What’s incorrect using HashMap inward the multi-threaded environment? When get() method acquire to the infinite loop? (answer)
Well, zippo is wrong, it depending upon how you lot use. For example, if you lot initialize the HashMap past times but i thread together with hence all threads are solely reading from it, hence it's perfectly fine. One illustration of this is a Map which contains configuration properties. The existent job starts when at to the lowest degree i of that thread is updating HashMap i.e. adding, changing or removing whatever primal value pair. Since put() performance tin ship away displace re-sizing together with which tin ship away farther Pb to infinite loop, that's why either you lot should work Hashtable or ConcurrentHashMap, after is better.
Question 2. Does non overriding hashCode() method has whatever performance implication? (answer)
This is a skilful enquiry together with opened upward to all, every bit per my cognition a piteous hashcode component subdivision volition number inward the frequent collision inward HashMap which eventually growth the fourth dimension for adding an object into HashMap. From Java 8 onwards though collision volition non comport on performance every bit much every bit it does inward before versions because after a threshold the linked listing volition hold out replaced past times the binary tree, which volition give you lot O(logN) performance inward the worst instance every bit compared to O(n) of linked list.
Question 3: Does all holding of Immutable Object needs to hold out final? (answer)
Not necessary, every bit stated inward a higher house you lot tin ship away accomplish the same functionality past times making fellow member every bit non-final but private together with non modifying them except inward constructor. Don't furnish setter method for them together with if it is a mutable object, hence don't e'er leak whatever reference for that member. Remember making a reference variable final, solely ensures that it volition non hold out reassigned a dissimilar value, but you lot tin ship away all the same alter private properties of an object, pointed past times that reference variable. This is i of the primal points, Interviewer likes to hear from candidates. Question 4: How does substring () within String works? (answer)
Another skilful Java interview question, I think the reply is non sufficient, but hither it is “Substring creates a novel object out of source string past times taking a portion of master copy string”. This enquiry was mainly asked to come across if the developer is familiar alongside the gamble of retentivity leak, which sub-string tin ship away create. Until Java 1.7, substring holds the reference of the master copy grapheme array, which way fifty-fifty a sub-string of v grapheme long, can prevent 1GB grapheme array from garbage collection, past times holding a rigid reference.This number is fixed inward Java 1.7, where master copy grapheme array is non referenced anymore, but that alter also made the creation of substring chip costly inward price of time. Earlier it was inward the attain of O(1), which could be O(n) in worst instance on Java 7.
Question 5: Can you lot write critical department code for the singleton? (answer)
This meat Java enquiry is a follow-up of the previous enquiry together with expecting the candidate to write Java singleton using double checked locking. Remember to work the volatile variable to brand Singleton thread-safe. Here is the code for critical department of a thread-safe Singleton pattern using double checked locking idiom:
public class Singleton { private static volatile Singleton _instance; /** * Double checked locking code on Singleton * @return Singelton instance */ public static Singleton getInstance() { if (_instance == null) { synchronized (Singleton.class) { if (_instance == null) { _instance = new Singleton(); } } } return _instance; } }
Question 6: How do you lot conduct hold mistake status piece writing stored physical care for or accessing stored physical care for from java? (answer)
This is i of the tough Java interview questions together with its opened upward for all, my friend didn't know the reply hence he didn't take away heed telling me. My convey is that stored physical care for should render an mistake code if to a greater extent than or less performance fails but if stored physical care for itself fails than catching SQLException is the solely choice. Question seven : What is deviation betwixt Executor.submit() together with Executer.execute() method ? (answer)
This Java interview enquiry is from my listing of Top fifteen Java multi-threading enquiry answers, It's getting pop twenty-four hours past times twenty-four hours because of huge demand of Java developer alongside skilful concurrency skill. Answer of this Java interview enquiry is that sometime returns an object of Future which tin ship away hold out used to uncovering number from worker thread)There is a deviation when looking at exception handling. If your tasks throw an exception together with if it was submitted alongside executing this exception volition acquire to the uncaught exception handler (when you lot don't receive got provided i explicitly, the default i volition but impress the stack delineate to System.err). If you lot submitted the chore alongside submit whatever thrown exception, checked exception or not, is hence component subdivision of the task's render status. For a chore that was submitted alongside submitting together with that terminates alongside an exception, the Future.get() volition re-throw this exception, wrapped inward an ExecutionException.
Question 8: What is the deviation betwixt mill together with abstract mill pattern? (answer)
Abstract Factory provides i to a greater extent than marker of abstraction. Consider dissimilar factories each extended from an Abstract Factory together with responsible for the creation of dissimilar hierarchies of objects based on the type of factory. E.g. AbstractFactory extended past times AutomobileFactory, UserFactory, RoleFactory etc. Each private mill would hold out responsible for the creation of objects inward that genre. Here is UML diagram of mill together with abstract mill pattern: Question 9: What is Singleton? is it amend to brand the whole method synchronized or solely critical department synchronized? (answer)
Singleton inward Java is a class alongside but i instance inward whole Java application, for example, java.lang.Runtime is a Singleton class. Creating Singleton was tricky prior Java 4 but i time Java v introduced Enum its real easy. come across my article How to create thread-safe Singleton inward Java for to a greater extent than details on writing Singleton using the enum together with double checked locking which is the purpose of this Java interview question.Question 10: Can you lot write code for iterating over HashMap inward Java 4 together with Java 5? (answer)
Tricky i but he managed to write using piece together with a for loop. Actually, at that topographic point are 4 ways to iterate over whatever Map inward Java, i involves using keySet() together with iterating over primal together with hence using get() method to recollect values, which is chip expensive. Second method involves using entrySet() together with iterating over them either past times using for each loop or piece alongside Iterator.hasNext() method. This i is a amend approach because both primal together with value object are available to you lot during Iteration together with you lot don't demand to telephone yell upward get() method for retrieving the value, which could give the O(n) performance inward instance of huge linked listing at i bucket. See my postal service 4 ways to iterate over Map inward Java for detailed explanation together with code examples.
Question eleven : When do you lot override hashCode() together with equals()? (answer)
Whenever necessary particularly if you lot desire to do equality depository fiscal establishment check based upon line of piece of work organisation logic rather than object equality e.g. 2 employee object are equal if they receive got the same emp_id, despite the fact that they are 2 dissimilar objects, created past times dissimilar component subdivision of the code. Also overriding both these methods are must if you lot desire to work them every bit primal inward HashMap. Now every bit component subdivision of the equals-hashcode contract inward Java, when you lot override equals, you lot must override hashcode every bit well, otherwise your object volition not break invariant of classes e.g. Set, Map which relies on equals() method for functioning properly. You tin ship away also depository fiscal establishment check my postal service 5 tips on equals inward Java to empathize subtle number which tin ship away arise piece dealing alongside these 2 methods.
Question 12:. What volition hold out the job if you lot don't override hashCode() method? (answer)
If you lot don't override equals method, hence the contract betwixt equals together with hashcode volition non work, according to which, 2 objects which are equal to equals() must receive got the same hashcode. In this case, to a greater extent than or less other object may render dissimilar hashCode together with volition hold out stored inward that location, which breaks invariant of HashMap class because they are non supposed to allow duplicate keys. When you lot add together the object using put() method, it iterate through all Map.Entry object nowadays inward that bucket location, together with update value of the previous mapping, if Map already contains that key. This volition non piece of work if hashcode is non overridden.
Question xiii : Is it amend to synchronize critical department of getInstance() method or whole getInstance() method? (answer)
The reply is an solely critical section, because if nosotros lock the whole method that every fourth dimension person calls this method, it volition receive got to hold off fifty-fifty though nosotros are non creating whatever object. In other words, synchronization is solely needed, when you lot create an object, which happens solely once. Once an object has created, at that topographic point is no demand for whatever synchronization. In fact, that's real piteous coding inward price of performance, every bit synchronized method trim performance upward to 10 to 20 times. Here is UML diagram of Singleton pattern:
By the way, at that topographic point are several ways to create a thread-safe singleton inward Java, which you lot tin ship away also get upward every bit component subdivision of this enquiry or whatever follow-up.
Question 14: Where does equals() together with hashCode() method comes inward the movie during the get() operation? (answer)
This meat Java interview enquiry is a follow-up of previous Java enquiry together with the candidate should know that i time you lot get upward hashCode, people are most probable ask, how they are used inward HashMap. When you lot furnish a primal object, commencement it's hashcode method is called to calculate bucket location. Since a bucket may incorporate to a greater extent than than i entry every bit linked list, each of those Map.Entry object is evaluated past times using equals() method to come across if they incorporate the actual primal object or not.
Questions 15: How do you lot avoid deadlock inward Java? (answer)
You tin ship away avoid deadlock past times breaking circular hold off condition. In lodge to do that, you lot tin ship away brand organisation inward the code to impose the ordering on acquisition and liberate of locks. If lock volition hold out acquired inward a consistent lodge together with released inward but reverse order, at that topographic point would non hold out a province of affairs where i thread is holding a lock which is acquired by other together with vice-versa. See the detailed reply for the code illustration together with to a greater extent than detailed explanation.
Question 16: What is the deviation betwixt creating String every bit new() together with literal? (answer)
When nosotros create the string with new() Operator, it’s created inward heap together with non added into string puddle piece String created using literal are created inward String puddle itself which exists in PermGen expanse of heap.
String str = new String("Test")
does non pose the object str inward String pool, nosotros demand to call String.intern() method which is used to pose them into String puddle explicitly. It's solely when you lot create String object every bit String literal e.g. String s = "Test" Java automatically pose that into String pool. By the way at that topographic point is a grab hither Since nosotros are passing arguments every bit "Test", which is a String literal, it volition also create to a greater extent than or less other object every bit "Test" on string pool. This is the i point, which has gone unnoticed until knowledgeable readers of weblog suggested it. To larn to a greater extent than most the deviation betwixt String literal together with String object, see this article.
Here is a overnice picture which shows this deviation quite well:
Question 17: What is Immutable Object? Can you lot write Immutable Class? (answer)
Immutable classes are Java classes whose objects tin ship away non hold out modified i time created. Any alteration inward Immutable object results inward the novel object. For example, String is immutable inward Java. Mostly Immutable classes are also in conclusion inward Java, inward lodge to forestall subclasses from overriding methods, which tin ship away compromise Immutability. You tin ship away accomplish the same functionality past times making fellow member every bit non-final but private together with non modifying them except inward constructor.
Apart from obvious, you lot also demand to brand certain that, you lot should non let on the internals of Immutable object, particularly if it contains a mutable member. Similarly, when you lot convey the value for the mutable fellow member from customer e.g. java.util.Date, use clone() method keep a divide re-create for yourself, to forestall the gamble of malicious customer modifying mutable reference after setting it.
The Same precaution needs to hold out taken piece returning value to a mutable member, render to a greater extent than or less other divide re-create to the client, never render master copy reference held past times Immutable class. You tin ship away come across my post How to create an Immutable class inward Java for measuring past times measuring guide together with code examples.
Question 18: Give the simplest way to uncovering out the fourth dimension a method takes for execution without using whatever profiling tool? (answer)
Read the scheme fourth dimension but before the method is invoked together with instantly after method returns. Take the fourth dimension difference, which volition give you lot the fourth dimension taken past times a method for execution.
To pose it inward code…
long start = System.currentTimeMillis (); method (); long destination = System.currentTimeMillis (); System.out.println (“Time taken for execution is ” + (end – start));
Remember that if the fourth dimension taken for execution is likewise small, it mightiness present that it is taking null milliseconds for execution. Try it on a method which is big enough, inward the sense the i which is doing considerable sum of processing
Question 19: Which 2 method you lot demand to implement to work an Object every bit primal inward HashMap? (answer)
In lodge to work whatever object every bit Key inward HashMap or Hashtable, it must implement equals and hashcode method in Java. Read How HashMap industrial plant inward Java for a detailed explanation on how equals and hashcode method is used to pose together with acquire an object from HashMap.
Question 20: How would you lot forestall a customer from direct instantiating your concrete classes? For example, you lot receive got a Cache interface together with 2 implementation classes MemoryCache together with DiskCache, How do you lot ensure at that topographic point is no object of this 2 classes is created past times customer using new() keyword.
I exit this question for you lot to practise together with think most before I give the answer. I am certain you lot tin ship away figure out the correct way to do this, every bit this is i of the of import determination to continue command of classes inward your hand, slap-up from a maintenance perspective.
I am also real grateful to my readers who receive got generously contributed several skilful questions from Java Interviews for both beginners together with experienced developers alike. I receive got already answered many of these questions inward this weblog together with you lot tin ship away easily uncovering a relevant postal service past times using the search box at the top correct corner of this page.
More questions:
133+ Java Interview Questions from Last v years (read here)
50+ Java Multithreading Questions from Last iii years (see here)
50+ Programmer Phone Interview Questions alongside answers (link)