When the experience of a Java Programmer grows in the years e.g. when it
goes from beginner years ( 2 to 4) to more experience or sort of senior
level ( 5 to 7 years),
Core Java Interview Questions
also change a bit. Of course, basics like data structure,
algorithms, and object-oriented programming remains the same,
but types of questions will become more advanced and their answers will
definitely need to be more detailed and accurate. I often receive queries
about core Java questions asked to a senior developer with 5 to 6-year
experience, or, sometimes, I am going for an interview of a senior Java
developer, what kind of questions I should expect.
This sometimes puzzles me, that once you become a senior, you automatically
start taking part in the interview, and you should have an idea of what to
expect on Interviews, but at the same time, I can understand that having an
idea of questions before going on Interview, helps preparation.
Of course, you are not going to get a question like the one you have faced
on
2 to 3 years level Java Interviews, but It also depends on different rounds of Interviews.
I have not seen many changes in the questions asked on the telephonic round, which almost remains the same. You will find some fact-based, some coding questions, and a few tricky questions. On the other hand face-to-face, interviews have become more detailed and more tricky, especially with nasty follow-ups.
In this article, I am going to share some 15 technical core Java Interview Questions, which I have seen asked senior and experienced developers of 4 to 6 years of experience in different interviews, mostly on telephonic rounds. I am not posting answers as of now, but you can find answers to most of the questions on here or on the Javarevisited blog.
If you are in a hurry and actively looking for a Java Developer Job, you can also take help from some good books written to prepare you for Java J2EE interviews. Books like, Java Programming Interview Exposed cover all important topics for both core Java and Java EE interviews, which include basic Java questions, data structure and algorithms, JVM internals and GC tuning, Hibernate and Spring interview questions, JUnit ant unit testing questions, and some Java 8 stuff.
It also covers knowledge of other JVM languages like Scala, Groovy, and other platforms like Android. A perfect companion to do well in Java interviews.
If you want to prepare more on the coding side then you can also check out Cracking the Coding Interview, which contains almost 150 programming questions and solutions from technical interviews of big tech companies like Amazon, Facebook, Google, Twitter, and Microsoft.
1. What is Busy Spinning? Why Should You Use It in Java?
I have not seen many changes in the questions asked on the telephonic round, which almost remains the same. You will find some fact-based, some coding questions, and a few tricky questions. On the other hand face-to-face, interviews have become more detailed and more tricky, especially with nasty follow-ups.
In this article, I am going to share some 15 technical core Java Interview Questions, which I have seen asked senior and experienced developers of 4 to 6 years of experience in different interviews, mostly on telephonic rounds. I am not posting answers as of now, but you can find answers to most of the questions on here or on the Javarevisited blog.
If you are in a hurry and actively looking for a Java Developer Job, you can also take help from some good books written to prepare you for Java J2EE interviews. Books like, Java Programming Interview Exposed cover all important topics for both core Java and Java EE interviews, which include basic Java questions, data structure and algorithms, JVM internals and GC tuning, Hibernate and Spring interview questions, JUnit ant unit testing questions, and some Java 8 stuff.
It also covers knowledge of other JVM languages like Scala, Groovy, and other platforms like Android. A perfect companion to do well in Java interviews.
If you want to prepare more on the coding side then you can also check out Cracking the Coding Interview, which contains almost 150 programming questions and solutions from technical interviews of big tech companies like Amazon, Facebook, Google, Twitter, and Microsoft.
15 Core Java Questions For 5 to 6 Years Experienced
All these questions have been collected from quite senior developers, which have at least 5 years of experience. They have seen these questions on different rounds of their core Java interviews, including telephonic and face-to-face rounds on different companies, mostly on Investment banks like Barclays, Morgan, RBS, and others.1. What is Busy Spinning? Why Should You Use It in Java?
One of the interesting multithreading question to senior Java programmers,
busy spinning is a waiting strategy, in which a thread just wait in a loop, without releasing the CPU for
going to sleep. This is a very advanced and specialized waiting strategy used in the
high-frequency trading application when the wait time between two messages
is very minimal.
By not releasing the CPU or suspending the thread, your thread retains all the cached data and instruction, which may be lost if the thread was suspended and resumed back in a different core of the CPU.
This question is quite popular in the high-frequency low latency programming domain, where programmers are trying for extremely low latency in the range of micro to milliseconds. See here more 50+ advanced thread interview questions for experienced programmers.
2. What is Read-Write Lock? Does ConcurrentHashMap in Java Use The ReadWrite Lock? ReadWrite Lock is an implementation of a lock stripping technique, where two separate locks are used for reading and write operations. Since the read operation doesn't modify the state of the object, it's safe to allow multiple thread access to a shared object for reading without locking, and by splitting one lock into the read and write lock, you can easily do that.
Java provides an implementation of a read-write lock in the form of the ReentrantReadWritLock class in the java.util.concurrent.lock package. This is worth looking at before you decide to write your own read-write locking implementation.
Also, the current implementation of java.util.ConcurrentHashMap doesn't use the ReadWriteLock, instead, it divides the Map into several segments and locks them separately using different locks. This means any given time, only a portion of the ConcurrentHashMap is locked, instead of the whole Map. See how ConcurrentHashMap internally works in Java for more detail.
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.
4. Which Design Patterns have You Used in Your Java Project?
By not releasing the CPU or suspending the thread, your thread retains all the cached data and instruction, which may be lost if the thread was suspended and resumed back in a different core of the CPU.
This question is quite popular in the high-frequency low latency programming domain, where programmers are trying for extremely low latency in the range of micro to milliseconds. See here more 50+ advanced thread interview questions for experienced programmers.
2. What is Read-Write Lock? Does ConcurrentHashMap in Java Use The ReadWrite Lock? ReadWrite Lock is an implementation of a lock stripping technique, where two separate locks are used for reading and write operations. Since the read operation doesn't modify the state of the object, it's safe to allow multiple thread access to a shared object for reading without locking, and by splitting one lock into the read and write lock, you can easily do that.
Java provides an implementation of a read-write lock in the form of the ReentrantReadWritLock class in the java.util.concurrent.lock package. This is worth looking at before you decide to write your own read-write locking implementation.
Also, the current implementation of java.util.ConcurrentHashMap doesn't use the ReadWriteLock, instead, it divides the Map into several segments and locks them separately using different locks. This means any given time, only a portion of the ConcurrentHashMap is locked, instead of the whole Map. See how ConcurrentHashMap internally works in Java for more detail.
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.
4. Which Design Patterns have You Used in Your Java Project?
Always expect some design patterns-related questions for the Core Java
Interview of senior developer position. It's a better strategy to mention
any GOF design pattern rather than Singleton or MVC, which almost every
other Java developer uses it.
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.
5. Do you know about Open Closed Design Principle or Liskov Substitution Principle?
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.
5. Do you know about Open Closed Design Principle or Liskov Substitution Principle?
Design patterns are based on
object-oriented design principles, which I strongly felt every object-oriented developer and the
programmer should know, or, at least, have a basic idea of what are these
principles and how they help you to write better object-oriented code.
I
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?
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.
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?
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.
Question 7. How do you prevent SQL Injection in Java Code?
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.
Question 7. How do you prevent SQL Injection in Java Code?
This question is more asked J2EE and Java EE developers than core Java
developers, but, it is still a good question to check the JDBC and Security
skill of experienced Java programmers.
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.
Question 8) Tell me about different Reference types available in Java, e.g. WeakReference, SoftReference, or PhantomReference? and Why should you use them?
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.
Question 8) Tell me about different Reference types available in Java, e.g. WeakReference, SoftReference, or PhantomReference? and Why should you use them?
Well, they are different reference types coming from java.lang.ref package and provided to assist Java Garbage Collector in a case of
low memory issues. If you wrap an object with WeakReference then it will be eligible for garbage collected if there are o strong
references. They can later be reclaimed by the Garbage collector if JVM is
running low on memory.
The java.util.WeakHashMap is a special Map implementation, whose keys are the object of WeakReference, so if only Map contains the reference of any object and no other, those objects can be garbage collected if GC needs memory. See Java Performance The Definitive Guide to learn more about how to deal with performance issues in Java.
Question 9) How does get method of HashMap works in Java?
The java.util.WeakHashMap is a special Map implementation, whose keys are the object of WeakReference, so if only Map contains the reference of any object and no other, those objects can be garbage collected if GC needs memory. See Java Performance The Definitive Guide to learn more about how to deal with performance issues in Java.
Question 9) How does get method of HashMap works in Java?
Yes, this is still one of the most popular core Java questions for senior
developer interviews. You can also expect this question on a telephonic
round, followed by lots of follow-up questions as discussed in my post how does HashMap work in Java.
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.
Question 10) Which Two Methods HashMap key Object Should Implement?
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.
Question 10) Which Two Methods HashMap key Object Should Implement?
This is one of the follow-up questions I was saying about in previous
questions. Since the working of HashMap is based upon hash table data
structure, any object which you want to use as a key for HashMap or any
other hash-based collection e.g. Hashtable, or ConcurrentHashMap must
implement equals() and hashCode() method.
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.
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.
Question 11) Why Should an Object Used As the Key should be Immutable?
Question 11) Why Should an Object Used As the Key should be Immutable?
This is another follow-up of previous core Java interview questions. It's
good to test the depth of technical knowledge of candidates by asking more
and more questions on the same topic. If you know about Immutability, you
can answer this question by yourself.
The short answer to this question is key should be immutable so that hashCode()
method always returns the same value.
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.
Question 12) How does ConcurrentHashMap achieve its Scalability?
Sometimes this multithreading + collection interview question is also asked
as, the difference between ConcurrentHashMap and Hashtable in Java. The
problem with
synchronized HashMap
or Hashtable
was that the whole Map is locked when a thread performs any operation with
Map.
The java.util.ConcurrentHashMap class solves this problem by using a lock stripping technique, where the whole map is locked at different segments and only a particular segment is locked during the write operation, not the whole map.
The java.util.ConcurrentHashMap class solves this problem by using a lock stripping technique, where the whole map is locked at different segments and only a particular segment is locked during the write operation, not the whole map.
The ConcurrentHashMap also achieves its scalability by allowing lock-free reads as read is a
thread-safe operation. See
here
for more advanced multi-threading and concurrency questions in
Java.
Question 13) How do you share an object between threads? or How to pass an object from one thread to another?
Question 13) How do you share an object between threads? or How to pass an object from one thread to another?
There are multiple ways to do that like Queues, Exchanger, etc, but
BlockingQueue using Producer-Consumer pattern
is the easiest way to pass an object from one thread to another.
Question 14) How do find if your program has a deadlock?
Question 14) How do find if your program has a deadlock?
By taking thread dump using kill -3, using JConsole or VisualVM), I suggest
preparing this core java interview question in more detail, as the
Interviewer definitely likes to go with more detail e.g. they will press
with questions like, have you really done that in your project or not?
Question 15. How do you avoid deadlock while coding?
Question 15. How do you avoid deadlock while coding?
By ensuring locks are acquired and released in an ordered manner, see here for a detailed answer to this question.
That's all on this list of Core Java Interview Questions for senior developers and experienced programmers. I haven't included a lot of questions from other important topics like Exception handling, Garbage Collection tuning and JVM Internals, which are also very popular among Java programmers with 5 to 6 years of experience, maybe I will include them in the next part.
References
Oracle's Java SE Tutorials
Java SE 8 API documentation
133+ Java Interview Questions from Last 5 Years
That's all on this list of Core Java Interview Questions for senior developers and experienced programmers. I haven't included a lot of questions from other important topics like Exception handling, Garbage Collection tuning and JVM Internals, which are also very popular among Java programmers with 5 to 6 years of experience, maybe I will include them in the next part.
References
Oracle's Java SE Tutorials
Java SE 8 API documentation
133+ Java Interview Questions from Last 5 Years
Related Java Interview Questions on other topics
By the way, if you don't find the answer to any of these core Java Questions, let me know. I may update the post with a more detailed option, based on my reader's request.
- Java Questions for Phone Screen Interviews (list)
- Thread and Concurrency Questions from Java Interviews (list)
- Java Enum Interview Questions and Answers (list)
- Java OOP Interview Questions with Answers (list)
- Spring MVC Interview Questions and Answers (list)
- Hibernate Interview Questions with Answers (list)
- JDBC Interview Questions and Answers (list)
- Array Concept Interview Questions in Java (list)
- RESTful Web Service Interview Questions (list)
- Servlet and JSP Interview Questions and Answers (list)
- Java Web Service Interview Questions and Answers (list)
By the way, if you don't find the answer to any of these core Java Questions, let me know. I may update the post with a more detailed option, based on my reader's request.
One way to answer your question related to SQL Injection is to use ORM Framework for Hibernate, which takes care of such issues by themselves.
ReplyDeletewhen you mention Spring is Immutable... This is String :)
ReplyDeleteI think so, String is Immutable, Spring is definitely not :)
DeleteGood questions but preparing just 15 20 questions won't be enough. See this mega list of Java interview questions for through preparation. It convers advanced topics like Concurrency, JVM internals, GC tuning, NIO and design patterns.
ReplyDeleteWhen you say technical interview questions, what does that mean? I am assuming for Programming Job interviews most of the rounds are technical round be it written test, phone interview or face-to-face interview, only exception is HR interview which happens last. Let me know if you have different meaning for technical interview here.
ReplyDeleteI would highly request to please answer question first instead of providing link links......to navigate on next page!
ReplyDeleteGood Collection
ReplyDeletereally good questions and answer but navigation from one page to another makes me crazy, please try to avoid linking multiple things for one questions, instead of that you can keep multiple open questions and answer set on same page.
ReplyDeleteVery good list but can you also add some questions on Java 8, Stream, Lambda expression and new Java features like Record, String in Switch, var and much more, this will be more aligned to what people are asked in recent Java interviews.
ReplyDeletehello Anonymous, I have shared few questions on Java Stream and Functional programming, you may want to check that. Yes, we are working to add more new questions to cover things like var, Record, and other new features.
DeleteGood collection of Core Java interview questions. https://www.java-success.com/
ReplyDelete