The Weirdest Java Interview Questions That You Ever Heard Of

This is a post that gathers the weirdest Java interview questions that I personally faced during job interviews or heard of during my career as a Software Engineer.

Question 1. Can you write a Hello World program without using a single semicolon ?

When I first heard this question I found it really funny, I was asking myself what would be the benefit of knowing such a trick ? Would that make me a better programmer ?

Well, No this has nothing to do with being a better programmer, but it highlights someone’s ability to think out of the box.

Answer 1. Here is one way to do it :

Weird Hello World

The printf method not only prints a String to the standard output stream System.out but it also returns a PrintStream Object representing this standard output stream, we can make a null condition check inside an if statement to achieve our goal.

Question 2. What’s the output of the following code ?

Hmm, so what’s the output ?

Intuitively, most people would say that the output is :

false
false

Arguing that a, b, c, d are not primitive types so they should not reference the same memory slots.

Actually, the output is:

true
false

Answer 2. Explanation :

This is because The Integer type keeps a cache of all objects with a value in the range of -128 to 127 for performance reasons. So when you declare new variables in that range, you’re actually referring to the same object.

Question 3. Is the following code valid ?

Focus !

Answer 3. Considering the following Exception Hierarchy :

Java Exception Hierarchy

Exceptions are categorized into two sections: checked exceptions like FileNotFoundException and unchecked exceptions like ArithmeticException .

Checked exceptions are checked by the Java compiler, Java forces you to handle these errors directly in your code in a try/catch block, ignoring them is a very bad practice in Java.

Unchecked Exceptions are subclasses of RuntimeException they are not checked by the compiler and they might occur in your program, once any buggy code is executed.

The overriding method can not throw a higher Exception than the original. So if the original method throws FileNotFoundException , then the overriding method can not throw a superclass of FileNotFoundException e.g. Exception but it can throw any subclass of FileNotFoundException or simply does not throw any Exception. This rule only applies to checked Exception in Java, overridden method is free to throw any unchecked Exception.

So, Snippet 1 is not valid and Snippet 2 is valid !

Question 4. Refactor the following code (from L24 to L38) in order to export the calculation logic in a Java Enum having some Lambda expressions.

Gotta love these Regex ❤

This is an exercise that I miserably failed, especially because I couldn’t imagine how I would put a lambda expression inside an Enum, that’s something that I never saw in a production running code.

Later, I was able to better understand Lambda expressions thanks to the following definition from Oracle :

Lambda expressions let you express instances of single-method classes more compactly.

Most of the time we use Lambda expressions with the Stream API to transform a stream of data on the fly, this is so tricky because it hides the fact that a Lambda expression is just a compact representation of an instance of a functional interface.

So How would we refactor the code ?

Answer 4. This can be achieved by implementing the@FunctionalInterface IntBinaryOperator with a Java enum.

Let’s see how :

Is this even Java ???

The use of lambdas in this case reduces boilerplate considerably, though these are some very simple calculation methods, it would not be correct to use lambdas when the methods are complex and require a lot of code. It would just become cumbersome, source code available here.

Most of the code that you find here is for interview purposes only, I personally don’t think that you would ever have to write these snippets of code in real-world Java applications.

I’ll try to update this post as often as possible, if you know some more weird examples please feel free to relate them on the comment section, and if you like these kinds of articles hit the 👏 button below.

Lead Software Engineer working across multiple technologies and frameworks. Always willing to challenge my problem solving skills.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store