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

✍️По всем вопросам: @Pascal4eg
Download Telegram
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
public class Test {
public static void main(String[] args) {
String[] strings = {"Java", "Code"};
Object[] objects = strings; // 1

System.out.print(objects[0] + " ");

try {
objects[1] = 100; // 2
System.out.print("Success");
} catch (Exception e) {
System.out.print(e.getClass().getSimpleName());
}
}
}
❤3
interface I {
static void print() {
System.out.print("I-static ");
}
}

class C implements I {
public static void print() {
System.out.print("C-static ");
}
}

public class Test {
public static void main(String[] args) {
C c = new C();
I i = c;

// Строка 1
c.print();

// Строка 2
I.print();

// Строка 3
i.print();
}
}
❤1
class Cat {
public static void mew() {
System.out.print("Meow");
}
}

class SimonsCat extends Cat {
public static void mew() { }
}

public class Mew {
public static void main(String[] args) {
Cat cat = new Cat();
Cat simonsCat = new SimonsCat();
cat.mew();
simonsCat.mew();
}
}
👍3❤2
👍3🎉1
public class Test {
public static void main(String[] args) {
int x = 0;
int y = 0;

boolean b1 = (x++ > 0) && (y++ > 0);
System.out.print("x=" + x + " y=" + y + ", ");

x = 0;
y = 0;

boolean b2 = (x++ > 0) & (y++ > 0);
System.out.print("x=" + x + " y=" + y);
}
}
❤2🔥1
❤2👍1🔥1🎉1