Top Xx Gist Coffee Interview Questions In Addition To Answers From Investment Banks
Core Java Interview Question Answer
This is a novel series of sharing centre Java interview query as well as reply on Finance domain as well as 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 centre Java interview questions from multi-threading, collection, serialization, coding as well as OOPS pattern principles. Anybody who is preparing for whatever Java developer Interview on whatever Investment banking concern tin flame live benefited from these laid of centre Java Interview questions as well as answers. I direct hold collected these Java questions from my friends as well as I idea to portion amongst you lot all.
I hope this volition live helpful for both of us. It's also beneficial to practise about programming interview questions because, inwards almost all Java interview, in that location are at to the lowest degree 1 or 2 coding questions appear. Please portion answers for unanswered Java interview questions as well as permit us know how skilful these Java interview questions are?
If you lot are seriously preparing for Java Interviews as well as aid interviews, as well as thus I also advise taking a aspect at Java Programming Interviews Exposed yesteryear Markham. This is the Java specific majority modeled on their before best seller, also virtually programming interviews. This majority contains questions non simply from Java but also from related technology stack e.g. JUnit, Maven, Design Patterns, JVM Internals, Android as well as Best practices. Questions are skilful as well as answers are crisp as well as rattling well explained, which makes it interesting read every bit well.
By the way apart from blogs as well as articles, you lot tin flame also convey payoff of about books, which are specially written for clearing whatever programming interviews as well as about focused on Java programming, 2 books which comes inwards minds are Cracking the Coding Interview as well as Programming Interviews Exposed. Both books are focused on programming inwards full general as well as a lot of other related topics e.g. information structures, algorithms, database, SQL, networking as well as behavioral questions, but also contains Java concepts.
Question 1: What’s incorrect using HashMap inwards the multi-threaded environment? When get() method acquire to the infinite loop? (answer)
Well, nix is wrong, it depending upon how you lot use. For example, if you lot initialize the HashMap yesteryear simply 1 thread as well as and thus all threads are solely reading from it, as well as thus it's perfectly fine. One instance of this is a Map which contains configuration properties. The existent work starts when at to the lowest degree 1 of that thread is updating HashMap i.e. adding, changing or removing whatever primal value pair. Since put() performance tin flame displace re-sizing as well as which tin flame farther atomic number 82 to infinite loop, that's why either you lot should exercise Hashtable or ConcurrentHashMap, after is better.
Question 2. Does non overriding hashCode() method has whatever performance implication? (answer)
This is a skilful query as well as opened upwardly to all, every bit per my noesis a piteous hashcode role volition outcome inwards the frequent collision inwards HashMap which eventually growth the fourth dimension for adding an object into HashMap. From Java 8 onwards though collision volition non conduct on performance every bit much every bit it does inwards before versions because after a threshold the linked listing volition live replaced yesteryear the binary tree, which volition give you lot O(logN) performance inwards the worst instance every bit compared to O(n) of linked list.
This number is fixed inwards Java 1.7, where master grapheme array is non referenced anymore, but that modify also made the creation of substring fleck costly inwards damage of time. Earlier it was inwards the gain 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 centre Java query is a follow-up of the previous query as well as expecting the candidate to write Java singleton using double checked locking. Remember to exercise 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 as well as if it was submitted amongst executing this exception volition acquire to the uncaught exception handler (when you lot don't direct hold provided 1 explicitly, the default 1 volition simply impress the stack draw to System.err). If you lot submitted the chore amongst submit whatever thrown exception, checked exception or not, is as well as thus component subdivision of the task's render status. For a chore that was submitted amongst submitting as well as that terminates amongst an exception, the Future.get() volition re-throw this exception, wrapped inwards an ExecutionException.
Question 10: Can you lot write code for iterating over HashMap inwards Java 4 as well as Java 5? (answer)
Tricky 1 but he managed to write using spell as well as a for loop. Actually, in that location are iv ways to iterate over whatever Map inwards Java, 1 involves using keySet() as well as iterating over primal as well as and thus using get() method to recollect values, which is fleck expensive. Second method involves using entrySet() as well as iterating over them either yesteryear using for each loop or spell amongst Iterator.hasNext() method. This 1 is a amend approach because both primal as well as value object are available to you lot during Iteration as well as you lot don't demand to telephone cry upwardly get() method for retrieving the value, which could give the O(n) performance inwards instance of huge linked listing at 1 bucket. See my postal service 4 ways to iterate over Map inwards Java for detailed explanation as well as code examples.
Question eleven : When do you lot override hashCode() as well as equals()? (answer)
Whenever necessary peculiarly if you lot desire to do equality banking concern check based upon work organisation logic rather than object equality e.g. 2 employee object are equal if they direct hold the same emp_id, despite the fact that they are 2 dissimilar objects, created yesteryear dissimilar component subdivision of the code. Also overriding both these methods are must if you lot desire to exercise them every bit primal inwards HashMap. Now every bit component subdivision of the equals-hashcode contract inwards 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 flame also banking concern check my postal service 5 tips on equals inwards Java to empathize subtle number which tin flame arise spell dealing amongst these 2 methods.
Question 12:. What volition live the work if you lot don't override hashCode() method? (answer)
If you lot don't override equals method, as well as thus the contract betwixt equals as well as hashcode volition non work, according to which, 2 objects which are equal to equals() must direct hold the same hashcode. In this case, about other object may render dissimilar hashCode as well as volition live stored inwards that location, which breaks invariant of HashMap shape 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 inwards that bucket location, as well as update value of the previous mapping, if Map already contains that key. This volition non piece of work if hashcode is non overridden.
Question thirteen : 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 direct hold 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, in that location is no demand for whatever synchronization. In fact, that's rattling piteous coding inwards damage of performance, every bit synchronized method cut performance upwardly to 10 to 20 times. Here is UML diagram of Singleton pattern:
By the way, in that location are several ways to create a thread-safe singleton inwards Java, which you lot tin flame also advert every bit component subdivision of this query or whatever follow-up.
Question 14: Where does equals() as well as hashCode() method comes inwards the painting demo during the get() operation? (answer)
This centre Java interview query is a follow-up of previous Java query as well as the candidate should know that in 1 lawsuit you lot advert hashCode, people are most probable ask, how they are used inwards HashMap. When you lot render a primal object, kickoff it's hashcode method is called to calculate bucket location. Since a bucket may incorporate to a greater extent than than 1 entry every bit linked list, each of those Map.Entry object is evaluated yesteryear using equals() method to run across if they incorporate the actual primal object or not.
Questions 15: How do you lot avoid deadlock inwards Java? (answer)
You tin flame avoid deadlock yesteryear breaking circular hold off condition. In companionship to do that, you lot tin flame brand organisation inwards the code to impose the ordering on acquisition and liberate of locks. If lock volition live acquired inwards a consistent companionship as well as released inwards simply contrary order, in that location would non live a province of affairs where 1 thread is holding a lock which is acquired by other as well as vice-versa. See the detailed reply for the code instance as well as to a greater extent than detailed explanation.
Question 16: What is the deviation betwixt creating String every bit new() as well as literal? (answer)
When nosotros create the string with new() Operator, it’s created inwards heap as well as non added into string puddle spell String created using literal are created inwards String puddle itself which exists in PermGen expanse of heap.
does non pose the object str inwards 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 in that location is a grab hither Since nosotros are passing arguments every bit "Test", which is a String literal, it volition also create about other object every bit "Test" on string pool. This is the 1 point, which has gone unnoticed until knowledgeable readers of weblog suggested it. To larn to a greater extent than virtually the deviation betwixt String literal as well as String object, see this article.
Here is a dainty ikon 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 flame non live modified in 1 lawsuit created. Any alteration inwards Immutable object results inwards the novel object. For example, String is immutable inwards Java. Mostly Immutable classes are also lastly inwards Java, inwards companionship to foreclose subclasses from overriding methods, which tin flame compromise Immutability. You tin flame gain the same functionality yesteryear making fellow member every bit non-final but private as well as non modifying them except inwards constructor.
Apart from obvious, you lot also demand to brand certain that, you lot should non break the internals of Immutable object, peculiarly 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 split re-create for yourself, to foreclose the direct chances of malicious customer modifying mutable reference after setting it.
The Same precaution needs to live taken spell returning value to a mutable member, render about other split re-create to the client, never render master reference held yesteryear Immutable class. You tin flame run across my post How to create an Immutable shape inwards Java for mensuration yesteryear mensuration guide as well as code examples.
Question 18: Give the simplest way to notice out the fourth dimension a method takes for execution without using whatever profiling tool? (answer)
Read the scheme fourth dimension simply before the method is invoked as well as straightaway after method returns. Take the fourth dimension difference, which volition give you lot the fourth dimension taken yesteryear a method for execution.
To pose it inwards code…
Remember that if the fourth dimension taken for execution is likewise small, it mightiness demo that it is taking null milliseconds for execution. Try it on a method which is big enough, inwards the sense the 1 which is doing considerable total of processing
Question 19: Which 2 method you lot demand to implement to exercise an Object every bit primal inwards HashMap? (answer)
In companionship to exercise whatever object every bit Key inwards HashMap or Hashtable, it must implement equals and hashcode method in Java. Read How HashMap works inwards Java for a detailed explanation on how equals and hashcode method is used to pose as well as acquire an object from HashMap.
Question 20: How would you lot foreclose a customer from straight instantiating your concrete classes? For example, you lot direct hold a Cache interface as well as 2 implementation classes MemoryCache as well as DiskCache, How do you lot ensure in that location is no object of this 2 classes is created yesteryear customer using new() keyword.
I move out this question for you lot to practise as well as think virtually before I give the answer. I am certain you lot tin flame figure out the correct way to do this, every bit this is 1 of the of import conclusion to expire along command of classes inwards your hand, corking from a maintenance perspective.
I am also rattling grateful to my readers who direct hold generously contributed several skilful questions from Java Interviews for both beginners as well as experienced developers alike. I direct hold already answered many of these questions inwards this weblog as well as you lot tin flame easily notice a relevant postal service yesteryear 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 three years (see here)
50+ Programmer Phone Interview Questions amongst answers (link)
This is a novel series of sharing centre Java interview query as well as reply on Finance domain as well as 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 centre Java interview questions from multi-threading, collection, serialization, coding as well as OOPS pattern principles. Anybody who is preparing for whatever Java developer Interview on whatever Investment banking concern tin flame live benefited from these laid of centre Java Interview questions as well as answers. I direct hold collected these Java questions from my friends as well as I idea to portion amongst you lot all.
I hope this volition live helpful for both of us. It's also beneficial to practise about programming interview questions because, inwards almost all Java interview, in that location are at to the lowest degree 1 or 2 coding questions appear. Please portion answers for unanswered Java interview questions as well as permit us know how skilful these Java interview questions are?
If you lot are seriously preparing for Java Interviews as well as aid interviews, as well as thus I also advise taking a aspect at Java Programming Interviews Exposed yesteryear Markham. This is the Java specific majority modeled on their before best seller, also virtually programming interviews. This majority contains questions non simply from Java but also from related technology stack e.g. JUnit, Maven, Design Patterns, JVM Internals, Android as well as Best practices. Questions are skilful as well as answers are crisp as well as rattling 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 as well as tricky Java questions e.g. Why multiple inheritance is non supported inwards Java is 1 of the tricky questions inwards java. Most questions are asked on Senior as well as experienced marking i.e. 3, 4, v or half-dozen years of Java sense e.g. How HashMap works inwards Java, which is most pop on experienced Java interviews. By the way, late I was looking at answers as well as comments made on Java interview questions given inwards this postal service as well as I industrial plant life about of them quite useful to include into the principal postal service to do goodness all.By the way apart from blogs as well as articles, you lot tin flame also convey payoff of about books, which are specially written for clearing whatever programming interviews as well as about focused on Java programming, 2 books which comes inwards minds are Cracking the Coding Interview as well as Programming Interviews Exposed. Both books are focused on programming inwards full general as well as a lot of other related topics e.g. information structures, algorithms, database, SQL, networking as well as behavioral questions, but also contains Java concepts.
Question 1: What’s incorrect using HashMap inwards the multi-threaded environment? When get() method acquire to the infinite loop? (answer)
Well, nix is wrong, it depending upon how you lot use. For example, if you lot initialize the HashMap yesteryear simply 1 thread as well as and thus all threads are solely reading from it, as well as thus it's perfectly fine. One instance of this is a Map which contains configuration properties. The existent work starts when at to the lowest degree 1 of that thread is updating HashMap i.e. adding, changing or removing whatever primal value pair. Since put() performance tin flame displace re-sizing as well as which tin flame farther atomic number 82 to infinite loop, that's why either you lot should exercise Hashtable or ConcurrentHashMap, after is better.
Question 2. Does non overriding hashCode() method has whatever performance implication? (answer)
This is a skilful query as well as opened upwardly to all, every bit per my noesis a piteous hashcode role volition outcome inwards the frequent collision inwards HashMap which eventually growth the fourth dimension for adding an object into HashMap. From Java 8 onwards though collision volition non conduct on performance every bit much every bit it does inwards before versions because after a threshold the linked listing volition live replaced yesteryear the binary tree, which volition give you lot O(logN) performance inwards the worst instance every bit compared to O(n) of linked list.
Question 3: Does all holding of Immutable Object needs to live final? (answer)
Not necessary, every bit stated inwards a higher house you lot tin flame gain the same functionality yesteryear making fellow member every bit non-final but private as well as non modifying them except inwards constructor. Don't render setter method for them as well as if it is a mutable object, as well as thus don't e'er leak whatever reference for that member. Remember making a reference variable final, solely ensures that it volition non live reassigned a dissimilar value, but you lot tin flame even thus modify private properties of an object, pointed yesteryear that reference variable. This is 1 of the primal points, Interviewer likes to listen 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 yesteryear taking a portion of master string”. This query was mainly asked to run across if the developer is familiar amongst the direct chances of retentivity leak, which sub-string tin flame create. Until Java 1.7, substring holds the reference of the master grapheme array, which agency fifty-fifty a sub-string of v grapheme long, can prevent 1GB grapheme array from garbage collection, yesteryear holding a potent reference.This number is fixed inwards Java 1.7, where master grapheme array is non referenced anymore, but that modify also made the creation of substring fleck costly inwards damage of time. Earlier it was inwards the gain 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 centre Java query is a follow-up of the previous query as well as expecting the candidate to write Java singleton using double checked locking. Remember to exercise 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 handgrip fault status spell writing stored physical care for or accessing stored physical care for from java? (answer)
This is 1 of the tough Java interview questions as well as its opened upwardly for all, my friend didn't know the reply thus he didn't heed telling me. My convey is that stored physical care for should render an fault code if about 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() as well as Executer.execute() method ? (answer)
This Java interview query is from my listing of Top xv Java multi-threading query answers, It's getting pop twenty-four hr catamenia yesteryear twenty-four hr catamenia because of huge demand of Java developer amongst skilful concurrency skill. Answer of this Java interview query is that erstwhile returns an object of Future which tin flame live used to notice outcome from worker thread)There is a deviation when looking at exception handling. If your tasks throw an exception as well as if it was submitted amongst executing this exception volition acquire to the uncaught exception handler (when you lot don't direct hold provided 1 explicitly, the default 1 volition simply impress the stack draw to System.err). If you lot submitted the chore amongst submit whatever thrown exception, checked exception or not, is as well as thus component subdivision of the task's render status. For a chore that was submitted amongst submitting as well as that terminates amongst an exception, the Future.get() volition re-throw this exception, wrapped inwards an ExecutionException.
Question 8: What is the deviation betwixt manufacturing industrial plant life as well as abstract manufacturing industrial plant life pattern? (answer)
Abstract Factory provides 1 to a greater extent than marking of abstraction. Consider dissimilar factories each extended from an Abstract Factory as well as responsible for the creation of dissimilar hierarchies of objects based on the type of factory. E.g. AbstractFactory extended yesteryear AutomobileFactory, UserFactory, RoleFactory etc. Each private manufacturing industrial plant life would live responsible for the creation of objects inwards that genre. Here is UML diagram of manufacturing industrial plant life as well as abstract manufacturing industrial plant life pattern: Question 9: What is Singleton? is it amend to brand the whole method synchronized or solely critical department synchronized? (answer)
Singleton inwards Java is a shape amongst simply 1 instance inwards whole Java application, for example, java.lang.Runtime is a Singleton class. Creating Singleton was tricky prior Java 4 but in 1 lawsuit Java v introduced Enum its rattling easy. run across my article How to create thread-safe Singleton inwards Java for to a greater extent than details on writing Singleton using the enum as well as double checked locking which is the purpose of this Java interview question.Question 10: Can you lot write code for iterating over HashMap inwards Java 4 as well as Java 5? (answer)
Tricky 1 but he managed to write using spell as well as a for loop. Actually, in that location are iv ways to iterate over whatever Map inwards Java, 1 involves using keySet() as well as iterating over primal as well as and thus using get() method to recollect values, which is fleck expensive. Second method involves using entrySet() as well as iterating over them either yesteryear using for each loop or spell amongst Iterator.hasNext() method. This 1 is a amend approach because both primal as well as value object are available to you lot during Iteration as well as you lot don't demand to telephone cry upwardly get() method for retrieving the value, which could give the O(n) performance inwards instance of huge linked listing at 1 bucket. See my postal service 4 ways to iterate over Map inwards Java for detailed explanation as well as code examples.
Question eleven : When do you lot override hashCode() as well as equals()? (answer)
Whenever necessary peculiarly if you lot desire to do equality banking concern check based upon work organisation logic rather than object equality e.g. 2 employee object are equal if they direct hold the same emp_id, despite the fact that they are 2 dissimilar objects, created yesteryear dissimilar component subdivision of the code. Also overriding both these methods are must if you lot desire to exercise them every bit primal inwards HashMap. Now every bit component subdivision of the equals-hashcode contract inwards 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 flame also banking concern check my postal service 5 tips on equals inwards Java to empathize subtle number which tin flame arise spell dealing amongst these 2 methods.
Question 12:. What volition live the work if you lot don't override hashCode() method? (answer)
If you lot don't override equals method, as well as thus the contract betwixt equals as well as hashcode volition non work, according to which, 2 objects which are equal to equals() must direct hold the same hashcode. In this case, about other object may render dissimilar hashCode as well as volition live stored inwards that location, which breaks invariant of HashMap shape 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 inwards that bucket location, as well as update value of the previous mapping, if Map already contains that key. This volition non piece of work if hashcode is non overridden.
Question thirteen : 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 direct hold 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, in that location is no demand for whatever synchronization. In fact, that's rattling piteous coding inwards damage of performance, every bit synchronized method cut performance upwardly to 10 to 20 times. Here is UML diagram of Singleton pattern:
By the way, in that location are several ways to create a thread-safe singleton inwards Java, which you lot tin flame also advert every bit component subdivision of this query or whatever follow-up.
Question 14: Where does equals() as well as hashCode() method comes inwards the painting demo during the get() operation? (answer)
This centre Java interview query is a follow-up of previous Java query as well as the candidate should know that in 1 lawsuit you lot advert hashCode, people are most probable ask, how they are used inwards HashMap. When you lot render a primal object, kickoff it's hashcode method is called to calculate bucket location. Since a bucket may incorporate to a greater extent than than 1 entry every bit linked list, each of those Map.Entry object is evaluated yesteryear using equals() method to run across if they incorporate the actual primal object or not.
Questions 15: How do you lot avoid deadlock inwards Java? (answer)
You tin flame avoid deadlock yesteryear breaking circular hold off condition. In companionship to do that, you lot tin flame brand organisation inwards the code to impose the ordering on acquisition and liberate of locks. If lock volition live acquired inwards a consistent companionship as well as released inwards simply contrary order, in that location would non live a province of affairs where 1 thread is holding a lock which is acquired by other as well as vice-versa. See the detailed reply for the code instance as well as to a greater extent than detailed explanation.
Question 16: What is the deviation betwixt creating String every bit new() as well as literal? (answer)
When nosotros create the string with new() Operator, it’s created inwards heap as well as non added into string puddle spell String created using literal are created inwards String puddle itself which exists in PermGen expanse of heap.
String str = new String("Test")
does non pose the object str inwards 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 in that location is a grab hither Since nosotros are passing arguments every bit "Test", which is a String literal, it volition also create about other object every bit "Test" on string pool. This is the 1 point, which has gone unnoticed until knowledgeable readers of weblog suggested it. To larn to a greater extent than virtually the deviation betwixt String literal as well as String object, see this article.
Here is a dainty ikon 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 flame non live modified in 1 lawsuit created. Any alteration inwards Immutable object results inwards the novel object. For example, String is immutable inwards Java. Mostly Immutable classes are also lastly inwards Java, inwards companionship to foreclose subclasses from overriding methods, which tin flame compromise Immutability. You tin flame gain the same functionality yesteryear making fellow member every bit non-final but private as well as non modifying them except inwards constructor.
Apart from obvious, you lot also demand to brand certain that, you lot should non break the internals of Immutable object, peculiarly 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 split re-create for yourself, to foreclose the direct chances of malicious customer modifying mutable reference after setting it.
The Same precaution needs to live taken spell returning value to a mutable member, render about other split re-create to the client, never render master reference held yesteryear Immutable class. You tin flame run across my post How to create an Immutable shape inwards Java for mensuration yesteryear mensuration guide as well as code examples.
Question 18: Give the simplest way to notice out the fourth dimension a method takes for execution without using whatever profiling tool? (answer)
Read the scheme fourth dimension simply before the method is invoked as well as straightaway after method returns. Take the fourth dimension difference, which volition give you lot the fourth dimension taken yesteryear a method for execution.
To pose it inwards code…
long start = System.currentTimeMillis (); method (); long halt = 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 demo that it is taking null milliseconds for execution. Try it on a method which is big enough, inwards the sense the 1 which is doing considerable total of processing
Question 19: Which 2 method you lot demand to implement to exercise an Object every bit primal inwards HashMap? (answer)
In companionship to exercise whatever object every bit Key inwards HashMap or Hashtable, it must implement equals and hashcode method in Java. Read How HashMap works inwards Java for a detailed explanation on how equals and hashcode method is used to pose as well as acquire an object from HashMap.
Question 20: How would you lot foreclose a customer from straight instantiating your concrete classes? For example, you lot direct hold a Cache interface as well as 2 implementation classes MemoryCache as well as DiskCache, How do you lot ensure in that location is no object of this 2 classes is created yesteryear customer using new() keyword.
I move out this question for you lot to practise as well as think virtually before I give the answer. I am certain you lot tin flame figure out the correct way to do this, every bit this is 1 of the of import conclusion to expire along command of classes inwards your hand, corking from a maintenance perspective.
I am also rattling grateful to my readers who direct hold generously contributed several skilful questions from Java Interviews for both beginners as well as experienced developers alike. I direct hold already answered many of these questions inwards this weblog as well as you lot tin flame easily notice a relevant postal service yesteryear 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 three years (see here)
50+ Programmer Phone Interview Questions amongst answers (link)







Komentar
Posting Komentar