public class Test {
public static synchronized void main(String[] a) throws InterruptedException {
Thread t = new Thread() {
public void run() {
pong();
}
};
t.start();
Thread.sleep(1000);
System.out.print("Ping");
}
static synchronized void pong() {
System.out.print("Pong");
}
}👍2
🎉2
public class Test {
static void m(Object o) {
System.out.println("Object");
}
static void m(String s) {
System.out.println("String");
}
public static void main(String[] args) {
m(null);
}
}❤3
❤3🎉2
import java.util.*;
public class Test {
public static void process(Collection<String> c) {
System.out.println("String Collection");
}
public static void process(Collection<Integer> c) {
System.out.println("Integer Collection");
}
public static void main(String[] args) {
List<String> list = new ArrayList<>();
process(list);
}
}
❤1
Что выведет код? 👆
Anonymous Quiz
49%
String Collection
16%
Integer Collection
25%
<Ошибка компиляции>
9%
<Runtime‑исключение>
🎉1
public class Test {
static class R implements AutoCloseable {
public void work() {
throw new RuntimeException("WORK");
}
@Override
public void close() {
throw new RuntimeException("CLOSE");
}
}
public static void main(String[] args) {
try (R r = new R()) {
r.work();
} catch (Exception e) {
System.out.print(e.getMessage() + " ");
for (Throwable sup : e.getSuppressed()) {
System.out.print(sup.getMessage() + " ");
}
}
}
}❤1
Что выведет код? 👆
Anonymous Quiz
22%
WORK
45%
WORK CLOSE
16%
CLOSE WORK
17%
<Ошибка RuntimeException>
🎉1
class A {
public static void foo() {
System.out.println("A");
}
}
class B extends A {
public void foo() {
System.out.println("B");
}
}
public class Test {
public static void main(String[] args) {
A a = new B();
a.foo();
}
}🔥2❤1
👍2🎉2
interface A {
default void f() {
System.out.println("A");
}
}
interface B {
default void f() {
System.out.println("B");
}
}
class C implements A, B {
}
public class Test {
public static void main(String[] args) {
new C().f();
}
}❤3
🎉2❤1
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
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
🎉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
🎉3
public class Test {
public static void main(String[] args) {
Object[] arr = new String[1];
arr[0] = 100;
System.out.println(arr[0]);
}
}❤2
Что выведет код? 👆
Anonymous Quiz
34%
100
17%
<Ошибка компиляции>
27%
<Возникнет ArrayStoreException во время выполнения>
22%
<Возникнет ClassCastException во время выполнения>
🎉2👍1
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