Java Ques
Java Ques
This core Java question is also very popular on senior and more experienced level Java
interviews e.g. 4 to 6 years, where you expect the Interviewer to go into more detail, like
by asking you to provide an implementation of the read-write lock with different policies.
If you are an experienced Java programmer, consider reading Java Concurrency in
Practice to gain more confidence about multithreading and concurrency in Java.
3. How to Make an Object Immutable in Java? Why Should You Make an Object
Immutable? Well, Immutability offers several advantages including thread safety, ability
to cache and result in a more readable multithreading code. See here to learn how to
make objects Immutable. Once again, this question can also go into more detail and
depending on your answer, can bring several other questions e.g. when you mention
Spring is Immutable, be ready with some reasons on Why String is Immutable in Java.
Your best bet can be a Decorator pattern or maybe a Dependency Injection Pattern,
which is quite popular in the Spring Framework. It's also good to mention only the
design patterns which you have actually used in your project and know its tradeoffs.
It's common that once you mention a particular design pattern say Factory or Abstract
Factory, Interviewer's next question would be, have you used this pattern in your
project? So be ready with proper examples and why you choose a particular pattern.
You can also see this article for more advanced design pattern questions from Java
interviews.
f you don't know the answer to this question, you can politely say No, as it's not
expected from you to know the answer to every question, but by answering this
question, you can make your claim stronger as many experienced developers fail to
answer basic questions like this. See Clean Code to learn more about object-oriented
and SOLID design principles.
6. Which Design Pattern Will You Use to Shield Your Code From a Third Party
library Which Will Likely to be Replaced by Another in a Couple of Months?
Read more: https://www.java67.com/2013/07/15-advanced-core-java-interview-
questions-answers-senior-experienced-5-6-years-programmers-
developers.html#ixzz889zzPTNc
This is just one example of the scenario-based design pattern interview question. In
order to test the practical experience of Java developers with more than 5 years of
experience, companies ask this kind of question.
You can expect more real-world design problems in different formats, some with more
detailed explanations with context, or some with only intent around.
One way to shield your code from a third-party library is to code against an interface
rather than implementation and then use dependency injection to provide a particular
implementation. This kind of question is also asked quite frequently to experienced and
senior Java developers with 5 to 7 years of experience.
You can use PreparedStatement to avoid SQL injection in Java code. Use of the
PreparedStatement for executing SQL queries not only provides better performance
but also shield your Java and J2EE application from SQL Injection attack.
On a similar note, If you are working more on Java EE or J2EE side, then you should
also be familiar with other security issues including Session Fixation attacks or Cross-
Site Scripting attacks, and how to resolve them. These are some fields and questions
where a good answer can make a lot of difference in your selection.
The short answer to this question is that HashMap is based upon hash table data
structure and uses the hashCode() method to calculate hash code to find the bucket
location on the underlying array and the equals() method to search the object in the
same bucket in case of a collision. See here to learn more about how does get()
method of HashMap works in Java.
The hashCode() is used to find the bucket location i.e. index of the underlying array
and the equals() method is used to find the right object in a linked list stored in the
bucket in case of a collision.
By the way, from Java 8, HashMap also started using a tree data structure to store the
object in case of a collision to reduce the worst-case performance of HashMap from
O(n) to O(logN). See the article for learning more about how does HashMap handless
collisions in Java.
Since the hash code returned by the hashCode() method depends on the content of
the object i.e. values of member variables. If an object is mutable then those values can
change and so is the hash code. If the same object returns a different hash code once
you inserted the value in HashMap, you will end up searching in different bucket
locations and will not be able to retrieve the object.
That's why the key object should be immutable. It's not a rule enforced by the compiler
but you should take care of it as an experienced programmer. See the article for more
advanced Java Collection interview questions.