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

✍️По всем вопросам: @Pascal4eg
Download Telegram
public class Test {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(() -> System.out.print("X"));

t.run();
t.start();
t.join();

System.out.print("Y");
}
}
👍2❤1
Что выведет код? 👆
Anonymous Quiz
39%
XY
47%
XXY
6%
XYY
8%
XYX
🎉3
public class Test {
static int x = 0;

public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(() -> x = 5);
t.start();
x = 3;
t.join();
System.out.println(x);
}
}
❤3👍2
Что выведет код? 👆
Anonymous Quiz
18%
5
38%
3
44%
3 или 5 (недетерминированно)
🎉5👍1
public class Test {
static void m(Integer i) { System.out.println("Integer"); }
static void m(long l) { System.out.println("long"); }
static void m(int... i) { System.out.println("varargs"); }
static void m(Object o) { System.out.println("Object"); }

public static void main(String[] args) {
m(5);
}
}
❤2👍1
Что выведет код? 👆
Anonymous Quiz
42%
Integer
15%
long
21%
varargs
21%
Object
🎉3
public class Test {
public static void main(String[] args) {
Object[] arr = new String[1];
arr[0] = 100;
System.out.println(arr[0]);
}
}
❤2
import java.io.*;

class User implements Serializable {
String name = "Alice";
transient int age = 30;
}

public class Test {
public static void main(String[] args) throws Exception {
User user = new User();

// Сериализация
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("user.ser"))) {
out.writeObject(user);
}

// Десериализация
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream("user.ser"))) {
User deserialized = (User) in.readObject();
System.out.println(deserialized.name + " " + deserialized.age);
}
}
}
❤2
❤1🎉1
public class Test {
int x = getX();

{
x = 2;
}

public Test() {
x = 3;
}

int getX() {
return 1;
}

public static void main(String[] args) {
Test t = new Test();
System.out.println(t.x);
}
}
❤1
Что выведет код? 👆
Anonymous Quiz
23%
1
15%
2
43%
3
18%
<Ошибка компиляции>
🎉1
public class Test {
public static void main(String[] args) {
System.out.print(testMethod() + " ");
}

static int testMethod() {
int x = 1;
try {
return x;
} finally {
x++;
System.out.print("In finally: x = " + x + " ");
}
}
}
❤1🔥1
🎉1
class Animal {
void speak() {
System.out.println("speaks ");
}
}

class Dog extends Animal {
void speak() {
System.out.println("barks ");
}
}

public class Test {
public static void makeSpeak(Animal animal) {
System.out.print("Animal ");
animal.speak();
}

public static void makeSpeak(Dog dog) {
System.out.print("Dog ");
dog.speak();
}

public static void main(String[] args) {
Animal a = new Dog();
makeSpeak(a);
}
}
👍3
Что выведет код? 👆
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