5 Weird Java Questions That Will Make Your Head Spin

 ● 30th Sep 2015

5 min read

Some of the weirdest Java puzzlers that we had a chance to get our hands on

Even the most experienced Java developers will find the questions in this post confusing. Or at the very least, amusing (And absolutely unfair). After our adventure with the Java Deathmatch we’ve decided to publish a different set of questions this time around, highlighting some of the unusual and quirky things you can do with Java. For the results of our previous quizzes, you can check out this post and see if you can solve the question that 4 out 5 of developers got wrong.

Do try this at home (or office). But please, please don’t use it in real life Java applications! Unless you’re trying to troll someone and in that case, everything goes. A huge huge thanks goes out to Peter Lawrey for sharing most of these questions with us. The solutions are available at the bottom of this post, but try giving them an honest try and see how many of them you manage to explain.

And the questions are…

1. Closing brackets are overrated

Oddly enough, the following piece of code compiles perfectly. But what does it print?

[java] import static java.lang.Character.getNumericValue; public class BigCharacters { public static void main(String…a‮) { for (char c‮ = 1; c‮ & gt; 0; c‮++) if (getNumericValue(c‮) & gt; 50) System.out.println(c‮ + “: ” + getNumericValue(c‮)); } } [/java]

Note: getNumbericValue returns the numeric int value that the Unicode character represents. If the character doesn’t have a numeric value, -1 is returned.


2. The answer to life, the universe, and everything

Phew. We have our closing brackets back on this one. But what does the following code snippet print out? And why?

[java] Integer a = 42; Integer b = 42; System.out.println(a == b); Integer c = 666; Integer d = 666; System.out.println(c == d); [/java]

3. Char arithmetic

Moving on. Can you explain what is happening here?

[java] char ch = ‘0’; ch *= 1.1; System.out.println(ch); [/java]

Possible options:

a) Compiler error
b) Runtime error
c) Prints 0
d) Prints 4

4. Don’t give up!

This one is just out of this world. What’s going on here?

[java] String _‎ = “Hello “; String _‏ = “World”; String _‎‏ = ” !!”; System.out.println(_‎+_‏+_‎‏); [/java]

Spoiler alert: It prints out “Hello World !!”


Ghost Busters

5. This question is self aware

What is the smallest value which prints WTF? The type of x is up to you.

[java] if (x != (x += 0.0f)) System.out.println(“WTF”); [/java]


1. Closing brackets are overrated

First, some background. There is a character encoded with \u202e which is used for Right to Left languages like Hebrew or Arabic, and makes the rest of the line appear in reverse order. It’s a zero width character, and not only that, it’s also valid to use as a Java identifier. Try copying the code snippet, move around your cursor and then you’ll notice where it appears in the code.

Other than RTL language support, it’s also very useful if you want to pull a prank on someone. Replace a closing bracket with an opening bracket that has this character behind it to reverse the rest of the line – And see what happens next. If you decide to do that, please send us a photo of the victim’s face 🙂

2. The answer to life, the universe, and everything

Integers are objects of course, as opposed to int which is a primitive type. However, this code snippet:

[java] Integer a = 42; Integer b = 42; System.out.println(a == b); Integer c = 666; Integer d = 666; System.out.println(c == d); [/java]

Prints out “true” for the first comparison and “false” for the next. This wouldn’t work for ints, but since Integers are separate objects it makes sense that c and d are not the same one. But why a == b?

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.

3. Char arithmetic

This code snippet prints out 4:

[java] char ch = ‘0’; // ASCII for ‘0’ is 48 ch *= 1.1; // 48 x 1.1 is 52.8 which turns to 52 when cast to char System.out.println(ch); // 52 represents ‘4’ in ASCII [/java]

4. Don’t give up!

How come _, _ and _ are different variables? You might have guessed it right. The answer is with hidden characters that pass on as legit Java identifiers. You can read more about it on Peter Lawrey’s blog right here.

5. This question is self aware

So what would make this expression to be evaluated as true?

[java] if (x != (x += 0.0f)) System.out.println(“WTF”); [/java]

You can use any String, and also an int or long of (1 << 24) + 1 works for these types as their respective smallest value. But the smallest value of them all is Double.MIN_VALUE which is rounded to 0.0f when cast to a float.

Final Thoughts

We hope you enjoyed this collection of questions! However, if you find yourself spending too much time on puzzlers in your own codebase, it will probably be less than ideal. We created OverOps for exactly these situations. OverOps is a platform that collects all the data you need to solve errors in pre-prod and production – WITHOUT going through log files and trying to recreate the state that caused them. It lets you see the variable values that cause errors, all across the stack, and overlays them on your code.

Subscribe for Post Updates

We will send you updates about industry trends and more

Please visit our privacy policy for further details about our privacy practices.

Next Article