Mastering the Coding Interview: 13 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 basic questions.
PS: I'm not going to show you in-depth answers, if you haven't mastered some of these questions, I would recommend studying them more.
Java Basics
1. How should we compare two objects?
Answer: With the Equals. Or using Objects.equals to avoid NPE
2. What are the 4 pillars of Object Orientation?
Answer: abstraction, encapsulation, polymorphism, and inheritance You have to know deep each one to explain it.
If you would like to know more about OOP, I wrote an article about it:
3. What are access modifiers in Java?
Answer: default, public, private, protected You have to know deep the differences between them.
4. Let's say I have a list of objects and I want to execute the contains method, what method does my object need to override for me to use the contains in the best way possible?
Answer: Equals and HashCode
5. Is String immutable? If so, what does it mean to be immutable?
Answer: Yes, the object is not changed, a new object is created, and String uses the Immutability Patterns design.
6. What is the best class to deal with money in Java?
Answer: BigDecimal, the Decimal has a problem with rounding.
7. What is the difference between List and Set?
Answer: Set does not accept duplicate elements and does not have indexers, so the order is not important and does not allow searching by index.
8. Which is more performant to perform contains method, ArrayList or HashSet?
Answer: HashSet O(1), ArrayList O(n)
If you would like to know more about Big O notation, I wrote about that in this article:
9. Which method is used to count elements in a stream?
Answer: Count, this is just a simple question about stream, you should know more about it to be able to respond to different types of questions about Stream.
10. Which method is used to filter a stream?
Answer: Filter
11. What is a functional interface? What is the difference between Consumer and Predicate?
Answer: Interface functional in Java is an Interface with only one default method. The Consumer receives an element and returns void, forEach for example. The Predicate takes an element and returns a boolean. This question talks about the difference between two functional interfaces, but you should also know the others.
12. What is Optional, and what is it for?
Answer: Prevent NullPointerException.
13. Final is a reserved word in Java, how can we use it?
Answer: With a variable to make it final, making it impossible to assign a new instance. With class to block extensions. With a method to block override.
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.