Mastering the Coding Interview: 11 Advanced Interview Questions for Java Developers
Hello, I'm going to share 32 questions that can help you to be prepared for an Interview, and not only that but also some important things we should know while we are coding.
These questions are divided into 3 sections, basics, advanced, and web. Today we'll talk about advanced questions.
PS: I'm not going to show you in-depth answers, if you haven't mastered these questions, I would recommend studying them more.
Java Advanced
1. Give and explain 2 design patterns of OOP.
Answer: Strategy, Template Method, Builder, Singleton, Flyweight, Observer, Factory…
If you are updated with the new features of Java, you probably already know better and easier ways of applying some design patterns.
If you don't know and would like to go deep into it, I recommend these articles by my friend Bruno Alves.
2. Is Java Multithread, what does that mean?
Answer: Yes, In Java, Multithreading refers to a process of executing two or more threads simultaneously for maximum utilization of the CPU.
If you would like to get deep into that, I wrote an article about the challenges of multithreading.
3. What are the problems we might have with concurrency?
Answer: DeadLock, Starvation
Starvation: Resource starvation can occur due to the lack of computer resources or the existence of multiple processes that are competing for the same computer resources.
DeadLock: is a situation in which two computer programs sharing the same resource are effectively preventing each other from accessing the resource, resulting in both programs ceasing to function.
4. What is the Synchronized reserved word, and what is it used for?
Answer: Ensures that only one thread accesses the resource concurrently.
5. Can you explain what the finally clause is for in a try-catch?
Answer: The finally clause is always executed after the try-catch, even if there is an exception.
6. What is try-with-resources?
Answer: It is an implementation of try-catch for elements that implement the Closeable interface, and with try-with-resouces we guarantee that the element will be closed after use.
7. What is SOLID, and how do you use them?
Answer: 5 design principles
S - Single-responsiblity principle
O - Open-closed principle
L - Liskov substitution principle
I - Interface segregation principle
D - Dependency Inversion Principle
Knowing the SOLID principles are essential, you can go deep on it here:
8. Do you know what is a clean code, can you give some examples?
Answer: Clean code is a practice to keep the code easier to understand. Some examples: 3 or fewer parameters, methods with few lines, small classes, and clear names...
If you would like to know more about this, I recommend reading the following article:
9. Do you know 2 examples of Garbage Collectors, and what is it?
Answer: Serial Collector, Parallel Collector, G1 (Garbage First), ZGC.
In Java, the process of deallocating memory is handled automatically by the garbage collector. It's also useful to know the 3 sections of the GC, the Young generation, the Old generation, and the Permanent generation.
10. What types of memories exist in the JVM?
Answer: Heap and non-heap. The Heap area is the memory block where objects are created or objects are stored. Non-heap is divided into 4 categories:
Class(Method) Area: used to store information about the classes that have been loaded. Constrained by MaxMetaspaceSize.
Stack/Thread: memory used by threads in the JVM. A function of the number of threads that are running. The Stack memory also stores primitive types.
Program Counter Register: A program counter register is linked with each JVM thread that carries out the task of a specific method.
Native Method Stack: Native method stacks are also called C stacks. They are not written in the Java language. For each thread, this memory is allocated when it is created.
11. Where primitives are stored in Java?
Answer: stack memory. Only local primitive variables and references to object (i.e. variable declared in method) are stored in stack. Others are stored in heap.
Conclusion
These are only some questions related to Java, to increase your chances of passing in an interview, I recommend going deep into each question. Furthermore, knowing deep these concepts is crucial to develop better applications, and also increase your productivity.
I hope this can help you, and if you know another question, please share it here in the comments.