15 questions on Core Java for programmers with 5-6 development experience
All questions were received from working senior developers with over 5 years of experience. The developers faced these questions at various stages of the hiring process, including phone interviews and face-to-face interviews.-
How does a
get
class method workHashMap
in Java?Yes, this is still one of the most popular questions for a senior developer, you should expect it on a phone interview, after it there will probably be many related questions, see the answers to them here .
-
What 2 methods should the key object implement
HashMap
?equals
Andhashcode
-
Why do objects used as a key need to be immutable?
So that the hash code always returns the same value.
-
What makes it possible to
ConcurrentHashMap
achieve scalability?Sometimes this interview question sounds like: the difference between
ConcurrentHashMap
andHashtable
in Java, look for answers here . -
How to make an object shared between different threads? Or how to pass an object from one thread to another?
There are many ways to implement this, such as queues, exchangers, but blocking queues with the Producer/Consumer design pattern are the easiest way to pass an object from one thread to another.
-
How do you know if your program has a deadlock?
(By getting a thread dump using kill -3 using JConsole or VisualVM). I suggest preparing for this interview question very carefully, because interviewers love details, they ask if there were similar problems in your project and how you managed to solve them.
-
How to avoid deadlocks while coding?
To understand blocking and get full information about the topic, read this .
-
What is Busy spinning? Why should you use it?
One of the most interesting topics in multithreading for a senior Java developer, Busy spinning is a waiting strategy, where a thread performs a wait in a loop, while not using processor resources and, as it were, falling asleep . This strategy is used when the waiting time is very short, while the processor is not loaded and the thread is not stopped, all data that can be lost when the thread starts on another processor core is saved. This question is popular among programmers who create high-load projects, where programmers achieve extremely low latency in work, in the range of micro- and milli-seconds.
-
What is a Read/Write (
ReadWriteLock
) lock? Does it useConcurrentHashMap ReadWrite
locks?A Read/Write lock is an implementation of a lock where multiple threads attempt to perform a read/write operation on the same object. the read operation itself does not modify the object, which allows multi-threaded read operations without locks. Java provides an implementation
ReadWriteLock
worth reading. For example ,ConcurrentHashMap
it does not applyReadWriteLock
, instead it dividesHash
into separate parts and blocks them separately, so at any given time only part of the hash is blocked, and not all of it. This question is popular with experienced developers, usually asking for more details, asking for different implementations of ReadWriteLock for different cases. -
How to make an object immutable (not mutable) in Java? Why make an object immutable?
Immutability offers several benefits including thread safety, cacheability, and makes multithreaded code more readable.
Watch this and learn how to make objects immutable. At the interview on this issue, additional clarifications are possible, depending on the completeness of your answer. For example, when you say that Spring is immutable, be prepared to answer why strings are also immutable in Java.
-
What design patterns do you use?
Always expect a question about design patterns in a senior developer job interview. It is better to mark any GOF pattern, and not Singleton or MVC which is used by every second Java programmer. A better answer might be the Decorator pattern or the Dependency Injection pattern , which are quite popular in the Spring Framework. It is also good if you actually used the design patterns you call and know what compromise their application leads to. As soon as you say the name of the design pattern, for example: "Factory", the interviewer will immediately ask: "Have you used this in your projects?" Therefore, be prepared to give examples and tell why you chose this particular template in your project.
-
Do you know about the Open/Closed Principle or Barbara Liskov's Substitution Principle ?
Design patterns are based on the principles of object-oriented design.
I highly recommend checking out my article 10 Object Oriented Design Principles that a Java programmer should know, at least to have an idea of how these principles will help you write object oriented code. If you don't have an answer to this question, you can politely answer "no", no one expects you to know the answers to all questions. However, knowing the answer to a question that most developers struggle with makes you a very strong candidate for an interview.
- What design pattern will you use to protect your code from a third party library that will be replaced in a couple of years?
This is just one of the possible variations of the question about design patterns that you may be asked depending on what you are talking about now in the interview. One way to shield your code from a third party library is to depend on the interface rather than the implementation and use dependencies to provide a concrete implementation. Similar questions are often asked to senior developers with 5-7 years of experience.
-
How to prevent SQL injection in Java code?
This question is asked more often by Java EE developers than by regular Java programmers, but it's still a great way to learn about PreparedStatement. PreparedStatement is an object that represents a precompiled SQL statement. PreparedStatement not only provides better performance, but also protects against SQL injection. If you work mostly with Java EE or J2EE, you should be familiar with some security issues, including the "Session Fixation attack" or "cross-site scripting", and you should be able to avoid these attacks.
-
Can you tell us about the difference between reference types in Java such as WeakReference, SoftReference and PhantomReference? And why should you use them?
In fact, there is only one difference between all types of links - the behavior of the Java Garbage Collector with the objects they refer to. In Java, objects created with the new operator are created with a strong reference. The garbage collector (garbage collector) destroys such objects only when there are no more strong references to them. The java.lang.ref package has 3 classes that describe 3 types of links, respectively SoftReference, WeakReference, PhantomReference. Objects created via SoftReference will be collected in case the JVM needs memory. That is, there is a guarantee that all SoftReference objects will be collected before the JVM throws an OutOfMemoryError exception. SoftReference is often used for caches that consume a lot of memory. WeakReference does not save object from finalization, even if there is enough free memory. As soon as there are no strong and soft references left on the object, it can be finalized. Used for caches and for creating chains of related objects. Objects created via PhantomReference are destroyed when the GC determines that the referenced objects can be freed. This link type is used as an alternative to finalization (for more flexible release of resources).
This is the entire list of interview questions for a senior Java developer. I didn't include many important questions about exception handling, garbage collector, JVM tuning that are very popular with senior developers, maybe I will include them in the next part. But if you don't find answers to any of the questions, let me know and I'll update this post for my regular readers.
GO TO FULL VERSION