JPoint 2019 parsing

    image

    Hello!

    JPoint 2019, one of the most hardcore Java conferences, ended, it was held for the seventh time and, as always, broke the attendance record, this time the event attracted more than 1700 specialists in the field of Java development.

    Odnoklassniki took part in all JPoint conferences. Since 2013, we have been actively supporting JPoint and at our stands we have been organizing various Java knowledge testing activities for our participants. This year we had the famous "unsolvable" tasks from the leading OK.ru developers. Conference participants who answered the questions correctly received prizes.

    In fairness, I must say that out of 600 leaflets with the tasks that we handed out, less than 100 were returned, the average score is approximately 0.25.

    The best solution was, scoring 4 points out of 5 possible.

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

    Beats be


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

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

    For simplicity, the size can be considered BitSetconstant.

    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);
    }
    

    Looks similar to the implementation on the good old compareAndSet():

    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.

    Tatiana wants to check whether two objects are constants of the same enum. What did she not take into account?

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

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

    public final Class getDeclaringClass() {
        Class clazz = getClass();
        Class zuper = clazz.getSuperclass();
        return (zuper == Enum.class) ? (Class)clazz : (Class)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 problem was solved by 42% who passed the answers.

    The following interface is available:

    interface Link {
        T next();
    }
    

    Change the signature (but not the body) of the method getTail()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:

    > Link getTail(Link head)
    > Link getTail(T head)
    > T getTail(T head)
    

    Paradoxical as it may seem, such a signature is too tough for the Java compiler:

    > T getTail(Link head)
    


    Messenger


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

    Kostya is developing a messaging application. Indicate errors in the method for sending messages 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 may look like a fixed version:

    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 be prescribed to Alexei in order to prevent Linux from killing the Java process due to exceeding the memory limit allocated to the container?

    • -Xmx
    • -XX:MaxMetaspaceSize
    • -XX:ReservedCodeCacheSize
    • -XX:+UseContainerSupport
    • -XX:MaxRAMPercentage
    • JVM memory cannot be limited

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

    The parameter UseContainerSupporttogether with MaxRAMPercentageaffects only the size of the hip. Thus, there is no guaranteed way to avoid exceeding the limit using JVM flags only, and the last answer will be the correct one. For more information on the use of Java memory by a process, see the report by Andrei Pangin at Joker 2018 Shelf Process Java Storage” .

    Also popular now: