Java | Tests
1.82K subscribers
1 photo
1 link
Проверь свои знания в Java

✍️По всем вопросам: @Pascal4eg
Download Telegram
public class Test {
public static void main(String[] args) {
StringBuilder s1 = new StringBuilder("Java");
StringBuilder s2 = new StringBuilder("Code");

trick(s1, s2);

System.out.println(s1 + " " + s2);
}

public static void trick(StringBuilder a, StringBuilder b) {
a.append(b);
b = a;
b.append("!");
}
}
❤2👍1
👍3❤2🎉1
public class Test {
public static void main(String[] args) {
System.out.println(calc());
}

static int calc() {
int i = 0;
try {
i = 10;
return i;
} catch (Exception e) {
i = 20;
return i;
} finally {
i = 30;
System.out.print("Finally ");
}
}
}
❤1
👍4✍1🤯1🎉1
enum Planet {
EARTH, MARS;

static {
System.out.print("Static ");
}

{
System.out.print("Init ");
}

Planet() {
System.out.print("Ctor ");
}
}

public class Test {
public static void main(String[] args) {
System.out.print(Planet.EARTH);
}
}
❤2
public class Test {
public static void main(String[] args) {
final String pig = "length: 10";
final String dog = "length: " + pig.length();
System.out.println(pig == dog);
}
}
Что выведет код? 👆
Anonymous Quiz
28%
true
58%
false
14%
<Ошибка компиляции>
import java.util.stream.Stream;

public class Test {
public static void main(String[] args) {
Stream.of("A", "B", "C")
.map(s -> {
System.out.print("Map-" + s + " ");
return s;
})
.anyMatch(s -> {
System.out.print("Match-" + s + " ");
return s.equals("B");
});
}
}
❤3
import java.util.ArrayList;
import java.util.List;

public class Test {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(10);
list.add(20);
list.add(30);

list.remove(1);
list.remove(Integer.valueOf(30));

System.out.println(list);
}
}
❤1
Что выведет код? 👆
Anonymous Quiz
11%
[10, 20]
8%
[20, 30]
22%
[10, 30]
48%
[10]
11%
[20]
❤6🎉2
public class Counter {
public static void main(String[] args) {
int counter = 0;
counter = counter++;
System.out.println(counter);
}
}
❤2
Что выведет код? 👆
Anonymous Quiz
41%
0
55%
1
5%
<Ошибка компиляции>
🤔5🥱2❤1🎉1
public class Test {
public static void main(String[] args) {
Object o1 = (true) ? new Integer(10) : new Double(20.0);

System.out.println(o1);
}
}
❤3🫡1
public class Test {
public static void main(String[] args) {
// Сравнение нулей
double d1 = 0.0;
double d2 = -0.0;
System.out.print((d1 == d2) + " ");
System.out.print(Double.valueOf(d1).equals(Double.valueOf(d2)) + " ");

// Сравнение NaN (Not a Number)
double n1 = Double.NaN;
double n2 = Double.NaN;
System.out.print((n1 == n2) + " ");
System.out.print(Double.valueOf(n1).equals(Double.valueOf(n2)));
}
}
🔥3❤1
import java.io.*;

class Data implements Serializable {
int x = 10;
static int y = 20;
transient int z = 30;
}

public class Test {
public static void main(String[] args) throws Exception {
Data d = new Data();

// 1. Сериализация (сохраняем объект в байты)
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(d);

// 2. Изменяем статическое поле ПОСЛЕ сериализации
Data.y = 50;

// 3. Десериализация (восстанавливаем объект из байтов)
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
Data dRestored = (Data) ois.readObject();

System.out.println(dRestored.x + " " + dRestored.y + " " + dRestored.z);
}
}
👍2❤1
Что выведет код? 👆
Anonymous Quiz
9%
0 50 0
22%
10 20 0
25%
10 20 30
25%
10 50 0
19%
10 50 30
👍3🎉1