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;
}
}
}
Что выведет код? 👆
Anonymous Quiz
27%
true
41%
false
32%
<Ошибка компиляции>
❤3🎉1
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!");
}
}
}
❤2
🎉5❤1👍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
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());
}
}
❤2👍1👎1
Что выведет код? 👆
Anonymous Quiz
23%
0
35%
10
17%
20
25%
<Ошибка компиляции>
🎉4
public class ShadesOfGray {
public static void main(String[] args) {
System.out.println(X.Y.Z);
}
}

class X {
static class Y {
static String Z = "BLACK";
}
static C Y = new C();
}

class C {
String Z = "WHITE";
}
❤4
Что выведет код? 👆
Anonymous Quiz
27%
BLACK
25%
WHITE
48%
<Ошибка компиляции>
😢11🎉5
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);
}
}
❤1👍1🔥1
Что выведет код? 👆
Anonymous Quiz
29%
true
48%
false
23%
<Ошибка компиляции>
👍4❤3🎉1
public class Counter {
public static void main(String[] args) {
int counter = 0;
counter = counter++;
System.out.println(counter);
}
}
❤3⚡2
Что выведет код? 👆
Anonymous Quiz
31%
0
57%
1
11%
<Ошибка компиляции>
🎉4
public class Test {
public static void main(String[] args) {
System.out.println(calc());
}

static int calc() {
try {
if (1 == 1)
throw new RuntimeException();
return 1;
} catch (Exception e) {
return 2;
} finally {
return 3;
}
}
}
👍7❤1
Что выведет код? 👆
Anonymous Quiz
18%
1
15%
2
41%
3
26%
<Ошибка компиляции>
🎉5
import java.util.List;

public class Test {

public static void main (String[] args) {
print(List.of(1, 2, 3));
}

private static void print(List<String> list) {
System.out.println(1);
}

private static void print(List<Integer> list) {
System.out.println(2);
}

}
❤1
Что выведет код? 👆
Anonymous Quiz
12%
1
49%
2
39%
<Ошибка компиляции>
🎉3
public class Test {

public static void main(String[] args) {
System.out.print(or1() || or2() || or3());
}

private static boolean or1() {
System.out.print(1);
return false;
}

private static boolean or2() {
System.out.print(2);
return true;
}

private static boolean or3() {
System.out.print(3);
return false;
}

}
❤2👍1
Что выведет код? 👆
Anonymous Quiz
18%
1false
36%
12true
25%
123true
21%
123false
🎉2❤1👍1