Once a colleague asked me to interview a candidate together. He didn't read the resume carefully, so after asking about design patterns, he asked him to talk about his understanding of memory leaks and garbage collection. The candidate was stunned. It was only later that I found out that he was dealing with junior and intermediate development positions. I guess the candidates were also scolding me in their hearts.
I myself have interviewed candidates in a variety of ways, and have been interviewed in a variety of ways. Some let you use a computer to write code directly, some let you write ideas, draw structures on the whiteboard, or ask questions one by one. An interview should be about testing a candidate's "ability to do certain things", not "the ability to know certain things." While the two are often related, there is a large percentage of questions that don't do anything to "get things done." One of them is the problem of "you only know about it when you don't need it, and you can figure it out in a minute when you need to use it". Even if you are a developer who is more willing to learn, you usually see some things that are not so related to your work. things, and can be very clear, but after a long time, if I ask you for details, I think it will also be vague and even make mistakes.
This article is not a question bank for interview questions. Instead, I hope that by classifying the interview questions, Java engineers can help Java engineers build a strong position and quickly find the required knowledge points when preparing for the interview. At the same time, I will give some good and bad examples, hoping to help some people.
Classification of interview questions
Questions about Java that will be asked can be roughly divided into the following categories:
1. Issues related to Hotspot VM (Runtime, GC, JIT)
These questions are actually things to understand, and are good for guiding coding, troubleshooting, and understanding of runtime logic.
But if you have to ask you about the implementation methods of G1, CMS, and Serial garbage collectors and their differences, unless you are in a JVM tuning position, then you are out of luck, and the solution can only be a surprise attack before the interview Consolidate this knowledge.
2. The use of specific classes in the JDK
If your work will use some classes, you may be familiar with them, but to be honest, there are still many classes that have been used many times, and every time you have to go to the source code to see the comments...
Some commented that Mao was wasting precious brain capacity. ill
3. Implementation problems of specific classes (or operators, keywords) in JDK
It is believed that a large part of the work content of (experienced) engineers does not need to consider these issues, but many aspiring engineers are happy to explore these issues, such as HashMap
the implementation principle and TransferQueue
algorithm logic.
However , with so many categories, who can guarantee that they can remember them all clearly, if there are, they must have memorized them before coming to the interview.
4. Some other perverted traps
All kinds of questions that are becoming more and more fascinating, such as "the difference between overloading and rewriting", "the difference between final, finally, and finalize", etc., are really powerless to complain. The people who asked these questions probably wanted to check your basic skills, but they were impatient, so they dug a hole to see how you danced.
There is a kind of programming you don't need a search engine.
Change your posture here and ask, and the interviewer's compulsiveness will appear immediately. For example, "How does the JDK guarantee that the finally block in try-catch-finally will be executed?", but this belongs to the third type of question. If he can't answer it, he should be guided through his existing knowledge and his own ideas. to achieve.
5. Solve practical problems
This is a really valuable question, one that examines people's thinking and problem-solving abilities. And whether it is business-related or technical theory-related, valuable questions can be asked.
Such as "design one by yourself " ArrayList
, LinkedBlockingQueue
"design a seckill system", "if you encounter a XXX problem, you guess where the problem is" and so on.
6. Issues not related to Java
This kind of question is usually to examine the applicant's personality, character, knowledge, learning ability, etc., such as "Briefly describe the workflow of HTTPS", "Have you been reading books or knowledge recently", etc. Some have routines, which can be crammed and brushed out by brushing questions. Others can only ask for their own blessings.
Examples of related questions
The following are some questions I have seen on the Internet or have been asked myself, for reference only.
Type 1 questions
1. Briefly describe the GC of the JVM
A: Feel free to play... You can refer to the basics of GC tuning in Java 9
This is a typical open-ended question, and the interviewee can start from the aspects that they understand, such as generation and runtime memory structure. If you know more about the garbage collector, you can talk more freely.
If you are asked a more targeted question, then it depends on whether your pre-interview class is good or not.
2. What are the means of multi-thread synchronization in Java
This question can also extend a lot of knowledge, synchronized
the implementation principle synchronized
of lock expansion in China, why sometimes use volatile
can bring better performance, the concept of pessimistic locking & optimistic locking, a new generation of Lock
lock mechanism based on, and even more possible Will ask about Java's Memory Model (JMM).
second type of problem
1. StringBuilder
vs StringBuffer
A: The former is the unlocked version of the latter, using the scene BlaBla...
There are still people asking this question, and there are only two possibilities: 1. The level of the interviewer or the candidate is relatively elementary; 2. The interviewer can't think of anything else to ask.
2. What new features does Java8 add over Java7?
A: Lambda, streams, interface default methods...
If I had endorsed it, I would have studied liberal arts earlier.
3. What is the method of Java's own thread pool to judge whether the thread pool has finished running?
A:isShutdown和isTerminated。
4. BlockingQueue
, CountDownLatch
and Semeaphore
the usage scenarios
5. What is the difference java.util.Date
withjava.sql.Date
A: Inheritance relationship
Not sure what the point of this question is.
6. If you want LinkedBlockingQueue
to take out the header object from it, which method will return null, throw an error, or block
A: take - block, poll - return null, remove - throw error
third category of questions
1. ThreadLocal
The realization principle
ThreadLocal
A: It is an instance with an instance as the key that can only be accessed by the current thread HashMap
. Its internal Map implementation HashMap
is similar to that of . The instance of this Map is stored on the Thread
object, so through encapsulation, the thread can only access its own ThreadLocal
variables.
Well, chances are, that's what the interviewer wants to hear.
But the reason why ThreadLocal is important is actually the design idea behind it. It transfers variables from a shared environment that requires multi-thread synchronization to a thread-private environment that does not require synchronization. This idea can be used to solve many different scenarios. The problem.
But who cares about this?
2. LinkedList
The realization principle
A: It is a two-way list that implements interfaces such as List
, , Deque
, Cloneable
etc.
To be honest, Deque
(two-way queue) I have seen this thing a few times, but I haven't used it, so I don't know its features and actual usage scenarios, so I don't want to talk nonsense.
3. ConcurrentHashMap
The realization principle
A: It is a lock for the write operation . The HashMap
difference is that it is divided twice, and the elements are stored in different buckets to effectively lock the data range and improve performance.
This implementation has been modified in JDK8. The ConcurrentHashmap
CAS-based TreeBin
implementation in JDK8 does not need to lock segments or globals, but only needs to lock a single row (the same hashCode), and the following linked list is a linked list plus a red-black tree. Use CAS for modification of a single value.
This is likely what the interviewer wants to hear, but the important thing here is the concept of divide and conquer. In fact, it is entirely possible for the candidate to try to design one by himself ConcurrentHashMap
, and then guide him to split HashMap
it. This is the right way.
4. Relationship with hashcode()
andequals()
A: According to the JVM standard, equals()
equal objects hashcode()
should always be relative, otherwise not necessarily, see HashMap
the implementation for details.
This question is actually quite good, but if it is just a simple question, it doesn't make any sense, and the test is still memory.
5. TransferQueue
What does the algorithm look like and BlockingQueue
how is it different from
A: It is in the source code.
This answer is definitely not good, let's go to make up lessons. If you think the interviewer is too boring to ask this, you can reject it after getting the offer.
Category 5 Questions
1. Use wait-notify to write a piece of code to solve the producer-consumer problem, and further, how to solve it in a distributed environment
A: Hahaha, let's write some code...
wait()
And notify()
are methods of inter-thread communication, which can directly operate on the behavior of threads. Their essence is to transmit the respective messages of producers and consumers. If you understand this, it is very simple in a distributed environment, as long as you find a third-party medium (Zookeeper, Redis, Kafka) that can be used to transmit messages etc.) is fine.
2. Design a thread pool
A: You can refer to the theory and practice of Java thread pools
If you have a better understanding of the JDK thread pool java.util.concurrent.ThreadPoolExecutor
, you can put some simple features on it. If you don't know it, you can design a thread array directly, and then add some business logic. The so-called thread pool is basically the same.
3. Design an IOC container
A: Using reflection, annotations, and the theory of IOC
Category 6 Questions
1. Talk about your understanding of the C10K problem
A: The advantage of NIO to BIO in throughput, you can refer to from I/O model to Netty (1)
I have a WeChat public account, and I often share some dry goods related to Java technology; if you like my sharing, you can use WeChat to search for "Java Head" or "javatuanzhang" to follow.