Here I will be posting some interview questions in Java, which I got during the interviews & I feel one can look the solution of these questions while going for interviews at senior level -
Question1 : Difference between 32 bit JVM & 64 bit JVM.
Solution : One can look this page for quick answer else can google further for more details - techidiocy.com/difference-
between-32-bit-and-64-bit-jvm-choose-wisely/
Question2 : Another questions I have got is about various scenarios & one can give solution related to interceptors.
Solution : One can go through the given page & here interceptors are explained briefly & with examples -
www.developer.com/java/understanding-interceptors-for-java-ee.html
And if you are interested in more about interceptors then please check on internet.
Check - www.buggybread.com/2015/02/java-j2ee-technical-architect-interview.html
Question3 : What is Optimistic Locking & Pessimistic Locking ?
Solution : By selecting anyone of these you decide on how much concurrency you want in your application. And rest below
is good explaination taken from stackoverflow -
Optimistic Locking is a strategy where you read a record, take note of a version number (other methods to do this involve dates, timestamps or checksums/hashes) and check that the version hasn't changed before you write the record back. When you write the record back you filter the update on the version to make sure it's atomic. (i.e. hasn't been updated between when you check the version and write the record to the disk) and update the version in one hit.
If the record is dirty (i.e. different version to yours) you abort the transaction and the user can re-start it.
This strategy is most applicable to high-volume systems and three-tier architectures where you do not necessarily maintain a connection to the database for your session. In this situation the client cannot actually maintain database locks as the connections are taken from a pool and you may not be using the same connection from one access to the next.
Pessimistic Locking is when you lock the record for your exclusive use until you have finished with it. It has much better integrity than optimistic locking but requires you to be careful with your application design to avoid Deadlocks. To use pessimistic locking you need either a direct connection to the database (as would typically be the case in a two tier client server application) or an externally available transaction ID that can be used independently of the connection.
In the latter case you open the transaction with the TxID and then reconnect using that ID. The DBMS maintains the locks and allows you to pick the session back up through the TxID. This is how distributed transactions using two-phase commit protocols (such as XA or COM+ Transactions) work.
Question4 : What is decorator pattern?
Solution : You can directly give the definition which is there over internet.
Or you can try the explanation given here. As I have always tried to explain this in the interviews with HCL, Exela
Technologies etc, but all those never agreed with this as to them it is simple overriding & not some decorator. In
HCL, interviewer commented that by this definition of mine every custom class is decorating 'Object' class. And I
say that whenever you do extend any class & override any method or add any method which you are calling from
the overridden method, you can decorate the object by this way, you just need to replace the parent object
with your object & any one getting the parent object will not complain for your object. This is called you decorated
the object. But in java.io package, it is wrapping the objects under your custom objects to add more functionality
but you may have not decorated the original one as you have changed its entity now. And no-one takes this
explanation. So you have to take the call here as which class from io package is being discussed for Decorator
pattern.
Question5 : What is façade design pattern? Why & where you used this pattern? And give the practical example where you
used it.- It was asked in Tech Mahindra.
Solution : Study about this pattern here.
Question6 : How to convert to/from JSON and how to parse the XML or how to validate the XML? Such questions I am
seeing many times during the interviews.
Solution : There are many libraries available, there are many web pages on these topics, so my concerns comes that why
does one need to store such in his/her mind rather keeping the mind free to digest the requirements with no
prejudices around known solutions.
Below are few examples/samples/ideas given, pick the one which suits your requirements/environment -
www.developer.com/java/ent/explore-the-java-api-for-json-processing.html
adam-bien.com/roller/abien/entry/converting_json_to_map_with
www.journaldev.com/2315/java-json-example
stackoverflow.com/questions/9593409/how-to-convert-pojo-to-json-and-vice-versa
So for such requirements one just need to know how to google, rather keep such in mind, as this way you can
get latest & better ideas to incorporate.
Question7 : How will you implement your custom HashMap or LinkedList? Provide 1-2 operations to add, iterate to fetch the
value or may be delete the value.
Solution : You can easily provide the basic implementation for these, but interviewer will try to unsettle you by comments like
Q1 : This kind of implementation is not in Java.
Q2 : This kind of method name is not in Java.
Q3 : What is the performance impact while using your custom implementation?
Q4 : Why to use your class & in what scenarios?
Just tell that person, that it is your implementation so you have given that as per the basic requirement, so you can also give
any method name there. But when you are giving your implementation then you must be able to understand it deeply & some
scenarios where it will work properly & correctly. And your class is custom implementation for particular kind of requirements,
so it will be used for those scenarios & may not be used for every general scenarios as you will be testing your code against those particular requirements only.
Question8 : How will you implement the immutability concept? It was asked in interview for SAS.
Solution : Such questions have been asked in many interviews. And definitely this concept looks straight & simple but it is
not. And also, it depends on the kind of mindset of your interviewer, like what s/he has read about this concept etc.
So you will need to understand the requirements & the purpose here, to frame your answer accordingly.
You can check for the ideas shared here.
Question9 : In case of multi-layered architecture, if DAO layer throws an exception then what action will you take in your
upstream layers?
Solutions : Though interviewer tries to get one straight answer then will drill down on that answer.
But truth is that there is no one straight answer for this & such questions are to make you uncomfortable & have
the upper hand on you. As such decision depends on the application architecture, business requirements, what
responsibilities are distributed across the layers, what kind of action you want to take for such actions etc.
You can't simple return the exception to the upper layers, as if such exception is compile time or checked one, then
upper layer has to take the responsibility to handle that & if it is runtime or unchecked then it can cause unplanned
failures at above layers.
You can't also handle every exception in one layer as may the above layer wants to take some different action.
Also you can't create the custom exception every time for such exceptions as creating a new exception is a costly
affair.
So decision needs thorough analysis of business requirements, application architecture & its design, one can't start
throwing random answers for such issues.
Question10 : Tell about shallow cloning & deep cloning.
Solution : I generally don't remember the method names but again this topic is worth reading every time, as some other APIs
or ways are out there. But yes, hit the google if you get any real work around this as there is way lot difference
between answering this for interview & implementing for the production.
Question1 : Difference between 32 bit JVM & 64 bit JVM.
Solution : One can look this page for quick answer else can google further for more details - techidiocy.com/difference-
between-32-bit-and-64-bit-jvm-choose-wisely/
Question2 : Another questions I have got is about various scenarios & one can give solution related to interceptors.
Solution : One can go through the given page & here interceptors are explained briefly & with examples -
www.developer.com/java/understanding-interceptors-for-java-ee.html
And if you are interested in more about interceptors then please check on internet.
Check - www.buggybread.com/2015/02/java-j2ee-technical-architect-interview.html
Question3 : What is Optimistic Locking & Pessimistic Locking ?
Solution : By selecting anyone of these you decide on how much concurrency you want in your application. And rest below
is good explaination taken from stackoverflow -
Optimistic Locking is a strategy where you read a record, take note of a version number (other methods to do this involve dates, timestamps or checksums/hashes) and check that the version hasn't changed before you write the record back. When you write the record back you filter the update on the version to make sure it's atomic. (i.e. hasn't been updated between when you check the version and write the record to the disk) and update the version in one hit.
If the record is dirty (i.e. different version to yours) you abort the transaction and the user can re-start it.
This strategy is most applicable to high-volume systems and three-tier architectures where you do not necessarily maintain a connection to the database for your session. In this situation the client cannot actually maintain database locks as the connections are taken from a pool and you may not be using the same connection from one access to the next.
Pessimistic Locking is when you lock the record for your exclusive use until you have finished with it. It has much better integrity than optimistic locking but requires you to be careful with your application design to avoid Deadlocks. To use pessimistic locking you need either a direct connection to the database (as would typically be the case in a two tier client server application) or an externally available transaction ID that can be used independently of the connection.
In the latter case you open the transaction with the TxID and then reconnect using that ID. The DBMS maintains the locks and allows you to pick the session back up through the TxID. This is how distributed transactions using two-phase commit protocols (such as XA or COM+ Transactions) work.
Question4 : What is decorator pattern?
Solution : You can directly give the definition which is there over internet.
Or you can try the explanation given here. As I have always tried to explain this in the interviews with HCL, Exela
Technologies etc, but all those never agreed with this as to them it is simple overriding & not some decorator. In
HCL, interviewer commented that by this definition of mine every custom class is decorating 'Object' class. And I
say that whenever you do extend any class & override any method or add any method which you are calling from
the overridden method, you can decorate the object by this way, you just need to replace the parent object
with your object & any one getting the parent object will not complain for your object. This is called you decorated
the object. But in java.io package, it is wrapping the objects under your custom objects to add more functionality
but you may have not decorated the original one as you have changed its entity now. And no-one takes this
explanation. So you have to take the call here as which class from io package is being discussed for Decorator
pattern.
Question5 : What is façade design pattern? Why & where you used this pattern? And give the practical example where you
used it.- It was asked in Tech Mahindra.
Solution : Study about this pattern here.
Question6 : How to convert to/from JSON and how to parse the XML or how to validate the XML? Such questions I am
seeing many times during the interviews.
Solution : There are many libraries available, there are many web pages on these topics, so my concerns comes that why
does one need to store such in his/her mind rather keeping the mind free to digest the requirements with no
prejudices around known solutions.
Below are few examples/samples/ideas given, pick the one which suits your requirements/environment -
www.developer.com/java/ent/explore-the-java-api-for-json-processing.html
adam-bien.com/roller/abien/entry/converting_json_to_map_with
www.journaldev.com/2315/java-json-example
stackoverflow.com/questions/9593409/how-to-convert-pojo-to-json-and-vice-versa
So for such requirements one just need to know how to google, rather keep such in mind, as this way you can
get latest & better ideas to incorporate.
Question7 : How will you implement your custom HashMap or LinkedList? Provide 1-2 operations to add, iterate to fetch the
value or may be delete the value.
Solution : You can easily provide the basic implementation for these, but interviewer will try to unsettle you by comments like
Q1 : This kind of implementation is not in Java.
Q2 : This kind of method name is not in Java.
Q3 : What is the performance impact while using your custom implementation?
Q4 : Why to use your class & in what scenarios?
Just tell that person, that it is your implementation so you have given that as per the basic requirement, so you can also give
any method name there. But when you are giving your implementation then you must be able to understand it deeply & some
scenarios where it will work properly & correctly. And your class is custom implementation for particular kind of requirements,
so it will be used for those scenarios & may not be used for every general scenarios as you will be testing your code against those particular requirements only.
Question8 : How will you implement the immutability concept? It was asked in interview for SAS.
Solution : Such questions have been asked in many interviews. And definitely this concept looks straight & simple but it is
not. And also, it depends on the kind of mindset of your interviewer, like what s/he has read about this concept etc.
So you will need to understand the requirements & the purpose here, to frame your answer accordingly.
You can check for the ideas shared here.
Question9 : In case of multi-layered architecture, if DAO layer throws an exception then what action will you take in your
upstream layers?
Solutions : Though interviewer tries to get one straight answer then will drill down on that answer.
But truth is that there is no one straight answer for this & such questions are to make you uncomfortable & have
the upper hand on you. As such decision depends on the application architecture, business requirements, what
responsibilities are distributed across the layers, what kind of action you want to take for such actions etc.
You can't simple return the exception to the upper layers, as if such exception is compile time or checked one, then
upper layer has to take the responsibility to handle that & if it is runtime or unchecked then it can cause unplanned
failures at above layers.
You can't also handle every exception in one layer as may the above layer wants to take some different action.
Also you can't create the custom exception every time for such exceptions as creating a new exception is a costly
affair.
So decision needs thorough analysis of business requirements, application architecture & its design, one can't start
throwing random answers for such issues.
Question10 : Tell about shallow cloning & deep cloning.
Solution : I generally don't remember the method names but again this topic is worth reading every time, as some other APIs
or ways are out there. But yes, hit the google if you get any real work around this as there is way lot difference
between answering this for interview & implementing for the production.
Question 11 : How you will make a class immutable?
Solution : About this question, I have even checked with a person who has worked with ServiceNow, Microsoft, Google like
big companies & this person also sing the same song which we generally see in books or internet that you will
make class as Final, take care about setters/getters, take care about the references. But think again on the
question, do you really need to do all this to make a class immutable. I see the issue in the metality of such people
who just follow the books with no capability to think otherwise but still looking to draw fat checks.
So think, and let me know via e-mail about your thoughts on this question. Else check Here.
Question 12 : Find the lighter or heavier person/coin/marble in the given set
Solution : https://www.youtube.com/watch?v=4cm1DowaQCA&feature=em-uploademail
Question 13 : What is the difference between API & SDK?
Solution : First thing & the most important thing, agreement with this & your answer depends on from where your interviewer
has read the answer about this question & what mindset s/he has. So I am not claiming my answer as correct or
wrong here, you can just read & try to understand your way.
Solution : About this question, I have even checked with a person who has worked with ServiceNow, Microsoft, Google like
big companies & this person also sing the same song which we generally see in books or internet that you will
make class as Final, take care about setters/getters, take care about the references. But think again on the
question, do you really need to do all this to make a class immutable. I see the issue in the metality of such people
who just follow the books with no capability to think otherwise but still looking to draw fat checks.
So think, and let me know via e-mail about your thoughts on this question. Else check Here.
Question 12 : Find the lighter or heavier person/coin/marble in the given set
Solution : https://www.youtube.com/watch?v=4cm1DowaQCA&feature=em-uploademail
Question 13 : What is the difference between API & SDK?
Solution : First thing & the most important thing, agreement with this & your answer depends on from where your interviewer
has read the answer about this question & what mindset s/he has. So I am not claiming my answer as correct or
wrong here, you can just read & try to understand your way.