Explore jobs
Find specific jobs
Explore careers
Explore professions
Best companies
Explore companies
Even if you’re an experienced software developer, you likely don't know all the nitty-gritty details of the Java programming language.
It’s important to thoroughly prepare before any interview, or risk being caught off guard and sending a negative first impression that doesn’t truly represent your abilities.
Overpreparation is especially crucial since many interviewers aren't actually developers themselves, but simply hiring managers given a list of questions and answers to ask candidates.
If a question happens to strike at one of your minor gaps in knowledge, the interviewer won’t recognize that you possess the overall fundamental programming skills that matter; they’ll just see that you failed to answer the question correctly.
In this article, we’ll provide you with 25 of the most common interview questions for Java developer positions. We’ll also give you sample answers and examples to help you prepare your own.
Looking for a job? These position are hiring now near you:
Here are 25 of the most common questions you’re likely to encounter during an interview for a Java developer position:
What access modifiers exist in Java and what do they mean? There are four access modifiers in Java:
Public. Methods, variables, and classes defined as public can be accessed by external methods from other classes.
Private. Methods, variables, and classes defined as private may only be called by other methods within the same class.
Default. If you don’t define a method, variable, or class using any other access modifier, it is accessible only within the same package.
Protected. If you define a class or method as protected, it may only be accessed by classes within the same package, sub-classes of the same parent class, or within the same class.
What is an Enum? Enum is a data type that is used to represent a fixed group of constants.
For example, we can define an Enum to represent the four cardinal directions:
enum cardDirections {
NORTH,
SOUTH,
WEST,
EAST,
}
We can then access its constants as follows:
cardDirections nextDirection = cardDirections.NORTH;
Enums are useful in place of other data structures such as arrays for sets of data with fixed and clearly defined members.
Everyone knows the four cardinal directions, so it makes intuitive sense to represent them using an Enum. This way, you can access each direction directly, instead of having to parse an array and find the appropriate index.
How do you write a custom exception? You’ll first have to create a custom exception class.
Example Answer:
public class ArticleNotFoundException extends Exception {
public ArticleNotFoundException(String message) {
super(message);
}
}
super(message) calls the constructor in the Exception parent class that constructs a new exception with the passed string.
You can then throw your newly defined exception in the same way that you would with built-in exceptions.
Can you have an empty catch block? Java won’t throw an error if you leave a catch block empty, but doing so isn’t recommended.
If you leave multiple catch blocks empty throughout your code, it’s going to be painful to debug your program and pinpoint the cause of it crashing or returning something unexpected.
Nobody enjoys crawling through their code line by line in debug mode.
What is JDBC API and how do you use it? JDBC (Java Database Connectivity) API allows you to connect with relationship databases, run SQL statements, and reference procedures stored in a database server.
Developers typically just use whatever tools are provided by their IDE of choice to create a JDBC connection.
If you’re using Eclipse, for example, just look up how to set up a JDBC connection through the IDE.
How do ArrayList and LinkedList differ? The two data types differ in a few major ways:
Storage. ArrayList uses a dynamic array to store its elements, while LinkedList uses a doubly linked list that points to the previous and next element in the list.
Manipulation. Whenever you add an element to an ArrayList, memory bits are shifted and the entire array is traversed.
Adding or removing elements to a LinkedList is less expensive since memory bits don’t need to be shifted. Instead, the reference link is simply changed.
For example, suppose you add an element to a LinkedList. Internally, all elements still occupy the same memory address. The only thing that’s changed is the previous last element in the list now contains a pointer to the new element you just added.
Usage. Using ArrayList is more efficient when your code needs to access data more often than it manipulates it. Conversely, LinkedList is preferable when your application frequently manipulates stored data.
What are class constructors? Constructors are methods used to initialize an object. They always have the same name as the class:
There are two types of constructors:
Default constructor: A default constructor is just a constructor that has no arguments.
Its purpose is to instantiate the class and initialize its instance variables with default values.
Parameterized constructor. Parameterized constructors are constructors that take inputs. You’ll typically use them when you want to provide specific values to initialize the class’ instance variables with.
Why doesn’t Java use pointers? Java was created so that developers don’t need to deal directly with pointers, as they can be unsafe and increase the complexity of programs.
Instead, JVM manages all implicit memory allocation.
What is the purpose of the final keyword? The final keyword has slightly different effects when defining a:
Variable. You can’t change a final variable once it has been assigned a value.
Method. Methods declared final cannot be overridden.
Class. Final classes can extend other classes, but can’t be extended by any of its subclasses.
Are Strings mutable or immutable in Java? Strings are immutable since they’re cached in the String pool.
When you update a string in Java, you’re actually creating a new one and assigning it that value.
What is a Map? A Map is an interface imported from the Util package that allows you to map values to unique keys.
Map cannot contain duplicate keys and each key can only map to a single value.
What is inheritance? Java inheritance allows you to implement new classes that inherit some of the properties of already existing classes.
The class that inherits properties from another is called the child class, while the original class is referred to as the parent class.
What is method overriding? To override a method means to implement a method in a child class that shares the same name, return type, and parameters as a method in its parent class, but differs in behavior.
The inherited method from the parent class can still be accessed by calling the method using the super keyword.
What is JVM? JVM, or Java Virtual Machine, is the environment that compiles and executes Java code.
Once you’re finished writing the source code for your Java program, you can compile it into bytecode by invoking JVM through the command line or by having your IDE do it for you.
JVM then starts your program and calls the main method in your code.
Since JVM is a standardized environment no matter what operating system you use, Java is considered platform-independent.
What is a while loop? A while loop is one of the several main control flow statements used in Java and other programming languages.
As long as the condition stated in the while loop equates to true, the code contained within the brackets of the while loop will repeatedly execute.
What is a singleton class and how do you implement it? Singletons are classes that can only be instantiated once per program execution.
You can create a singleton class by making its constructor private and providing a unique way to instantiate it.
Is Java 100% object-oriented? No, since it still uses some primitive data types, such boolean, int, and char.
What is the difference between a class and an object? An object is an instance of a class, while the class itself is the blueprint that defines the properties of and behaviors that are common to a number of objects.
You first create a file to define a class with its own instance variables and methods. Once your executed program instantiates that class, those instances are generally referred to as objects.
Which class is the superclass of all other classes? Object is the implicit superclass of all classes implemented in Java.
Can you declare a constructor as final? Constructors are not members and cannot be inherited by subclasses, even though they can be invoked. Consequently, they can’t be declared as final either.
If you try to declare a constructor as final, the compiler will just throw an error.
What is the purpose of the super keyword? Super is used to access methods of the same name from the superclass.
For example, suppose the parent class of a particular class implements a toString() method. You can access that method using super.toString() in a subclass.
What is the instanceOf operator? instanceOf is an operator that checks if an object is of a certain class.
For example, suppose you want to check if the variable c is an instance of the Cars class.
c instanceOf Cars would return a boolean that corresponds to the answer.
What is the difference between local and instance variables? Local variables are defined inside a specific method and cannot be accessed by the rest of the class.
This can take place at the start of a method so that it can be used method-wide. Local variables may also be defined within a block of code within a method, limiting its scope even further.
Instance variables, in contrast, are defined outside of any methods so that they have global scope and can be used by any of them.
Instance variables differ from constants, another type of global variable, in that they don’t have set values and must be instantiated. Each instance of the class needs to create its own copy of the instance variable to use it.
What is the difference between using equals() and ==? equals() and == are both used to evaluate equality between two variables.
== is a reference comparison and simply checks if two objects point to the same location in memory, rather than whether their values are equivalent.
The exception is with primitive types, such as ints. == is regularly used to check if two ints or chars are the same.
equals(), meanwhile, is used to compare the actual content of objects. The basic equals() method is defined in Java’s Object class.
You can override this method in your classes to implement your own logic to decide what makes two objects equal.
What is the significance of the main() method and why does it usually follow the same template? The main() method is the entry point for any Java program during runtime.
The most common template for writing the method is:
Public static void main(String[] args) {
//do something
}
It’s necessary to set the method public so that JVM can invoke it externally. This is also why main() is always declared static, so that it can be called without needing to instantiate the class.
main() is set to void since it doesn’t return anything. As soon as the main() method terminates, so too does the Java program.
JVM first executes the program by passing it an array of command line arguments, hence why String[] args is always used as the argument.
In addition to technical questions, you’ll likely be asked a variety of more general questions that assess your ability to work with others in a professional environment.
Here are some of the most common questions you need to prepare for:
Tell me about what you did at X job on your resume. Especially in software development, it’s common for applicants to include buzzwords and false claims in their resumes.
It’s extremely important to prepare for this question since the employer isn’t just testing your abilities, but your honesty as well.
Reference one or two of the accomplishments you listed under the job on your resume and expand on them.
Start by giving context and describing the state of the problem or challenge before you worked on it.
Next, explain the iterative approach you took and the specific technical tools you used, such as a particular library or algorithm.
By providing this level of detail, you won’t only demonstrate your technical competence but also reduce doubts the interviewer had about your resume’s claims.
This question is also one of many reasons why you should try to keep your resume brief.
The more detail your answer provides when compared with your resume, the more confident the interviewer will be in your true abilities.
Talk about a time you had to work with someone you disagreed with. It’s an unavoidable fact that you won’t mesh perfectly with all your coworkers.
When answering this situational question, cite an experience you’ve had that demonstrates:
Your ability to compromise. Employers want to know that you have the interpersonal skills to navigate disagreements.
Convey your ability to communicate your opinion, while simultaneously listening with an open mind to differing ones. Also, show that you’re able to bridge the gap and reach agreements even with individuals that you disagree with.
Your respect for the coworker. It’s important to show that no matter the situation, you always maintain a professional and respectful attitude when working with others.
Why are you the best person for the job? Rather than focusing on your technical skills, the key to answering this question is to personalize it.
You’ll leave a stronger impression by talking about your personality traits and values.
Start by stating a few positive personality traits, such as your leadership ability or constant dedication to self-improvement.
Next, describe a past experience that demonstrates the trait.
You can also link your personality trait to your technical skills by picking a past experience from one of the prior jobs on your resume.
This will remind the interviewer of the technical achievements you listed on your resume and legitimize your claims.
What are your career goals? Interviewers ask this question for a variety of reasons.
It’s important to know what these are so that you can address them in your answer:
To check that you’re dedicated to constant improvement. Employers want to see that you have ambitions to advance your career.
They’ll be more willing to invest in workers that develop their skills over time.
To make sure you won’t leave quickly. In your answer, you should align your goals with the company.
Employers try to avoid hiring employees that just want to use the position as a stepping stone towards higher-paying opportunities. This is especially common in the software development industry.
What is your greatest accomplishment? This is one of the more common open-ended questions you might encounter.
The key isn’t to just brag about your achievements. What the interviewer really wants to know is your:
Work ethic. Focus less on the achievement and more on the hard work and dedication you devoted to accomplishing it.
Values. The interviewer wants to know what types of goals and activities motivate you.
The details. Of course, they are also interested in the achievement itself. Try to link some of the skills you used to the duties you’ll have on the job.