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

✍️По всем вопросам: @Pascal4eg
Download Telegram
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
❤3🎉1
import java.math.BigInteger;

public class Test {
public static void main(String[] args) {
BigInteger fiveThousand = new BigInteger("5000");
BigInteger fiftyThousand = new BigInteger("50000");
BigInteger fiveHundredThousand = new BigInteger("500000");

BigInteger total = BigInteger.ZERO;
total.add(fiveThousand);
total.add(fiftyThousand);
total.add(fiveHundredThousand);
System.out.println(total);
}
}
❤2
Что выведет код? 👆
Anonymous Quiz
26%
0
54%
555000
20%
<Ошибка компиляции>
👍2🎉1
import java.util.Calendar;
import java.util.Date;

public class Test {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
cal.set(1999, 12, 31); // Year, Month, Day
System.out.print(cal.get(Calendar.YEAR) + " ");
Date d = cal.getTime();
System.out.println(d.getDay());
}
}
❤2
Что выведет код? 👆
Anonymous Quiz
45%
1999 31
12%
1999 5
20%
2000 1
22%
<Ошибка компиляции>
🎉1