5 Weird Java Questions That Will Make Your Head Spin
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?
import static java.lang.Character.getNumericValue; public class BigCharacters { public static void main(String... a) { for (char c = 1; c > 0; c++) if (getNumericValue(c) > 50) System.out.println(c + ": " + getNumericValue(c)); } }
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?
Integer a = 42; Integer b = 42; System.out.println(a == b); Integer c = 666; Integer d = 666; System.out.println(c == d);
3. Char arithmetic
Moving on. Can you explain what is happening here?
char ch = '0'; ch *= 1.1; System.out.println(ch);
Possible options:
- Compiler error
- Runtime error
- Prints 0
- Prints 4
4. Don’t give up!
This one is just out of this world. What’s going on here?
String _ = "Hello "; String _ = "World"; String _ = " !!"; System.out.println(_+_+_);
Spoiler alert: It prints out “Hello World !!”
¯\_(ツ)_/¯
5. This question is self aware
What is the smallest value which prints WTF? The type of x is up to you.
if (x != (x += 0.0f)) System.out.println("WTF");
Solutions
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:
Integer a = 42; Integer b = 42; System.out.println(a == b); Integer c = 666; Integer d = 666; System.out.println(c == d);
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:
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
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?
if (x != (x += 0.0f)) System.out.println("WTF");
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. For these kind of situations, we’ve built Takipi for Java. Takipi is a Java agent that collects all the data you need to solve errors in 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.
Reference: | 5 Weird Java Questions That Will Make Your Head Spin from our JCG partner Alex Zhitnitsky at the Takipi blog. |
I am seriously considering of dropping java and becoming a farmer !
same here ! : D
Hi,
I am just curious about number 4, in the code snippet in Peter Lawery’s blog the snipet shows as below:
String _? = “Hello “;
String _? = “World”;
String _?? = ” !!”;
System.out.println(_?+_?+_??);
While here in number 4 it is:
String _ = “Hello “;
String _ = “World”;
String _ = ” !!”;
System.out.println(_+_+_);
Can you please explain how it would still print “hello world!!”? I tried it on my own by copying the code I found is the _ variable is declared already ;)
Hi AD,
Looks like your editor doesn’t support the encoding, try play with its settings.
I have used notepad and compiled from console.. Did not use any IDE. still duplicate variable.
Eclipse requested changing to utf – i did and it worked – sweet!
As Alex said, try changing the encoding to UTF-8 and then see. It should do the trick.
The \u200e and \u200f characters are invisible ,therefore it displays question mark for a character it cannot display.
#Ques 4
It’s showing compile time error in myeclipse id .
Even if you add many semicolons at the end of a java statement, compiler never complains..WHY ?