Hello! A few days ago we
laid out a post about puzzles that were given at the Joker 2018 conference. But this is not all! This year, especially for Joker, we made a whole game with equally interesting Java tasks (and not only), about which we’ll tell you today.
We have done similar games at conferences before, for example, at the JPoint past this spring. To make a game, we had to: 1) invent a game mechanic, 2) come up with questions, 3) implement all this.
Game mechanics should be very simple, intuitive, but at the same time not too banal, so as not to deprive of excitement. As a result of long discussions, they came up with such a game:
It is necessary, answering questions, to try to lead Duke (in the upper left corner) to one of the doors in the other corners. One session of the game takes 3 minutes. To open a cell, you need to correctly answer the question, which is chosen randomly each time in accordance with the category. For the correct answers points are awarded, for incorrect ones - a penalty, the cell with the question is blocked and it will have to be bypassed. As a result, the path can be quite long or even blocked. Players can choose different strategies: reach the exit as quickly as possible and get an extra bonus; answer as many questions as possible in the allotted time; go a long way on simple issues, or directly on more complex and get more points.
')
With the mechanics figured out, now we need to come up with questions. They should be of three categories of complexity, from the simplest to hardcore. The wording of the questions should be extremely short, there will be no time to read the sheets of the text. Answers should be such as not to give too many clues. Well, the questions themselves should be interesting and practical. At the same time there should have been a lot of them so that they would not repeat too quickly. As a result of the joint efforts, we managed to come up with 130 questions about data structures, algorithms, “java-passers”, hardcore questions about the internals of the JVM, and even a few questions about Docker.

There is a problem: the game is displayed on the big screen, and those who stand side by side will remember most of the answers for several games. At first it seemed that we would need hundreds of questions in order to keep the possibility of remembering to a minimum. But, on reflection, they realized that there are easier options. For a start, they removed the mouse control, leaving only the keyboard. Now those who are near see the question, but do not see what answer the player has chosen. Added mixing options for answers, making it difficult to remember. For each question, we added several similar, but slightly different wording. Of course, it is still possible to remember the answers, and this was evident from the noticeably improved results on the second day of the conference. Many users spent dozens of minutes playing to try to break the record of others. But everything is like in life - diligence is rewarded.
Two weeks have passed from the emergence of an idea to inventing questions and implementing them. Of course, everything is in Java. Used spring boot and gradle. WEB-interface is made on Angular. The built-in database H2, which is out of the box, comes with a web-based interface, which is very convenient. The stand configuration is two MacBooks, the picture of which is duplicated on two TVs. For convenience, the application settings were remotely deployed in our cloud (
https://habr.com/company/odnoklassniki/blog/346868/ ).
We learned a long time ago: no feature should be developed without collecting statistics. Of course, for the game we screwed up detailed statistics that we can share.
In total, the game in two days was played 811 times. Statistics of answers depending on the complexity of the question:
DIFFICULTY
| COUNT
| CORRECT_PERCENT
|
one
| 3552
| 61
|
2
| 2031
| 49
|
3
| 912
| 46
|
Which cells of the field and how often the players reached:
The percentage of correct answers on each cell of the field:
But the most interesting is, of course, the statistics of questions. It was not so easy to assess their complexity in view of their categorization, evaluation is always subjective, and a simple question for one user is difficult for another.
Oleg offered to throw out one of the questions with the words “even the cleaners know this,” but it turned out that not all cleaners know the correct answers to many “simple” questions, and the programmers too. We offer you some questions from our game - the leaders with the wrong answers, try to evaluate your strength!
- The result of calling this code?
System.out.println(1/0d)
- Throw ArithmeticException
- Will print "Infinity"
- Will print "NaN"
- Will print 0
AnswerThis seems like a very simple question. Here, simple arithmetic, what could be the catch, why did only 28% of the players give the correct answer? Dividing an integer by 0 leads in Java to an ArithmeticException
. But are there integers here? Let's look carefully. What is the “d” after 0? This letter means that before us is not an integer constant 0, but a value of type double
. And it turns out that the expression is identical to 1.0 / 0.0. And this is the division with a floating point by zero, the result of which is Double.POSITIVE_INFINITY
. So the correct answer is “b”.
- The result of calling this code?
System.out.println( Long.MAX_VALUE==(long)Float.MAX_VALUE );
- prints true
- prints false
- throw away ArithmeticException
AnswerFirst, you need to understand what is more: Float.MAX_VALUE
or Long.MAX_VALUE
? Although a float
has a smaller range of values ​​than double
, still its maximum value of about 20 orders of magnitude is beyond the range of possible long
values. But how in this case does casting work? You can guess, but it is better to run the code. Better yet, open the Java Language Specification, the section on Narrowing Primitive Conversion, and read that if the value of a floating-point number is too large and goes beyond the range of available values ​​of an integer type, then the result of the conversion is equal to the maximum value that can be represented using an integer type . Those. the result of the conversion is Long.MAX_VALUE
. The correct answer was given by 27% of respondents.
- What class is not
Comparable
?
- java.lang.String
- java.util.TreeSet
- java.io.File
- java.lang.Enum
AnswerThis seemingly simple question stumped many, more precisely - 76% of the respondents. Guess yourself which of the answers is correct here and which answer was the most popular - 61% of players chose it.
- What is the identical code?
Object o = Math.min(-1, Double.MIN_VALUE)
- Object o = -1
- Object o = Double.MIN_VALUE
- Object o = -1.0
AnswerThe minimum double
value is certainly less than -1, what could be wrong here again? Of course, everything is not so simple, otherwise we would not ask. It turns out that Double.MIN_VALUE
contains not exactly what is expected, namely “constant holding the smallest positive nonzero value”, according to the documentation. It would be Double.MIN_POSITIVE_VALUE
correct to call it Double.MIN_POSITIVE_VALUE
. Double
circled his finger again! The correct answer is: Object o = -1.0
, and so only 22% of the players responded.
- What line is the result of calling this code?
Long.toHexString(0x1_0000_0000L + 0xcafe_babe)
- 1cafebabe
- cafebabe
- ffffffffcafebabe
AnswerIf you selected the second answer, then you are among the 22% who answered correctly. This question is taken from the book “Java Puzzlers: Traps, Pitfalls, and Corner Cases” by Joshua Bloch and Neal Gafter. If answered incorrectly, do not be discouraged, and run to read this book!
- JDK 8 introduced support for annotations on method parameters. Is it possible to add annotation to the parameter of
this
method?
- It is impossible
- Possible, but only in bytecode
- Perhaps by defining
this
explicitly by the first parameter of the method.
AnswerWhen in JDK 8 they added the ability to annotate method parameters, they did not deprive
this
option. It is for this purpose that
this
can now be explicitly specified in method signatures:
class Foo { public void test(@Annotated Foo this) {} }
Although you can argue about its practical benefits, now it is a feature of the language. Before the correct answer, 32% of the players guessed.
- In JDK 8, the
concurrencyLevel
parameter in the ConcurrentHashMap
constructor affects:
- Available read / write concurrency
- Initial table size
- On both parameters
AnswerIf you choose option 2, then you are among the 15% who gave the correct answer to this most difficult question of the game. The thing is, JDK 8 abandoned the segments in ConcurrentHashMap
, so the concurrencyLevel
lost its previous meaning. It affects only the initial size of the table, and even then it only limits the value of initialCapacity
from below.
Due to numerous requests, we have posted the game on our website, where you can
play right now !