Java Interview Questions and Answers (2023) YASH PAL, 31 July 2024 Thinking of becoming a Java developer? I must say it’s a good choice! Java is continuously named the most popular programming language. And the average salary of a Java developer is about $105,800 annually. I think it’s a great motivation in itself. So, do you have what it takes to ace your first job interview? To help you check yourself and prepare for the upcoming interview, I’ve gathered some of the most popular questions with answers. This article is based on the CodeGym Java Course material. If you are not familiar – it is a practical online Java course with over 1200 tasks and exercises with instant validation. Make sure to check it out if you can’t answer some of the questions below. Basic Java interview questions for juniors Outline the major Java features. Answer: Object-oriented. Its classes and methods describe the state and behavior of an object. Platform independent. Java compiler converts the source code not into machine code but into byte code. Which is translated to machine code by Java Virtual Machine (JVM). JVM, in its turn, can execute byte code on any platform or OS. Architecture-neutral. The object file format the Java compiler generates is architecture-neutral. Which enables the compiled code to be executed on many processors. Multithreaded. Java allows the creation of interactive applications that run multiple tasks simultaneously. What is the difference between StringBuffer and StringBuilder? Answer: Both are mutable classes. Which allows modifying strings without the need to create an object. StringBuffer is thread-safe and synchronized, while StringBuilder is not. Since StringBuffer doesn’t allow for multiple threads to access at the same time, it is slower than StringBuilder. StringBuilder Example: public class StringBuilderExample{ public static void main(String[] args){ StringBuilder builder=new StringBuilder(“Hi”); builder.append(“Java 8”); System.out.println(“StringBuilderExample” +builder); } } StringBuffer Example: public class StringBufferExample{ public static void main(String[] args){ StringBuffer buffer=new StringBuffer(“Hi”); buffer.append(“Java 8”); System.out.println(“StringBufferExample” +buffer); } } What is the difference between JDK, JRE, and JVM? Answer: Java Virtual Machine (JVM) provides the runtime environment to execute Java byte code. Java Runtime Environment (JRE) is a set of files JVM requires at runtime. And Java Development Kit (JDK) is JRE plus a toolkit for developers to write and execute a Java program. What is a Java class? Answer: A class is a group of objects with common properties or methods. It is a user-defined template (also referred to as a blueprint or prototype) from which objects are created. Intermediate Java interview questions Define inheritance. Answer: In Java, one object can acquire states, behaviors, and all the other properties of a parent object. So you can build new classes upon already existing ones. And when this new class inherits from an existing one, the parent’s methods and fields can be reused. What are Access Specifiers and types of Access Specifiers? Answer: Access specifiers (also referred to as access modifiers or visibility specifiers) are entities used to restrict access to or hide a class, variable, or method from the other classes. Types of access specifiers: Public (visible to any class) Private Protected (visible within the class where defined) Default (no specifier) Can a constructor return a value? Answer: There is no “return value” statement in the constructor. But it returns the current class instance. So yes, a constructor can return a value implicitly, not explicitly. Compare method overloading and overriding. Answer: Overloading refers to two or more methods of the same class having the same name but different parameters. Overriding refers to two methods with the same name and parameters, but one is in the parent class, and the other is in the child class. Overloading is resolved at the compile time, and overriding is resolved at method runtime. Advanced Java interview questions for experienced developers Explain why Inheritance is less advantageous than Composition. Answer: Both composition and inheritance allow to reuse of code. But Java does not support multiple inheritances. So if you need multiple features, it is easier to have them as private members. With Composition, you can run unit tests of a class composing a different class with a mock object representing the composed class. This is impossible with inheritance since the composed class cannot be tested without the help of the superclass if you opt for inheritance. The composition is more flexible. With it, you can easily replace the updated version of the composed class implementation. It also prevents encapsulation from breaking. If a child class depends on the action of the parent class for its function, the change in the parent class behavior will break the child class functionality. Here’s an example: package comparison; public class Top { public int start() { return 0; } } class Bottom extends Top { public int stop() { return 0; } } In the example, inheritance is followed. Now let’s add some modifications to the Top class: public class Top { public int start() { return 0; } public void stop() { } } If we follow the new implementation of the Top class, we are bound to get a compile-time error in the Bottom class. Incompatible return type is there for the Top.stop() function. To ensure compatibility, we’ll need to apply changes to either the Top or the Bottom class. The composition technique can be used to solve the problem: class Bottom { Top par = new Top(); public int stop() { par.start(); par.stop(); return 0; } } Is exceeding the memory limit possible despite the presence of a garbage collector? Answer: Yes. If an object is unreachable in a program, then garbage collection is executed in regard to that object. If the memory limit is not sufficient to create a new object, then the memory limit is released for the objects that are no longer in the scope. This is done with the help of the garbage collector. A program goes out of memory when the released memory is not enough to create new objects. The heap memory can also be exhausted if the created objects remain in the scope and continue to consume memory. So the objects should be dereferenced after their work is done. Describe the Java thread lifecycle Answer: A Java thread always exists in one of these states: New. A new thread is always created in the New state. This is a state when the code has not been executed yet. Active. When the start() method is invoked, the thread is moved from the New state to the Active state. This state has two states within it: runnable and running. Once the start() method is invoked and before the run() method is called, the thread is in the runnable (ready to run) state. As soon as the run() method is invoked and the thread starts executing, it is in a running state. Blocked/Waiting. If a thread is inactive for a period of time but not permanently, then it is either in the Blocked or in the Waiting state. A blocked state occurs when a thread is unable to enter a synchronized block because another thread is already operating in the block on the same object. The Waiting state occurs when a thread is waiting for a signal to start execution from another thread. Terminated. This state occurs when a thread is done with its job. These were just a few examples of the questions you might get in a Java interview. Whether you are a junior or a senior, preparing for a Java interview should include refreshing both theoretical and practical knowledge. You should also be prepared for tricky and seemingly trivial questions. But generally, an interview is meant to test a developer’s ability to apply the language to solve real-life problems. So if you’ve been practicing, you will ace the interview and get your dream job easily. Interview questions answers Java Tutorials computer scienceinterview preparationjava