Given:
public class Threads2 implements Runnable {
public void run() {
System.out.println("run.");
throw new RuntimeException("Problem");
}
public static void main(String[] args) {
Thread t = new Thread(new Threads2());
t.start();
System.out.println("End of method.");
}
}
Which two can be results? (Choose two.)
Correct Answer:
DE
🗳️
End of method.
run.
Exception in thread "Thread-0" java.lang.RuntimeException: Problem at Threads2.run(Threads2.java:5) at java.lang.Thread.run(Unknown Source)
Which two statements are true? (Choose two.)
Correct Answer:
AF
🗳️
Given:
void waitForSignal() {
Object obj = new Object();
synchronized (Thread.currentThread()) {
obj.wait();
obj.notify();
}
}
Which statement is true?
Correct Answer:
B
🗳️
Not quite sure about the answer, because first of all this code will not compile:
Threads2.java:15: unreported exception java.lang.InterruptedException; must be caught or declared to be thrown obj.wait();
^
1 error
Given:
class PingPong2 {
synchronized void hit(long n) {
for(int i = 1; i < 3; i++)
System.out.print(n + "-" + i + " ");
}
}
public class Tester implements Runnable {
static PingPong2 pp2 = new PingPong2();
public static void main(String[] args) {
new Thread(new Tester()).start();
new Thread(new Tester()).start();
}
public void run() { pp2.hit(Thread.currentThread().getId()); }
}
Which statement is true?
Correct Answer:
B
🗳️
Given:
public class Threads4 {
public static void main (String[] args) {
new Threads4().go();
}
public void go() {
Runnable r = new Runnable() {
public void run() {
System.out.print("foo");
}
};
Thread t = new Thread(r);
t.start();
t.start();
}
}
What is the result?
Correct Answer:
B
🗳️
Exception in thread "main" java.lang.IllegalThreadStateException at java.lang.Thread.start(Unknown Source) at Threads4.go(Threads4.java:14) at Threads4.main(Threads4.java:3) foo