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

✍️По всем вопросам: @Pascal4eg
Download Telegram
public class Test {
public static void main(String[] args) {
System.out.println(toBeOrNotToBe());
}

static boolean toBeOrNotToBe() {
try {
return true;
} finally {
return false;
}
}
}
❤1👍1
Что выведет код? 👆
Anonymous Quiz
22%
true
67%
false
12%
<Ошибка компиляции>
❤2🎉2
public class Boom {
private Boom internalInstance = new Boom();

public Boom() throws Exception {
throw new Exception("Boom!");
}

public static void main(String[] args) {
try {
Boom b = new Boom();
System.out.println("Surprise");
} catch (Exception e) {
System.out.println("I caught!");
}
}
}
❤1
🔥2🎉1
class Point {
private final int x, y;
private final String name;

Point(int x, int y) {
this.x = x;
this.y = y;
name = makeName();
}

protected String makeName() {
return "[" + x + "," + y + "]";
}

public final String toString() {
return name;
}
}

public class ColorPoint extends Point {
private final String color;

ColorPoint(int x, int y, String color) {
super(x, y);
this.color = color;
}

protected String makeName() {
return super.makeName() + ":" + color;
}

public static void main(String[] args) {
System.out.println(new ColorPoint(3, 5, "red"));
}
}
❤2🔥1
🎉2
class Cache {
private static int sum = 0;

static {
initializeIfNecessary();
}

public static int getSum() {
initializeIfNecessary();
return sum;
}

private static boolean initialized = false;

private static synchronized void initializeIfNecessary() {
if (!initialized) {
for (int i = 0; i < 5; i++)
sum += i;
initialized = true;
}
}
}

public class Client {
public static void main(String[] args) {
System.out.println(Cache.getSum());
}
}
❤1
Что выведет код? 👆
Anonymous Quiz
14%
0
41%
10
21%
20
23%
<Ошибка компиляции>
🎉1
import java.util.ArrayList;
import java.util.List;

class Person {
Long id;
String name;

public Person(Long id, String name) {
this.id = id;
this.name = name;
}

@Override
public boolean equals(Object obj) {
System.out.print(1);
if (obj == this) {
return true;
}
if (obj instanceof Person person) {
return id.equals(person.id);
}
return false;
}
}

public class Test {
public static void main(String[] args) {
List<Person> list = new ArrayList<>();
list.add(new Person(1L, "Leo"));
list.add(new Person(2L, "Raph"));
list.add(new Person(3L, "Donnie"));
list.add(new Person(4L, "Mikey"));

list.contains(new Person(3L, "Shredder"));
}
}
❤1🔥1
Что выведет код? 👆
Anonymous Quiz
19%
1
6%
11
37%
111
24%
1111
14%
<Ошибка компиляции>
🎉1
public class Test {
public static void main(String[] args) {
byte b = 50;
print(b);
}
private static void print(Short num) {
System.out.println(num++);
}
}
❤1
Что выведет код? 👆
Anonymous Quiz
47%
50
12%
51
7%
0
34%
<Ошибка компиляции>
🎉2
public class Test {
public final int value = 4;
public void doIt() {
int value = 6;
Runnable r = new Runnable() {
public final int value = 8;
public void run() {
int value = 10;
System.out.println(this.value);
}
};
r.run();
}
public static void main(String[] args) {
new Test().doIt();
}
}
❤2
Что выведет код? 👆
Anonymous Quiz
23%
4
8%
6
43%
8
26%
10
❤2👍1🎉1
public class Test {
public static void main(String[] args) {
System.out.println(isEqual(1, 1));
}

private static boolean isEqual(int i, int j) {
return (i == j) ? null : false;
}
}
❤1🔥1
Что выведт код? 👆
Anonymous Quiz
11%
true
14%
false
40%
null
35%
<Ошибка NullPointerException>
🎉1
public class Test {
public static void main(String[] args) {
System.out.println(2 ^ 3);
}
}
❤1🔥1
Что выведет код? 👆
Anonymous Quiz
10%
0
30%
1
12%
5
17%
6
32%
<Ошибка компиляции>
🎉1
public class Test {
public static void main(String[] args) {
int i = 0;
System.out.println(--i++);
}
}
❤2🔥1
Что выведет код? 👆
Anonymous Quiz
47%
-1
23%
0
3%
1
27%
<Ошибка компиляции>
🌚4🎉1