If you have 10+ years of experience in the field of software development and more specifically in Java, it is very much necessary to know all the concepts of Java in depth. The interviewer can ask you the trickiest of questions that might confuse you and even rattle your foundation of what you know about Java. You need to be well-prepared to know the concepts in depth and prove why you are a developer with 5+ years of experience.
In this section, we will see what are the most commonly asked Core Java Interview questions and how you can answer them. We will cover a wide range of important Java topics that are the applications of variables, collections, polymorphism, strings, datatypes, and threads.
Java Interview Questions for Experienced
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.
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.
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. 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.
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.
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?
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?
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.
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.
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.
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).
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.
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 ConcurrentHashMap also achieves its scalability by allowing lock-free reads as read is a thread-safe operation.
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?
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?