2. Given:
1. public class BackHanded {
2. int state = 0;
3. BackHanded(int s) { state = s; }
4. public static void main(String... hi) {
5. BackHanded b1 = new BackHanded(1);
6. BackHanded b2 = new BackHanded(2);
7. System.out.println(b1.go(b1) + " " + b2.go(b2));
8. }
9. int go(BackHanded b) {
10. if(this.state == 2) {
11. b.state = 5;
12. go(this);
13. }
14. return ++this.state;
15. } }
What is the result?
A. 1 2
B. 1 3
C. 1 6
D. 1 7
E. 2 6
F. 2 7
G. Compilation fails.
H. An exception is thrown at runtime.
4. Given:
3. class Sport {
4. Sport play() { System.out.print("play "); return new Sport(); }
5. Sport play(int x) { System.out.print("play x "); return new Sport(); }
6. }
7. class Baseball extends Sport {
8. Baseball play() { System.out.print("baseball "); return new Baseball(); }
9. Sport play(int x) { System.out.print("sport "); return new Sport(); }
10.
11. public static void main(String[] args) {
12. new Baseball().play();
13. new Baseball().play(7);
14. super.play(7);
15. new Sport().play();
16. Sport s = new Baseball();
17. s.play();
18. } }
What is the result?
A. baseball sport sport play play
B. baseball sport play x play sport
C. baseball sport play x play baseball
D. Compilation fails due to a single error.
E. Compilation fails due to errors on more than one line.
Given:
2. class Paratrooper implements Runnable {
3. public void run() {
4. System.out.print(Thread.currentThread().getName() + " ");
5. } }
6. public class Jump {
7. static Paratrooper p;
8. static { p = new Paratrooper(); }
9. { Thread t1 = new Thread(p, "bob"); t1.start(); }
10. public static void main(String[] args) {
11. new Jump();
12. new Thread(new Runnable() { public void run()
{ ; }}, "carol").start();
13. new Thread(new Paratrooper(), "alice").start();
14. }
15. Jump() { Thread t2 = new Thread(p, "ted"); t2.start(); }
16. }
Which are true? (Choose all that apply.)
A. The output could be ted bob alice
B. The output could be bob alice carol
C. The output could be bob carol ted alice
D. Compilation fails due to an error on line 8.
E. Compilation fails due to an error on line 9.
F. Compilation fails due to an error on line 12.
G. Compilation fails due to an error on line 15.
4. Given:
2. public class Volume {
3. Volume v;
4. int size;
5. public static void main(String[] args) {
6. Volume myV = new Volume();
7. final Volume v2;
8. v2 = myV.doStuff(myV);
9. v2.v.size = 7;
10. System.out.print(v2.size);
11. }
12. Volume doStuff(Volume v3) {
13. v3.size = 5;
14. v3.v = new Volume();
15. return v3;
16. } }
What is the result? (Choose all that apply.)
A. 5
B. 7
C. Compilation fails due to an error on line 8.
D. Compilation fails due to an error on line 9.
E. Compilation fails due to an error on line 13.
F. Compilation fails due to an error on line 14.
Given:
1. public class Endless {
2. public static void main(String[] args) {
3. int i = 0;
4. short s = 0;
5. for(int j = 0, k = 0; j < 3; j++) ;
6. for(int j = 0; j < 3; counter(j)) ;
7. for(int j = 0, int k = 0; j < 3; j++) ;
8. for(; i < 5; counter(5), i++) ;
9. for(i = 0; i < 3; i++, System.out.print("howdy ")) ;
10. }
11. static int counter(int y) { return y + 1; }
12. }
What is the result? (Choose all that apply.)
A. howdy howdy howdy
B. The code runs in an endless loop.
C. Compilation fails due to an error on line 5.
D. Compilation fails due to an error on line 6.
E. Compilation fails due to an error on line 7.
F. Compilation fails due to an error on line 8.
G. Compilation fails due to an error on line 9.
27. Given that FileNotFoundException extends IOException, and given:
2. import java.io.*;
3. public class MacPro extends Laptop {
4. public static void main(String[] args) {
5. new MacPro().crunch();
6. }
7. // insert code here
8. }
9. class Laptop {
10. void crunch() throws IOException { }
11. }
Which method(s), inserted independently at line 7, compile? (Choose all that apply.)
A. void crunch() { }
B. void crunch() throws Exception { }
C. void crunch(int x) throws Exception { }
D. void crunch() throws RuntimeException { }
E. void crunch() throws FileNotFoundException { }
25. Given:
2. import java.util.*;
3. public class Vinegar {
4. public static void main(String[] args) {
5. Set mySet = new HashSet();
6. do1(mySet, "0"); do1(mySet, "a");
7. do2(mySet, "0"); do2(mySet, "a");
8. }
9. public static void do1(Set s, String st) {
10. s.add(st);
11. s.add(Integer.parseInt(st));
12. }
13. public static void do2(Set s, String st) {
14. s.add(st);
15. s.add(Integer.parseInt(st));
16. } }
Which are true? (Choose all that apply.)
A. Compilation succeeds.
B. Compilation fails due to an error on line 6.
C. Compilation fails due to an error on line 13.
D. Compilation fails due to an error on line 14.
E. Compilation fails due to an error on line 15.
F. If only the line(s) of code that don’t compile are removed, the code will run without exception.
G. If only the line(s) of code that don’t compile are removed, the code will throw an exception.
33. Given:
1. public class LaSelva extends Beach {
2. LaSelva() { s = "LaSelva"; }
3. public static void main(String[] args) { new LaSelva().go(); }
4. void go() {
5. Beach[] ba = { new Beach(), new LaSelva(), (Beach) new LaSelva() };
6. for(Beach b: ba) System.out.print(b.getBeach().s + " ");
7. }
8. LaSelva getBeach() { return this; }
9. }
10. class Beach {
11. String s;
12. Beach() { s = "Beach"; }
13. Beach getBeach() { return this; }
14. }
What is the result?
A. Beach LaSelva Beach
B. Beach LaSelva LaSelva
C. Beach LaSelva followed by an exception.
D. Compilation fails due to an error at line 5.
E. Compilation fails due to an error at line 6.
F. Compilation fails due to an error at line 8.
G. Compilation fails due to an error at line 13.
35. Given:
3. public class Stealth {
4. public static void main(String[] args) {
5. Integer i = 420;
6. Integer i2;
7. Integer i3;
8. i2 = i.intValue();
9. i3 = i.valueOf(420);
10. System.out.println((i == i2) + " " + (i == i3));
11. } }
What is the result?
A. true true
B. true false
C. false true
D. false false
E. Compilation fails.
F. An exception is thrown at runtime.
36. Given:
2. import java.io.*;
3. interface Risky {
4. String doStuff() throws Exception;
5. Risky doCrazy();
6. void doInsane();
7. }
8. class Bungee implements Risky {
9. public String doStuff() throws IOException {
10. throw new IOException();
11. }
12. public Bungee doCrazy() { return new Bungee(); }
13. public void doInsane() throws NullPointerException {
14. throw new NullPointerException();
15. } }
What is the result? (Choose all that apply.)
A. Compilation succeeds.
B. The Risky interface will not compile.
C. The Bungee.doStuff() method will not compile.
D. The Bungee.doCrazy() method will not compile.
E. The Bungee.doInsane() method will not compile.
1. Given:
1. abstract class Vibrate {
2. static String s = "-";
3. Vibrate() { s += "v"; }
4. }
5. public class Echo extends Vibrate {
6. Echo() { this(7); s += "e"; }
7. Echo(int x) { s += "e2"; }
8. public static void main(String[] args) {
9. System.out.print("made " + s + " ");
10. }
11. static {
12. Echo e = new Echo();
13. System.out.print("block " + s + " ");
14. } }
What is the result?
A. made -ve2e
B. block -ee2v
C. block -ve2e
D. made -eve2 block -eve2
E. made -ve2e block -ve2e
F. block -ve2e made -ve2e
G. block -ve2e made -ve2eve2e
H. Compilation fails.
4. Given:
3. import java.util.*;
4. public class VC {
5. public static void main(String[] args) {
6. List x = new ArrayList();
7. Integer[] a = {3, 1, 4, 1};
8. x = Arrays.asList(a);
9. a[3] = 2;
10. x.set(0, 7);
11. for(Integer i: x) System.out.print(i + " ");
12. x.add(9);
13. System.out.println(x);
14. } }
A. Compilation fails.
B. 3 1 4 2 [7, 1, 4, 1]
C. 3 1 4 2 [7, 1, 4, 2]
D. 7 1 4 2 [7, 1, 4, 2]
E. 3 1 4 2 [7, 1, 4, 1, 9]
F. 3 1 4 2 [7, 1, 4, 2, 9]
G. 7 1 4 2, followed by an exception.
H. 3 1 4 2, followed by an exception.
5. Given:
3. public class Honcho {
4. static boolean b1 = false;
5. static int z = 7;
6. static Long y;
7. public static void main(String[] args) {
8. for(int i = 0; i < 4; i++)
9. go(i);
10. }
11. static void go(int x) {
12. try {
13. if((x == 0) && (!b1 && z == 7)) System.out.print("0 ");
14. if(x < 2 ^ x < 10) System.out.print("1 ");
15. if((x == 2) &&
(y == null | (y.longValue() == 0))) System.out.print("2 ");
16. if(z
17. }
18. catch(Exception e) { System.out.print("e "); }
19. } }
What is the result?
A. 0 1 2 3
B. 1 e 1 3
C. 0 1 e 1 3
D. 0 1 1 1 1 3
E. 1 1 1 2 1 3
F. 0 1 1 1 2 1 3
G. Compilation fails.