As a widely used pro­gram­ming language, Java and its unique features are well known to re­cruiters. This is why questions about various aspects of Java are often asked in the early stages of an interview. We’ve compiled a list of ten fre­quent­ly asked questions for Java de­vel­op­ers and have included answers to each one as well.

This article provides an overview of the potential questions you may be asked about Java in a developer job interview. Depending on the level of pro­fes­sion­al ex­pe­ri­ence required for the role, some questions may be more detailed. Sometimes in in­ter­views for senior roles, scenarios are presented that contain de­lib­er­ate errors or are clearly mis­lead­ing in order to test the technical knowledge of the person in­ter­view­ing for the position. This is why it’s important to be well prepared for your interview.

Question 1: What are Java’s special features and what ad­van­tages does the pro­gram­ming language offer?

Java is a powerful and widely used language, which has made it a popular pro­gram­ming language to learn. Several unique features make Java stand out, par­tic­u­lar­ly in how it’s used. A key advantage of Java is that its code can run on various operating systems without any mod­i­fi­ca­tions.

This flex­i­bil­i­ty is enabled by the Java Virtual Machine, which ensures de­vel­op­ment and ap­pli­ca­tion aren’t re­strict­ed to a specific platform, allowing the code to be used across multiple platforms. This ensures programs run flexibly and ef­fi­cient­ly on various devices.

Another advantage of Java is the automatic memory man­age­ment provided by the Garbage Collector, which sim­pli­fies memory resource man­age­ment and reduces potential errors. Ad­di­tion­al­ly, Java’s extensive standard libraries of many ready-made functions ac­cel­er­ates the de­vel­op­ment of ap­pli­ca­tions.

As an object-oriented pro­gram­ming language, Java, like other languages in this category, excels because software com­po­nents can be modeled more easily, and the code can be reused re­peat­ed­ly.

Question 2: How does Java deal with multiple in­her­i­tance?

In principle, Java doesn’t support multiple in­her­i­tance of classes. This means classes can only inherit from a single class. Multiple in­her­i­tance can neg­a­tive­ly impact code in object-oriented pro­gram­ming, a well-known issue being the diamond problem.

Although multiple in­her­i­tance isn’t possible for classes in Java, this can be solved using in­ter­faces. Since a class can implement several in­ter­faces in Java, it can inherit func­tion­al­i­ties from different sources. This as­sign­ment ensures that the functions are clearly defined and problems with multiple in­her­i­tance cannot occur.

Question 3: What’s the dif­fer­ence between an abstract class and an interface in Java?

Both abstract classes and in­ter­faces are used in Java to define abstract types, which can then be im­ple­ment­ed by other classes. There are sig­nif­i­cant dif­fer­ences, par­tic­u­lar­ly when it comes to in­her­i­tance rules:

  • In­her­i­tance: In Java, a class can only inherit from an abstract class. Abstract classes in turn cannot perform multiple in­her­i­tance, meaning that several in­ter­faces must be im­ple­ment­ed for this.
  • Class re­la­tion­ship: Since a class can only inherit from an abstract class, the use of abstract classes is well suited for “is a” re­la­tion­ships, while in­ter­faces are better suited for “can” re­la­tion­ships.
  • Con­crete­ness: An abstract class can contain abstract (unim­ple­ment­ed) and concrete (im­ple­ment­ed) methods and can also have instance variables. In contrast, in­ter­faces can only define abstract methods and constants. All methods in an interface are im­plic­it­ly abstract and public.
  • Func­tion­al­i­ty: Abstract classes rely on sharing a common im­ple­men­ta­tion (and im­ple­ment­ing multiple in­ter­faces). In­ter­faces, on the other hand, are designed to declare specific functions, which are then im­ple­ment­ed in different classes.
Web Hosting
Hosting that scales with your ambitions
  • Stay online with 99.99% uptime and robust security
  • Add per­for­mance with a click as traffic grows
  • Includes free domain, SSL, email, and 24/7 support

Question 4: What’s the dif­fer­ence between instance variables and local variables?

The main dif­fer­ence between instance variables and local variables lies in their scope and lifetime. Instance variables are mainly prop­er­ties of an object within a class. Local variables represent tem­porar­i­ly created values in a specific scope.

Instance variables

  • Instance variables are variables declared at the class level, outside of methods, con­struc­tors and blocks.
  • Each object of a class has its own copy of an instance variable.
  • Instance variables are accessed through the instance of a class. The values can differ for each object of the class.

Local variables

  • Local variables are declared within a method, a con­struc­tor or a block. Their validity is limited to this defined area.
  • These variables must be ex­plic­it­ly ini­tial­ized before use and exist only for the duration of the code block’s execution.
  • They are not visible outside the code block where they were declared.

Question 5: What do the terms JVM, JDK and JRE mean and how do they differ from each other?

Although these terms may appear similar when ab­bre­vi­at­ed, their un­der­ly­ing tasks and scope within Java are fun­da­men­tal­ly different.

Java Virtual Machine (JVM)

  • The Java Virtual Machine (JVM) is a virtual machine that serves as the interface between the Java program and the un­der­ly­ing hardware or operating system, executing Java bytecode.
  • The JVM is crucial because, as a runtime en­vi­ron­ment, it can execute the same bytecode on different operating systems, provided it is available on the re­spec­tive platforms. This con­tributes sig­nif­i­cant­ly to the porta­bil­i­ty of Java.
  • This porta­bil­i­ty is enabled by the Java compiler trans­lat­ing Java source code into bytecode, which the JVM then in­ter­prets.

Java De­vel­op­ment Kit (JDK)

  • The Java De­vel­op­ment Kit (JDK) is the complete de­vel­op­ment package, which bundles various tools that assist in the de­vel­op­ment of Java ap­pli­ca­tions. It includes tools for creating, compiling and debugging ap­pli­ca­tions.
  • The JDK contains the Java compiler, the Java Virtual Machine (JVM), Java debugger, and Java profiler.
  • In addition to these tools, the JDK includes a large number of pre­de­fined classes and in­ter­faces for fre­quent­ly used functions that are available in the Java API or the Java class library.

Java Runtime En­vi­ron­ment (JRE)

  • The Java Runtime En­vi­ron­ment (JRE) provides a reduced en­vi­ron­ment where Java ap­pli­ca­tions can be executed.
  • The JRE contains the Java Virtual Machine (JVM) and the Java API, which are both necessary for starting and running ap­pli­ca­tions.
  • De­vel­op­ment tools, such as the Java compiler, are not included in the JRE, so it is typically installed by end users.

Question 6: What are col­lec­tion classes in Java and what are they used for?

In Java, the term col­lec­tion class usually refers to classes that are part of the Java Col­lec­tions Framework. This framework provides a stan­dard­ized way to store, organize and ma­nip­u­late groups of objects. It consists of various in­ter­faces and concrete im­ple­men­ta­tions of data struc­tures.

Col­lec­tion classes are used for various purposes:

  • Data or­ga­ni­za­tion: They enable the efficient or­ga­ni­za­tion of data in lists, sets or maps.
  • Data ma­nip­u­la­tion: They provide methods for adding, removing and searching elements.
  • Generic pro­gram­ming: The use of generic types in col­lec­tion classes enables the creation of reusable and type-safe code.
  • Al­go­rithms: The framework also contains al­go­rithms that operate on data struc­tures (e.g., sorting or searching).

Question 7: What’s the dif­fer­ence between == and equals() in Java?

== and equals() are two different mech­a­nisms or operators that are used to compare objects:

  • The == operator compares the ref­er­ences of objects, not their content values. When used with objects, == checks whether the two ref­er­ences point to the same object (i.e., they refer to the same memory area). In contrast, with primitive data types (e.g., char, int or byte), the operator matches the values.
  • With equals(), you can compare the content in objects to determine if the objects are the same, even if they are in different memory locations. By default, equals() behaves like ==, in­her­it­ing the reference com­par­i­son logic from the Object class. However, it usually needs to be over­rid­den in user-defined classes to enable mean­ing­ful content com­par­i­son.

Question 8: What are con­struc­tors used for?

Con­struc­tors are special methods within a class used to in­stan­ti­ate and ini­tial­ize objects. The four main tasks of con­struc­tors are:

  • Object ini­tial­iza­tion: Con­struc­tors are primarily used to put an object into a valid and ini­tial­ized state as soon as it is created. At­trib­ut­es are ini­tial­ized, and the necessary resources are allocated.
  • Parameter passing: Con­struc­tors can accept pa­ra­me­ters to create different instances of the class with varying prop­er­ties, allowing objects with specific attribute values to be created.
  • Code man­age­ment: Using con­struc­tors improves code read­abil­i­ty, as object ini­tial­iza­tion can be performed directly in the con­struc­tor. This also sim­pli­fies code main­te­nance, since changes within the ini­tial­iza­tion logic only need to be made in one place.
  • In­her­i­tance: Con­struc­tors play an important role in in­her­i­tance hierarchy. A derived class typically calls the con­struc­tor of the base class to handle its ini­tial­iza­tion before per­form­ing its own ini­tial­iza­tions.
Tip

In addition to questions about in­ter­faces, classes and the like, there are other areas of Java that may come up in an interview. Here are ad­di­tion­al articles you can read through to prepare yourself:

Question 9: Java uses a number of different string types. What are they and how do they differ from each other?

In Java, the string type is rep­re­sent­ed by the class java.lang.String. This class is the primary way to represent character strings. It also provides various options for string ma­nip­u­la­tion and pro­cess­ing:

  • String­Builder: This class is used to ef­fi­cient­ly create mod­i­fi­able strings in Java. Unlike the immutable String class, it allows changes without gen­er­at­ing a new instance.
  • String literal: This refers to character strings that are written in double quotation marks, for example "Nice to see you!". Identical string literals share one instance in the string pool to optimize memory usage.
  • String objects: These can be created as a new instance by using the new keyword, for example as new string ("Nice to see you!"). The instance is created in­de­pen­dent­ly of the content.
  • String­Buffer: Like String­Builder, this class creates mod­i­fi­able strings. The main dif­fer­ence is that String­Buffer is thread-safe, whereas String­Builder is not.

Question 10: What dis­tin­guish­es throw from throws?

In Java, the keywords throw and throws are both used to handle ex­cep­tions. Despite this sim­i­lar­i­ty, they serve different purposes and are applied in different contexts:

  • throw is used to throw an exception manually. De­vel­op­ers can use this to generate an exception and pass it to the calling program.
  • In contrast, throws indicates that a method is capable of throwing a specific exception. It is used in the method de­c­la­ra­tion to specify which ex­cep­tions may not be handled by the method but instead passed on to the caller for handling, allowing the code to react ac­cord­ing­ly.
Go to Main Menu