Question-1:
You have the following code:
ExecutorService executor = [Link](1);
Future<Integer> future = [Link](() -> {
[Link](1000);
return 42;
});
[Link]([Link]());
[Link]();
Which statement is true?
Question-2:
class Shared {
synchronized void doWork() throws InterruptedException {
[Link]("Start ");
wait();
[Link]("End ");
}
}
Shared s = new Shared();
new Thread(() -> {
try { [Link](); } catch (Exception e) {}
}).start();
[Link](500);
[Link]();
What is the output?
Question-3:
class Shared {
synchronized void doWait() throws InterruptedException {
[Link]([Link]().getName() + " waiting");
wait();
[Link]([Link]().getName() + " resumed");
}
}
Shared s = new Shared();
Runnable r = () -> { try { [Link](); } catch (Exception e) {} };
Thread t1 = new Thread(r, "T1");
Thread t2 = new Thread(r, "T2");
[Link](); [Link]();
synchronized(s) {
[Link]();
}
What is guaranteed?
Question-4:
ExecutorService ex = [Link]();
Future<String> f = [Link](() -> {
if (true) throw new IllegalStateException("Fail");
return "Done";
});
try {
[Link]([Link]());
} catch (Exception e) {
[Link]([Link]());
}
[Link]();
What is printed?
Question-5: What is the output?
class Counter {
private int count = 0;
public synchronized void increment() {
count++;
notifyAll();
}
public synchronized void waitForValue(int target) throws InterruptedException {
while (count < target) {
wait();
}
[Link]("Reached " + target);
}
}
Counter c = new Counter();
new Thread(() -> {
try { [Link](5); } catch (Exception e) {}
}).start();
for (int i = 0; i < 5; i++) {
[Link](100);
[Link]();
}
Question-6:
final Object A = new Object();
final Object B = new Object();
Thread t1 = new Thread(() -> {
synchronized (A) {
sleep(50);
synchronized (B) { [Link]("T1 got A then B"); }
}
});
Thread t2 = new Thread(() -> {
synchronized (B) {
sleep(50);
synchronized (A) { [Link]("T2 got B then A"); }
}
});
[Link](); [Link]();
(Assume sleep wraps [Link] and swallows checked exceptions.)
What is most accurate?
Question-7: What output pattern is correct?
ExecutorService ex = [Link](2);
Runnable r1 = () -> { };
Runnable r2 = () -> { throw new RuntimeException("boom"); };
Callable<String> c1 = () -> "ok";
Callable<String> c2 = () -> { throw new IllegalStateException("bad"); };
Future<?> fr1 = [Link](r1);
Future<?> fr2 = [Link](r2);
Future<String> fc1 = [Link](c1);
Future<String> fc2 = [Link](c2);
try { [Link]([Link]()); } catch (Exception e)
{ [Link]("R1:" + e); }
try { [Link]([Link]()); } catch (Exception e)
{ [Link]("R2:" + [Link]()); }
try { [Link]([Link]()); } catch (Exception e)
{ [Link]("C1:" + e); }
try { [Link]([Link]()); } catch (Exception e)
{ [Link]("C2:" + [Link]()); }
[Link]();
Question-8: What is printed by the worker thread?
Object lock = new Object();
Thread t = new Thread(() -> {
synchronized (lock) {
try {
[Link]();
[Link]("woke normally");
} catch (InterruptedException e) {
[Link]("interrupted");
[Link]([Link]().isInterrupted());
}
}
});
[Link]();
[Link](100);
[Link]();
Question-9: What is true about the behavior?
class Box {
private boolean ready = false;
private final Object m1 = new Object();
private final Object m2 = new Object();
void awaitReady() throws InterruptedException {
synchronized (m1) {
if (!ready) [Link]();
}
[Link]("go");
}
void signalReady() {
synchronized (m2) {
ready = true;
[Link]();
}
}
}
Box b = new Box();
new Thread(() -> { try { [Link](); } catch (Exception ignored) {} }).start();
[Link](100);
[Link]();