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

✍️По всем вопросам: @Pascal4eg
Download Telegram
Что выведет код? 👆
Anonymous Quiz
45%
Dog barks
16%
Animal speaks
28%
Animal barks
11%
Dog speaks
❤2🎉2
public class Test {
public static void main(String[] args) {
String a = "Java";
String b = "Ja" + "va";
String c = "Ja";
String d = c + "va";
String e = new String("Java");

System.out.println(a == b);
System.out.println(a == d);
System.out.println(a == e);
System.out.println(a.equals(e));
}
}
❤1
🎉2❤1👍1
public class Test {
public static void main(String[] args) {
System.out.print(testMethod() + " ");
}

static int testMethod() {
try {
throw new RuntimeException("Test exception");
} catch (RuntimeException e) {
System.out.println("Catch ");
return 1;
} finally {
System.out.println("Finally ");
return 2;
}
}
}
❤3
❤1🎉1
interface A {
default void show() {
System.out.println("A");
}
}

interface B {
default void show() {
System.out.println("B");
}
}

abstract class C {
public void show() {
System.out.println("C");
}
}

public class D extends C implements A, B {
public static void main(String[] args) {
new D().show();
}
}
❤1👍1🔥1
Что выведет код? 👆
Anonymous Quiz
3%
A
9%
B
37%
C
51%
<Ошибка компиляции>
❤2🎉2👍1
public class Test {
public static void main(String[] args) {
Number[] numbers = new Integer[3];
numbers[0] = 42;
numbers[1] = 3.14;
System.out.println(numbers[0]);
}
}
❤1👍1
🎉4
public class Test {
enum Level {
LOW, MEDIUM, HIGH
}

public static void main(String[] args) {
Level level = null;

switch (level) {
case LOW:
System.out.println("Low");
break;
case MEDIUM:
System.out.println("Medium");
break;
case HIGH:
System.out.println("High");
break;
}
}
}
❤3
🤔4🎉1
public class Test {
public static void main(String[] args) {
Shape s = new Circle(5);

switch (s) {
case Circle c -> System.out.println("Circle with r=" + c.radius());
case Rectangle r -> System.out.println("Rectangle: " + r.width() + "x" + r.height());
}
}

sealed interface Shape permits Circle, Rectangle {}

record Circle(int radius) implements Shape {}

record Rectangle(int width, int height) implements Shape {}
}
❤1
Что выведет код (Java 21+)? 👆
Anonymous Quiz
53%
Circle with r=5
16%
Rectangle: 0x0
31%
<Ошибка компиляции>
🤯5🎉1
public class Test {
public static void main(String[] args) {
Expr expr = new Add(new Literal(2), new Literal(3));

switch (expr) {
case Add(Literal l1, Literal l2) -> System.out.println("Sum: " + (l1.value() + l2.value()));
case Literal l -> System.out.println("Single literal: " + l.value());
default -> System.out.println("Unknown expression");
}
}

sealed interface Expr permits Literal, Add {}

record Literal(int value) implements Expr {}

record Add(Expr left, Expr right) implements Expr {}
}
👍1
🎉1
import java.util.HashSet;
import java.util.Set;

class Item {
private String name;

public Item(String name) {
this.name = name;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Item item = (Item) o;
return name != null ? name.equals(item.name) : item.name == null;
}
}

public class Test {
public static void main(String[] args) {
Set<Item> items = new HashSet<>();
items.add(new Item("apple"));
items.add(new Item("banana"));
items.add(new Item("apple"));

System.out.println(items.size());
}
}
❤2
Что выведет код? 👆
Anonymous Quiz
5%
1
39%
2
43%
3
13%
<Ошибка компиляции>
👍1🎉1
Какой из следующих методов НЕ является методом интерфейса java.util.Collection?
Anonymous Quiz
10%
size()
13%
isEmpty()
22%
get(int index)
22%
contains(Object o)
34%
toArray()
🎉1
import java.util.stream.IntStream;

public class Test {
public static void main(String[] args) {
boolean found = IntStream.range(1, 6)
.peek(x -> System.out.print("P"))
.filter(x -> {
System.out.print("F");
return x % 2 == 0;
})
.anyMatch(x -> {
System.out.print("M");
return x > 2;
});
}
}
👍3
Что выведет код? 👆
Anonymous Quiz
30%
PPPPPFFFFFMMMMM
25%
PPPPPFFFFFMM
33%
PFPFMPFPFM
12%
PFPFMPFPFMPF
🎉1
public class Test {
public static void main(String[] args) {
Parent obj = new Child();
System.out.print(obj.x);
obj.printX();
}
}

class Parent {
public int x = 10;

public void printX() {
System.out.print(" Parent x: " + x);
}
}

class Child extends Parent {
public int x = 20;

@Override
public void printX() {
System.out.print(" Child x: " + x);
}
}
❤2👍1