📜 ⬆️ ⬇️

Simple tasks in Java. Weakly solve everything?



Hi ! We’ve put together a get-together for one of the coolest Russian-speaking Java practitioners and asked them to give a task for you to break your teeth, your brain and beat against the wall, trying to figure out how it works. Actually, we argued on a bottle of Hennessy that in 12 hours after publication no one would send all the correct answers. I'm sure someone can. Therefore, if you do it first - from my bottle.

The first task is simple, it is from the viewer Nikolay Garbuzov, a specialist in rolling pins, who loves recursion, pattern matching and compilation magic:
Does the following aspect compile AJC compiler?
If so, what will it output to the console during compilation?
')
public aspect QuizAspect { public static int count(int i) { return i++; } before (int n) : execution(public int QuizAspect.count(int)) && args(n) && if(QuizAspect.count(1)>1) { System.out.println("QuizAspect " + n); } } 


While simple, right?

The second task is from Vladimir Sitnikov (NetCracker), a dirty pervert with regards to regular expressions. This bad man even wrote an announcement of his report to the JPoint with their help.

What is the catch to delete Java comments with such an expression? List 3 reasons why this should not be done. (we believe that the source code is written in normal characters) -
 Pattern.compile("/\\*(?:[^*]|\\*[^/])*\\*/") 


And the second task right away - is it possible to write “hello world” on java without a single space?


The third task was sent by the viewer Nikolai Alimenkov from the club of anonymous developers. He has been scaling his scalable systems for 10 years, and has come to rest with us, so the task is one of the simplest:

There are 2 Spring contexts:

1. a.xml with bin

 <util:list id="myList"> <value>3</value> <value>4</value> </util:list> 


2. b.xml with bin

 <util:list id="myList"> <value>6</value> </util:list> 


What a code snippet prints:

 System.out.println(new ClassPathXmlApplicationContext("a.xml", "b.xml").getBean("myList")); 


And how can you get him to throw an error without changing the logic of the code?


The fourth task is provided by Nikita Salnikov-Tarnovsky, specifically focusing on hardcore and writing a tool for finding memory leaks - Plumbr. Not one wrote, of course, but only he can read the received code. Fucking optimizer.

Below are 2 programs. Each of them tries to allocate a total of memory larger than the size of the heap. But one of them throws out java.lang.OutOfMemoryError, and the second does not. Why?

 public class OOM1 { private static final int SIZE = (int) (Runtime.getRuntime().maxMemory() * 0.55); public static void main(String[] args) { { byte[] bytes = new byte[SIZE]; System.out.println(bytes.length); } byte[] bytes1 = new byte[SIZE]; System.out.println(bytes1.length); System.out.println("I allocated memory successfully"); } } public class OOM2 { private static final int SIZE = (int) (Runtime.getRuntime().maxMemory() * 0.35); public static void main(String[] args) { { byte[] bytes = new byte[SIZE]; System.out.println(bytes.length); } byte[] bytes1 = new byte[SIZE]; System.out.println(bytes1.length); byte[] bytes2 = new byte[SIZE]; System.out.println(bytes2.length); System.out.println("I allocated memory successfully"); } } 



The penultimate task from Baruch Sadogursky from JFrog and Bintray / Artifactory

What will this code output?

 def back = 'back' def quotes = ["I'll be $back", "I'll be ${-> back}", "I'll be ${back}", "I'll be "+back] println quotes back = 'bach' println quotes 


The latter is from Yevgeny Borisov, trainer of the officers of the Israeli army in Java. Spring:

There are two bins:
 @Component public class  { public void closeAll() { while (()) { (); } } } @Component public class  { public void closeAll() { while (()) { (); } } } 

How to make, that at context closing, both methods closeAll worked in parallel (and what options are)?


Achivki and solutions




Answers, please, in the comments under the spoiler. If you cannot comment, do #jpoint on Facebook or VKontakte.

Here are the answers to the first four questions, and after the JPoint we will publish and the answers to the questions jbaruch and EvgenyBorisov .

PS: you can search for tips in the announcements of relevant reports on the JPoint website.

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


All Articles