How to Answer Top Java Interview Questions

Need Answers to the Top 10 Interview Questions?

Get our free eBook guide of sample answers and expert interview advice emailed to you now

How to Answer Top Java Interview Questions

Hiring experts agree that Java is one of the most in-demand tech skills. But how do you get one of those hot jobs? First you have to artfully ace those pesky Java interview questions.

We asked John Zukowski, an experienced Java pro who has also interviewed and hired many developers, to provide his expert advice on how to prepare for the most common Java interview questions.

Interviewing for a Java-related job can be difficult. You might be going for a typical software development job, but the range of questions you can face makes the SATs seem easy.

With this article, you will be better prepared to know the types of questions that you might run across, and more importantly, how to answer the questions, no matter what they are.

With each release of the Java platform, the API set grows, thus, the range of potential Java interview questions grows regularly. List Java on your resume, and you can get asked a question on anything from the first to the last release. Add in the Enterprise and Micro Edition, and the API-related questions grow even further.

Whether you’re fresh out of school or someone who has been programming for a while, we’ll prep you here for that next interview, as just knowing the API isn’t all there is to the Java interview.

The Java Job Interview

The best way to prepare for your interview starts with having a realistic resume. If you put on your resume that you’ve used JDBC or Security, you better be prepared for someone to ask you a question about those technologies.

The more specific a technology, like Hibernate, the higher the likelihood of getting asked a question about it, if it is a technology the company is interested in your specific experience. Don’t put technologies on your resume that you think might help with automated keyword searches, but your exposure to the technologies is so sparse or non-existent that you can’t answer a basic technical question about the technology.

You will be expected to know computer science fundamentals. How much of that depends on your level. These computer science questions may actually be easier for the more beginning developer, as someone straight out of college might have just had a course on algorithms perhaps. Think data structures and performance-related here.

The Interview Coding Test — How to Work the White Board

java interview questions
The more experienced you are, the more you will be expected to know how to code AND how to read code. Expect to have to go to a white board and solve a problem. Get comfortable doing it.

When coding the solution to a problem on a white board in a job interview, talk through your solution. It may seem obvious, but don’t just code the whole solution silently and ask if the questioner has any questions when you are done.

Don’t just code a brute force solution. More than likely, that isn’t what the questioner is looking for. Also, once completed, the interviewer could just move on to the next question. If not, you’ll be taking up valuable time correcting to the right solution after, and more importantly, you might do it wrong.

When responding to coding problems, there are a few guidelines to follow. Some may seem obvious, but all are worth reminding.

  1. Listen carefully to the questions. If you’re unsure of a question, ask for clarification, don’t just answer what you think the interviewer might be looking for.
  2. Don’t make assumptions. If asked to code something like a binary search, don’t just assume an array of numbers.
  3. Listen for hints. If your interviewer gives you a hint, don’t miss it, but more importantly don’t ignore it. They’re trying to push you in the right direction to an answer.

Questions About Computer Science Fundamentals

The most common set of questions you’ll face test your knowledge of computer science. Why do you pick one data structure over another? How do you code a particular algorithm?

It might seem simple, but could you code binary search in the Java programming language?

Here is the implementation of the algorithm from the java.util.Arrays class for an int array source:

    private static int binarySearch0(int[] a, int fromIndex, int toIndex, int key) {
        int low = fromIndex;
        int high = toIndex - 1;

        while (low <= high) {
            int mid = (low + high) >>> 1;
            int midVal = a[mid];

            if (midVal < key)
                low = mid + 1;
            else if (midVal > key)
                high = mid - 1;
                return mid; // key found
        return -(low + 1);  // key not found.

First, could you code this off the top of your head? Second, do any lines look different than your knowledge of the algorithm? What does the “>>> 1” do? That’s basically a divide by two operation.

I would say doing “/ 2” would suffice there. The return value of “-(low +1)” gives the caller the insert position where to insert the new key, if they wanted to do that task.

Ask your interviewer, -1 might suffice to say something isn’t there. The “-(low + 1)” bit could be a great follow-on question from the interviewer after you get the coded algorithm properly.

Don’t assume after you have a successful solution you are done with that problem.

Introductory Java Interview Questions

java interview questions

As your skill level increases, the questions you receive get more complicated. That doesn’t excuse you from knowing the introductory questions, too, if you are more experienced.

The obvious set of questions you’ll be asked test your knowledge of the Java API. The range of questions you will be asked here is enormous. Fresh out of school, you might be asked about the different keywords in Java. Been around the block a few times, then write a regular expression that describes the format of an email address.

I was once asked the latter question and I didn’t even mention regular expressions on my resume.

Here are a couple sample questions and answers of what to expect at this level.

Sample Introductory Java Question 1

Describe what all the different parts of the main() method declaration mean.

How to Answer

The first thing you should do here is write the main() method declaration on the whiteboard:

public static void main(String[] args)

Next, describe what each of public, static, void, main, String[], and args does.

It is probably sufficient to just verbalize what each means. But if you want to write each word down as you verbalize, it helps to get the point across.

a) public means that the method is visible everywhere

b) static means you don’t need an instance of the class to call the method

c) void means the method doesn’t return anything

d) main is just the predefined method name for how to start a Java program

e) String[] means the method accepts an array of strings as arguments to the method

f) args is the name of the parameter to the method. Any name will do here. There is no hard and fast requirement that the parameter be called args.

Sample Introductory Java Question 2

Given a line of text, verify that the beginning and ending parenthesis match up.

How to Answer

This is a little more complicated than the first, but still a coding problem a junior person should be able to code.

The easiest way to do this is to push each beginning paran onto a stack, then when you get to a closing paran, pop an item off the stack. If the parens match (‘(‘ and ‘)’ or ‘{‘ and ‘}’) continue.

If not, there is a mismatch. If the pop is ever empty or the stack isn’t empty at the end, there is a mismatch. Here is one solution that relies on the java.util.Stack class.

import java.util.Stack;
public class Match {

  public static boolean doParensMatch(String str) {
    Stack stack = new Stack<>();
    char c;
    for (int i=0; i < str.length(); i++) {
      c = str.charAt(i);

      if (c == '(' || c == '{') {
      } else if (c == '}') {
        if (stack.empty()) {
          return false;
        } else if (stack.peek() == '{') {
      } else if (c == ')') {
        if (stack.empty()) {
          return false;
        } else if (stack.peek() == '(') {
        } else {
          return false;
    return stack.empty();

  public static void main(String[] args) {
    System.out.println(doParensMatch("{)")); // failure
    System.out.println(doParensMatch("{((((((((()")); // failure

Three things worth pointing out here:

1. Recursion isn’t the answer to everything.

2. The main() method declaration here demonstrates lots of test cases. Don’t just code a solution. Walk through the solution with at least one if not more test cases.

3. The Stack<Character> stack = new Stack<>(); line requires special mention.

First, the <Character> bit demonstrates at least some knowledge of generics. Secondly, the <> declaration shows you’ve kept up with the language updates and know you don’t have to do <Character> again. Little things like that are things that interviewers are looking for.

You won’t automatically fail with a line like Stack stack = new Stack() and casting when you get a character off the stack, but the more up to date you are with your code, the better you’ll look.

Advanced Java Interview Questions

java interview questions

As you get more experienced, you can expect more advanced types of coding problems. Not only will you be expected to code the solution, but you’ll be expected to explain your answer more eloquently.

Even more important with advanced Java programming questions is talking your way through the possible solution before coding.

Returning to that email regex expression mentioned earlier, here’s a simple solution that isn’t fully RFC822 compliant but would probably suffice for the purposes of an interview.

Pattern regexPattern =
    Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE);
Matcher matcher = regexPattern.matcher(emailStr);
boolean found = matcher.find();

Can you come up with something comparable? If given that solution, could you explain what the different elements of the pattern do? What do the ^, $, +, and {2,6} indicate? (start of line/string, end of line/string, at least once, and at least 2 but not more than 6 times, respectively).

Given the recent addition of new top level domains, that 6 is now too short. Good thing to point out if given that code as a possible solution.

The CASE_INSENSITIVE isn’t an absolute requirement, but does simplify the solution a little. Also, if asked to write the pattern, the compile(), matcher(), and find() calls aren’t required either, but help to demonstrate your knowledge of the API to actually use the regular expression.

On the more advanced side, you should expect pattern-related questions. These would range from describe the Singleton design pattern and how you would use it, to where in Java is the Singleton design pattern used, to code the Singleton design pattern, and finally what issues serialization can bring up with the Singleton design pattern. Observer, Factory, Decorator patterns are some others to be aware of that are found in Java.

The best way to prepare for the more advanced interview questions for a particular company is to actually use Google to lookup what other people are reporting they were asked. This works great for larger companies.

For instance, with a company like Google itself, you might run across a resource they provide:

When asked more advanced questions, the solution doesn’t stop when the question is answered. You need to expect follow-up questions, like why did you do the operation in a particular way, what is the performance of your solution, and how can you improve it.

For some reason, many of the more advanced questions are game related. Don’t be surprised if you have to code the implementation of some game, or a part thereof.

Some questions I’ve been asked include finding words on a Boggle board, reporting which if any player won from a Tic-Tac-Toe board, and dealing several card game related questions.

More Java Interview Tips/Advice

java interview questions answers

As with any job interview, the technical questions are only part of the fun. You will also be asked fit and resume questions to determine if you're a good match for the company and dig deeper into your job experience.

Do your best to learn something about the company and position beforehand. They shouldn’t be the only people asking questions in the interview. It may seem obvious, but make sure you’ll be a good fit with them, too. Are you more of a blue jeans and t-shirt person?

While that might be more casual than most environments, you won’t do well with a company wearing suits and ties every day. Do you prefer to sit in your cube coding with your headphones on? You won’t do well with a company that requires lots of interacting with customers. If you don’t ask, you’ll never know.

Be prepared to ask at least some questions of the interviewer. Try not to rely on the obvious questions like how long they’ve been at the company and don’t go with a presumptuous question like, "When can I start?"

It's okay to ask the same question of all interviewers if you think you’ll get a different answer from each. A better question here might be “What is a typical workday like?” A bad question here is “How does your company/team do testing?”

The former is good to see how each person does their job. There should be some similarities in the answers, but don’t expect them to be 100% the same. The latter is still a good question, but not one to ask to each interviewer. At most two, to see if they report the same thing.

A final thing to mention that might seem obvious, but is rarely if ever brought up during the interview: If an interviewer asks you to do the same coding problem that you addressed with an earlier interviewer, tell them.

While you might feel great that you already know the answer and can do it right (or better) the second time around, the interviewers do talk to each other and will mark you down if you don’t let them know about the duplicity of questions. They should know better but it sometimes happens. You don’t want to be rejected because you didn’t open your mouth about it.

Another useful video for acing the technical interview in general:


Main Photo Credit: Jose Espartosa

Pamela Skillings