Что выведет код? 👆
Anonymous Quiz
8%
JavaCode Code
17%
JavaCode! Code
45%
JavaCode! JavaCode!
29%
Java Code!
👍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
Что выведет код? 👆
Anonymous Quiz
40%
Finally 30
28%
Finally 10
6%
Finally 0
10%
10
16%
<Ошибка компиляции (Unreachable code)>
👍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);
}
}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
❤6🎉2
public class Counter {
public static void main(String[] args) {
int counter = 0;
counter = counter++;
System.out.println(counter);
}
}❤2
🤔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
Что выведет код? 👆
Anonymous Quiz
37%
10
15%
10.0
10%
20.0
24%
<Ошибка компиляции (несовместимые типы)>
15%
<ClassCastException>
😁4❤2🎉2
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
Что выведет код? 👆
Anonymous Quiz
16%
true true true true
27%
true false false true
19%
true true false false
30%
false true true false
8%
true false false false
🔥4🎉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
👍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