Что выведет код?
#Tasks
public class ThreadStopTricky {
private static int value = 0;
public static void main(String[] args) throws Exception {
Thread t = new Thread(() -> {
synchronized (ThreadStopTricky.class) {
value = 1;
try { Thread.sleep(1000); } catch (InterruptedException e) {}
value = 2;
}
});
t.start();
Thread.sleep(50);
t.stop();
synchronized (ThreadStopTricky.class) {
System.out.println(value);
}
}
}#Tasks
👍2
Что выведет код?
#Tasks
import java.io.IOException;
import java.nio.file.*;
import java.util.stream.Stream;
public class Task210726 {
public static void main(String[] args) throws IOException {
Path dir = Paths.get("test");
Files.createDirectory(dir);
Files.createFile(dir.resolve("a.txt"));
Files.createFile(dir.resolve("b.txt"));
Stream<Path> stream = Files.list(dir);
System.out.print(stream.count() + " ");
System.out.print(stream.count());
}
}
#Tasks
👍2
Что выведет код?
#Tasks
public class Task220726 {
public static void main(String[] args) {
String s = "é";
System.out.println(s.getBytes().length);
}
}#Tasks
👍2
Что выведет код?
#Tasks
import java.nio.file.*;
import java.util.*;
import java.util.stream.Stream;
public class FilesLinesTricky {
public static void main(String[] args) throws Exception {
Path file = Paths.get("test.txt");
Files.write(file, Arrays.asList("A", "B", "C"));
try (Stream<String> lines = Files.lines(file)) {
Iterator<String> it = lines.iterator();
System.out.print(it.next());
System.out.print(it.next());
Files.write(file, Arrays.asList("D", "E"), StandardOpenOption.APPEND);
while (it.hasNext()) {
System.out.print(it.next());
}
}
}
}
#Tasks
👍3
Что выведет код?
#Tasks
public class Task240726 {
static class Key {
String id;
Key(String id) { this.id = id; }
@Override
public int hashCode() { return id.hashCode(); }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Key)) return false;
Key key = (Key) o;
return id.equals(key.id);
}
}
static class Value {
Key ref;
Value(Key ref) { this.ref = ref; }
}
public static void main(String[] args) throws InterruptedException {
WeakHashMap<Key, Value> map = new WeakHashMap<>();
Key key1 = new Key("one");
Key key2 = new Key("two");
map.put(key1, new Value(key1));
map.put(key2, new Value(null));
key1 = null;
key2 = null;
System.gc();
Thread.sleep(1000);
System.out.println(map.size());
System.out.println(map.containsKey(new Key("one")));
System.out.println(map.containsKey(new Key("two")));
}
}#Tasks
👍2
Что выведет код?
#Tasks
import java.nio.channels.SeekableByteChannel;
import java.nio.file.*;
import java.nio.*;
public class Task270726 {
public static void main(String[] args) throws Exception {
Path path = Paths.get("test.txt");
try (SeekableByteChannel ch = Files.newByteChannel(path,
StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.READ)) {
ch.write(ByteBuffer.wrap("Hello".getBytes()));
ch.position(10);
ch.write(ByteBuffer.wrap("World".getBytes()));
System.out.println(ch.size());
}
}
}
#Tasks
👍2
Что выведет код?
#Tasks
public class Task280726 {
public static void main(String[] args) {
Object[] src = {"A", "B", 1};
String[] dest = new String[3];
System.arraycopy(src, 0, dest, 0, 3);
System.out.println(dest[0]);
}
}#Tasks
👍3
Что выведет код?
#Tasks
import java.io.*;
public class Task290726 {
public static void main(String[] args) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
MyObject290726 obj = new MyObject290726("Hello");
oos.writeObject(obj);
oos.close();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
MyObject290726 deserialized = (MyObject290726) ois.readObject();
ois.close();
System.out.println(deserialized.value);
}
static class MyObject290726 implements Serializable {
transient String value;
MyObject290726(String value) { this.value = value; }
}
}
#Tasks
👍3
Что выведет данный код?
#Tasks
import java.util.Arrays;
public class Task300726 {
static class Item300726 {
int value;
Item300726(int v) { value = v; }
}
public static void main(String[] args) {
Item300726[] original = {new Item300726(1), new Item300726(2)};
Item300726[] copy = Arrays.copyOf(original, 2);
copy[0].value = 10;
copy[1] = new Item300726(20);
System.out.println(original[0].value);
System.out.println(original[1].value);
}
}
#Tasks
👍3
Что выведет код?
#Tasks
import java.io.*;
public class Task310726 {
static class DataV1 implements Serializable {
private static final long serialVersionUID = 1L;
String name = "John";
}
static class DataV2 implements Serializable {
private static final long serialVersionUID = 1L;
String fullName;
}
public static void main(String[] args) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
oos.writeObject(new DataV1());
}
byte[] bytes = baos.toByteArray();
try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
DataV2 obj = (DataV2) ois.readObject();
System.out.println(obj.fullName);
} catch (Exception e) {
System.out.println(e.getClass().getSimpleName());
}
}
}
#Tasks
👍2