Java Interview Questions
04 Oct 2018
A little while ago, I was asked what my favorite Java interview questions were. As an added wrinkle, the questions needed to be such that they could be evaluated by a non-technical person. Because they are meant to be used for a cursory impression of a candidate’s Java skills, the answers are not rigorous.
Level: Entry/Mid
What is the difference between overloading and overriding?
Overloading: 2 or more methods in one class have the same name, but different arguments. (see https://en.wikipedia.org/wiki/Function_overloading)
Overriding: A method in a parent and child class (also called the superclass and subclass) have the same name and arguments. The child class method replaces the parent class method. (see https://en.wikipedia.org/wiki/Method_overriding#Java)
Interpreting: For overloading, you should hear something about same name, different arguments. For overriding, should hear super and subclass have at least one method with the same name and arguments
This is a pretty basic question, and should probably be considered go/no go.
Level: Entry/Mid
Talk about interface vs abstract, and where/why you’d use each.
Interface: Contain no code, only method signatures. Any class that implements (inherits) must have a reference to the method that includes code. Interfaces can’t be used to create objects directly. (see https://en.wikipedia.org/wiki/Interface_(Java))
Abstract: Contains code and abstract methods, which are placeholders for methods that are in subclasses. (see https://en.wikipedia.org/wiki/Abstract_type)
Interpreting: Looking for them to talk about specifying behavior, or talking about how they can inherit some behavior. Note that Java 8 and later do allow default and static methods, so it is correct if they qualify their answer.
Level: Mid/Senior
Class hierarchies are common in object oriented programming. What are other ways of grouping classes and objects?
Inheritance is one way to organize classes. Another is composition. (see https://en.wikipedia.org/wiki/Composition_over_inheritance)
Interpreting: I mostly want to hear that they say ‘compose’ or ‘composition’.
Level: Mid/Senior
Explain how Java supports multiple inheritance.
For Java 7, this is definitely a trick question. No multiple class inheritance is allowed. This was specifically designed into the language (as opposed to C++). (see https://en.wikipedia.org/wiki/Multiple_inheritance)
For Java 8 and later, it is possible to have a kind of multiple inheritance, but some of the problems associated with multiple inheritance are detected at compile time.
Interpreting: Since this is a trick question, they can just say ‘no, it isn’t allowed’.
They can also talk details about ‘diamond of death’, ‘deadly diamond’, or something like
that. The answer tells how much they understand about the language design.
Level: Mid/Senior
What do you know about how garbage collection works?
Java and C# are similar in how they garbage collect, but the terminology is different. (see https://en.wikipedia.org/wiki/Garbage_collection_(computer_science), http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/
Interpreting: I would look for more than just the basics here. The basic idea is that memory that is no longer used is automatically disposed of, and is made available again. The more detailed explanation that I would like to see includes a discussion of generations, and that young objects tend to get collected (or recovered) more frequently than ‘older’ objects. This is generational garbage collection.
Level: Mid/Senior
Question: What do you know about Lambdas?
Answer: A lambda is a function that doesn’t belong to a class. They might also note that they are declared inline (https://en.wikipedia.org/wiki/Anonymous_function#Java)
Interpreting: Often, you’ll hear the words ‘arrow operator’ or ‘closure’. If they know what it is in Java, they know at least Java 8. If they don’t, they know up to Java 7.