📜 ⬆️ ⬇️

Parsing JPoint 2019 tasks

image

Hello!

One of the most hardcore Java conferences, JPoint 2019, ended for the seventh time and, as always, broke the attendance record, this time the event attracted more than 1,700 Java developers.
')
Odnoklassniki participated in all JPoint conferences. Since 2013, we have been actively supporting JPoint and at our stands we organize various activities for testing Java knowledge for participants. This year we had the famous “unsolvable” tasks from the leading developers of OK.ru. The conference participants who correctly answered the questions received prizes.

For the sake of fairness, it must be said that out of 600 leaves with tasks that we distributed, less than 100 were received back, the average score is about 0.25.

The best solution turned out to be 4 points out of 5 possible.

We publish tasks and their solutions so that you can test your strength.

Bit to be


This task was solved by 40% who passed the answers.

Michael creates a thread-safe analogue of BitSet . Add the implementation of the setBit() method.

For simplicity, BitSet size can be considered constant.

 public class ConcurrentBitSet { private final AtomicLongArray bits; public ConcurrentBitSet(int size) { assert size >= 0; int words = (size + 63) / 64; bits = new AtomicLongArray(words); } public void setBit(int index) { // TODO: Implement me! } } 

Decision
An implementation using updateAndGet() / getAndUpdate() , available with Java 8, might look like this:

 public void setBit(int index) { int word = index >> 6; long mask = 1L << index; bits.updateAndGet(word, value -> value | mask); } 

Similarly, the implementation on the good old compareAndSet() looks like:

 public void setBit(int index) { int word = index >> 6; long mask = 1L << index; long oldValue; long newValue; do { oldValue = bits.get(word); newValue = oldValue | mask; } while (!bits.compareAndSet(word, oldValue, newValue)); } 


Enum is not the same


This problem was solved by 45% who passed the answers.

Tatyana wants to check whether two objects are constants of the same enum . What did she not consider?

 boolean sameEnum(Object o1, Object o2) { return o1.getClass().isEnum() && o1.getClass() == o2.getClass(); } 

Decision
The tip is in the documentation for the Enum.getDeclaringClass () method, which is used, for example, in Enum.compareTo():

 public final Class<E> getDeclaringClass() { Class<?> clazz = getClass(); Class<?> zuper = clazz.getSuperclass(); return (zuper == Enum.class) ? (Class<E>)clazz : (Class<E>)zuper; } 

For enum constants with non-empty bodies, intermediate classes are created, so the correct answer might look like this:

 boolean sameEnum(Object o1, Object o2) { return o1 instanceof Enum && o2 instanceof Enum && ((Enum) o1).getDeclaringClass() == ((Enum) o2).getDeclaringClass(); } 


Uncompiled links


This task was solved by 42% who passed the answers.

The following interface is available:

 interface Link<T> { T next(); } 

Change the signature (but not the body) of the getTail() ) method so that the code compiles without errors and warnings.

 Link getTail(Link head) { if (head.next() == null) { return head; } return getTail(head.next()); } 

Decision
There are only three correct minimum answers:

 <T extends Link<T>> Link<T> getTail(Link<T> head) <T extends Link<T>> Link<T> getTail(T head) <T extends Link<T>> T getTail(T head) 

Paradoxically, this signature is too tough for the Java compiler:

 <T extends Link<T>> T getTail(Link<T> head) 


Messenger


This problem was solved by 14% who passed the answers.

Kostya is developing a messaging application. Specify errors in the method for sending the message over the network.

 void send(SocketChannel ch, String message) throws IOException { byte[] bytes = message.getBytes(); ByteBuffer header = ByteBuffer.allocate(4); header.putInt(bytes.length); ch.write(header); ch.write(ByteBuffer.wrap(bytes)); } 

Decision
There are at least three errors in this code:


This is what the revised version might look like:

 void send(SocketChannel ch, String message) throws IOException { byte[] bytes = message.getBytes(StandardCharsets.UTF_8); ByteBuffer header = ByteBuffer.allocate(4); header.putInt(bytes.length); header.flip(); while (header.hasRemaining()) { ch.write(header); } ByteBuffer body = ByteBuffer.wrap(bytes); while (body.hasRemaining()) { ch.write(body); } } 


Java in container


This problem was solved by 7.5%, who passed the answers.

What parameters of the JVM should Alexei prescribe in order not to allow Linux OS to kill the Java process due to exceeding the memory limit allocated for the container?


Decision
The memory consumed by the Java process is far from being limited to just the hip, Metaspace and Code Cache. Many other JVM structures also take up memory, and not all of them are governed by settings. In addition to the virtual Java machine, native memory is allocated by the Java Class Library and user code via Direct ByteBuffers and Mapped ByteBuffers.

The UseContainerSupport parameter together with MaxRAMPercentage affects only the size of the heap. Thus, there is no guaranteed way to avoid exceeding the limit using only the JVM flags, and the correct answer is the last one. You can find out more about Java memory usage by the process in Andrei Pangin's report on Joker 2018 “Java memory by process” .

Source: https://habr.com/ru/post/447218/


All Articles