1.
Given:
void waitForSignal() {
Object obj = new Object();
synchronized (Thread.currentThread()) {
obj.wait();
obj.notify();
}
}
Which statement is true?
2.
Given:
1. class PingPong2 {
2. synchronized void hit(long n) {
3. for(int i = 1; i < 3; i++)
4. System.out.print(n + - + i +  );
5. }
6. }
1. public class Tester implements Runnable {
2. static PingPong2 pp2 = new PingPong2();
3. public static void main(String[] args) {
4. new Thread(new Tester()).start();
5. new Thread(new Tester()).start();
6. }
7. public void run() { pp2.hit(Thread.currentThread().getId()); }
8. }
Which statement is true?
3.
Given:
1. public class Threads4 {
2. public static void main (String[] args) {
3. new Threads4().go();
4. }
5. public void go() {
6. Runnable r = new Runnable() {
7. public void run() {
8. System.out.print("foo");
9. }
10. };
11. Thread t = new Thread(r);
12. t.start();
13. t.start();
14. }
15.}
What is the result?
4.
Given:
1. public class Barn {
2. public static void main(String[] args) {
3. new Barn().go(hi, 1);
4. new Barn().go(hi, world, 2);
5. }
6. public void go(String... y, int x) {
7. System.out.print(y[y.length - 1] +  );
8. }
9. }
What is the result?
5.
Given:
1. class Nav{
2. public enum Direction { NORTH, SOUTH, EAST, WEST }
3. }
1. public class Sprite{
2. // insert code here
3. }
Which code, inserted at line 14, allows the Sprite class to compile?
6.
Given:
1. public class Rainbow {
2. public enum MyColor {
3. RED(0xff0000), GREEN(0x00ff00), BLUE(0x0000ff);
4. private final int rgb;
5. MyColor(int rgb) { this.rgb = rgb; }
6. public int getRGB() { return rgb; }
7. };
8. public static void main(String[] args) {
9. //insert code here
10. }
11.}
Which code fragment, inserted at line 19, allows the Rainbow class to compile?
7.
Given:
1. public class Mud {
2. //insert code here
3. System.out.println(hi);
4. }
5. }
And the following five fragments:
public static void main(String...a) {
public static void main(String.* a) {
public static void main(String... a) {
public static void main(String[]... a) {
public static void main(String...[] a) {
How many of the code fragments, inserted independently at line 2, compile?
8.
Given:
1. class Atom {
2. Atom() { System.out.print(atom ); }
3. }
4. class Rock extends Atom {
5. Rock(String type) { System.out.print(type); }
6. }
7. public class Mountain extends Rock {
8. Mountain() {
9. super(granite );
10. new Rock(granite );
11. }
12. public static void main(String[] a) { new Mountain(); }
13.} What is the result?
9.
Given:
interface TestA { String toString(); }
public class Test {
public static void main(String[] args) {
System.out.println(new TestA() {
public String toString() { return test; }
});
}
}
What is the result?
10.
Given:
1. public static void parse(String str) {
2. try {
3. float f = Float.parseFloat(str);
4. } catch (NumberFormatException nfe) {
5. f = 0;
6. } finally {
7. System.out.println(f);
8. }
9. }
10. public static void main(String[] args) {
11. parse(invalid);
12. }
What is the result?