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
🎉1
import java.util.HashMap;
import java.util.Map;
public class Test {
private Map<String, String> m = new HashMap<>();
public void Test() {
m.put("Mickey", "Mouse");
m.put("Mickey", "Mantle");
}
public int size() {
return m.size();
}
public static void main(String args[]) {
Test moreNames = new Test();
System.out.println(moreNames.size());
}
}
❤3❤🔥1
🎉3
public class Lazy {
private static boolean initialized = false; static {
Thread t = new Thread(new Runnable() {
public void run() {
initialized = true;
}
});
t.start();
try {
t.join();
} catch (InterruptedException e) {
throw new AssertionError(e);
}
}
public static void main(String[] args) {
System.out.println(initialized);
}
}❤3
🔥3🎉1
public class Test {
public static void main(String[] args) {
String s;
System.out.println(s);
}
}👍4❤1
👍2🎉1
public class Test {
String s;
public static void main(String[] args) {
System.out.println(new Test().s);
}
}❤1👏1
🤯2🎉2👍1
Какое из приведённых ниже утверждений о Java Stream API является точным?
Anonymous Quiz
20%
Терминальные операции Stream всегда возвращают новый Stream для последующей обработки.
20%
Параллельные потоки гарантируют порядок выполнения и всегда быстрее, чем последовательные потоки.
7%
Промежуточные операции в Stream API выполняются немедленно при их объявлении.
52%
Операции выполняются лениво, и промежуточные операции не выполняются до вызова терминальной операции
🎉4❤🔥1❤1🤔1
Какое поведение правильно описывает загрузку классов в Java с участием ClassLoader (CL)?
Anonymous Quiz
27%
Каждый класс загружается в память только один раз для всей JVM, независимо от используемого CL.
22%
Классы с одинаковым полным именем, загруженные разными ClassLoader-ами, считаются разными типами.
39%
ClassLoader загружает все классы JVM в самом начале выполнения программы.
12%
Пользователь не может создавать собственные ClassLoader-ы, доступен только системный загрузчик.
👍2❤1🎉1
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
public class TestMutability {
public static void main(String[] args) {
Set<Employee> set = new HashSet<>();
Employee e1 = new Employee("Alice", 1000);
set.add(e1);
System.out.println(set.contains(e1));
e1.setSalary(2000);
System.out.println(set.contains(e1));
System.out.println(set.size());
}
}
class Employee {
private String name;
private int salary;
public Employee(String name, int salary) {
this.name = name;
this.salary = salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Employee)) return false;
Employee employee = (Employee) o;
return salary == employee.salary && Objects.equals(name, employee.name);
}
@Override
public int hashCode() {
return Objects.hash(name, salary);
}
}
❤1👍1
Что выведет код? 👆
Anonymous Quiz
43%
true true 1
43%
true false 1
7%
false false 1
8%
<Ошибка компиляции>
🎉1
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;
public class Test {
public static void main(String[] args) {
Set<User> users = new TreeSet<>();
users.add(new User("Alice", 1000));
users.add(new User("Bob", 1000));
users.add(new User("Charlie", 1000));
System.out.println(users.size());
}
}
class User implements Comparable<User> {
private String name;
private int score;
public User(String name, int score) {
this.name = name;
this.score = score;
}
@Override
public int compareTo(User o) {
// сравниваем только по score
return Integer.compare(this.score, o.score);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof User)) return false;
User user = (User) o;
return score == user.score && Objects.equals(name, user.name);
}
@Override
public int hashCode() {
return Objects.hash(name, score);
}
}
❤1👍1
🎉2
import java.lang.annotation.*;
import java.lang.reflect.Method;
public class Test {
public static void main(String[] args) throws Exception {
Method method = Service.class.getMethod("doWork");
if (method.isAnnotationPresent(Log.class)) {
System.out.println("Log annotation is present");
} else {
System.out.println("Log annotation is NOT present");
}
}
}
class Service {
@Log
public void doWork() {
System.out.println("Working...");
}
}
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.METHOD)
@interface Log {
}
❤2👍2💅1
Что выведет код? 👆
Anonymous Quiz
40%
Log annotation is present
20%
Log annotation is NOT present
28%
Working...
7%
<Ошибка во время выполнения>
6%
<Ошибка компиляции>
🎉2❤1
import java.lang.annotation.*;
public class Test {
public static void main(String[] args) {
printAnnotation(Child.class);
}
static void printAnnotation(Class<?> clazz) {
if (clazz.isAnnotationPresent(Marker.class)) {
System.out.println("Marker is present");
} else {
System.out.println("Marker is NOT present");
}
}
}
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface Marker {
}
@Marker
class Parent {
}
class Child extends Parent {
}
👍3❤1
Что выведет код? 👆
Anonymous Quiz
45%
Marker is present
33%
Marker is NOT present
13%
<Ошибка во время выполнения>
9%
<Ошибка компиляции>
🎉1
public class LambdaTest {
public static void main(String[] args) {
new LambdaTest().run();
}
public void run() {
Runnable r1 = new Runnable() {
@Override
public void run() {
System.out.println("Anonymous: " + this.getClass().getName());
}
};
Runnable r2 = () -> {
System.out.println("Lambda: " + this.getClass().getName());
};
r1.run();
r2.run();
}
}❤2👍1