PRACTICE EXAM 2

The real exam has 60 questions and you are given three hours to complete it. Use the same time limits for this exam. On the real exam, and on all of the exams in this book, give yourself credit only for those questions that you answer 100 percent correctly. For instance, if a question has three correct answers and you get two of the three correct, you get zero credit. There is no partial credit. Good luck!

1. Concerning Java’s Garbage Collector (GC), which are true? (Choose all that apply.)

A. If Object X has a reference to Object Y, then Object Y cannot be GCed.

B. A Java program can request that the GC runs, but such a request does NOT guarantee that the GC will actually run.

C. If the GC decides to delete an object, and if finalize() has never been invoked for that object, it is guaranteed that the GC will invoke finalize() for that object before the object is deleted.

D. Once the GC invokes finalize() on an object, it is guaranteed that the GC will delete that object once finalize() has completed.

E. When the GC runs, it decides whether to remove objects from the heap, the stack, or both.

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.

3. Given:

         42. String s = "";
         43. if(011 == 9) s += 4;
         44. if(0x11 == 17) s += 5;
         45. Integer I = 12345;
         46. if(I.intValue() == Integer.valueOf("12345")) s += 6;
         47. System.out.println(s);

What is the result?

A. 5

B. 45

C. 46

D. 56

E. 456

F. Compilation fails.

G. 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.

5. Given:

          2. public class Self extends Thread {
          3.   public static void main(String[] args) {
          4.     try {
          5.       Thread t = new Thread(new Self());
          6.       t.start();
          7.       t.start();
          8.     } catch (Exception e) { System.out.print("e "); }
          9.   }
         10.  public void run() {
         11.     for(int i = 0; i < 2; i++) 
         12.       System.out.print(Thread.currentThread().getName() + " ");
         13. } }

Which are true? (Choose all that apply.)

A. Compilation fails.

B. No output is produced.

C. The output could be Thread-1 Thread-1 e

D. The output could be Thread-1 e Thread-1

E. The output could be Thread-1 Thread-1 Thread-2 Thread-2

F. The output could be Thread-1 Thread-2 Thread-1 Thread-2

G. The output could be Thread-1 Thread-1 Thread-1 Thread-1

6. Given:

          3. class Stereo { void makeNoise() { assert true; } }
          4. public class BoomBox2 extends Stereo {
          5.   public static void main(String[] args) {
          6.     new BoomBox2().go(args);
          7.   }
          8.   void go(String[] args) {
          9.     if(args.length > 0) makeNoise();
         10.     if(args[0].equals("x")) System.out.print("x ");
         11.     if(args[0] == "x") System.out.println("x2 ");
         12. } }

And (if the code compiles), the invocation:

         java -ea Boombox2 x

What is the result?

A. x

B. x x2

C. An Error is thrown at runtime.

D. Compilation fails due to an error on line 3.

E. Compilation fails due to an error on line 8.

F. Compilation fails due to an error on line 9.

7. Given:

          2. import java.util.*;
          3. public class Olives {
          4.   public static void main(String[] args) {
          5.     Set<Integer> s = new TreeSet<Integer>();
          6.     s.add(23); s.add(42); s.add(new Integer(5));
          7.     Iterator i = s.iterator();
          8.     // while(System.out.print(i.next())) { }
          9.     // for(Integer i2: i) System.out.print(i2);
         10.     // for(Integer i3: s) System.out.print(i3);
         11.     // while(i.hasNext()) System.out.print(i.get());
         12.     // while(i.hasNext()) System.out.print(i.next());
         13. } }

If lines 8–12 are uncommented, independently, which are true? (Choose all that apply.)

A. Line 8 will compile.

B. Line 9 will compile.

C. Line 10 will compile.

D. Line 11 will compile.

E. Line 12 will compile.

F. Of those that compile, the output will be 23425

G. Of those that compile, the output will be 52342

8. Given the proper import statements and:

         23. try {
         24.   File file = new File("myFile.txt");
         25.   PrintWriter pw = new PrintWriter(file); 
         26.   pw.println("line 1");
         27.   pw.close();
         28.   PrintWriter pw2 = new PrintWriter("myFile.txt");
         29.   pw2.println("line 2");
         30.   pw2.close();
         31. } catch (IOException e) { }

What is the result? (Choose all that apply.)

A. No file is created.

B. A file named "myFile.txt" is created.

C. Compilation fails due to an error on line 24.

D. Compilation fails due to an error on line 28.

E. "myFile.txt" contains only one line of data, "line 1"

F. "myFile.txt" contains only one line of data, "line 2"

G. "myFile.txt" contains two lines of data, "line 1" then "line 2"

9. Given this code in a method:

          3. String s = "-";
          4. boolean b = false;
          5. int x = 7, y = 8;
          6. if((x < 8) ∧ (b = true))           s += "∧";
          7. if(!(x > 8) | ++y > 5)             s += "|";
          8. if(++y > 9 && b == true)           s += "&&";
          9. if(y % 8 > 1 || y / (x - 7) > 1)   s += "%";
         10. System.out.println(s);

What is the result?

A. -

B. -|%

C. -∧|%

D. -|&&%

E. -∧|&&%

F. Compilation fails.

G. An exception is thrown at runtime.

10. Given:

          3. public class Limits {
          4.   private int x = 2;
          5.   protected int y = 3;
          6.   private static int m1 = 4;
          7.   protected static int m2 = 5;
          8.   public static void main(String[] args) {
          9.     int x = 6; int y = 7;
         10.     int m1 = 8; int m2 = 9;
         11.     new Limits().new Secret().go();
         12.   }
         13.   class Secret {
         14.     void go() { System.out.println(x + " " + y + " " + m1 + " " + m2); }
         15.   } }

What is the result?

A. 2 3 4 5

B. 2 7 4 9

C. 6 3 8 4

D. 6 7 8 9

E. Compilation fails due to multiple errors.

F. Compilation fails due only to an error on line 11.

G. Compilation fails due only to an error on line 14.

11. Note: This question concerns Serialization. As of this writing, the Serialization topic was officially removed from the objectives, BUT we were still getting reports that some testing centers were using versions of the exam that included Serialization questions. If you choose to ignore the Serialization topic, give yourself a free “correct answer” for this question. (FWIW, your authors believe it’s a good topic to understand.)

Given:

          3. import java.io.*;
          4. class ElectronicDevice { ElectronicDevice() { System.out.print("ed "); }}
          5. class Mp3player extends ElectronicDevice implements Serializable {
          6.   Mp3player() { System.out.print("mp "); }
          7. }
          8. class MiniPlayer extends Mp3player {
          9.   MiniPlayer() { System.out.print("mini "); }
         10.   public static void main(String[] args) {
         11.      MiniPlayer m = new MiniPlayer();
         12.      try {
         13.        FileOutputStream fos = new FileOutputStream("dev.txt");
         14.        ObjectOutputStream os = new ObjectOutputStream(fos);
         15.        os.writeObject(m); os.close();
         16.        FileInputStream fis = new FileInputStream("dev.txt");
         17.        ObjectInputStream is = new ObjectInputStream(fis);
         18.        MiniPlayer m2 = (MiniPlayer) is.readObject(); is.close();
         19.      } catch (Exception x) { System.out.print("x "); }
         20.   } }

What is the result?

A. ed mp mini

B. ed mp mini ed

C. ed mp mini ed mini

D. ed mp mini ed mp mini

E. Compilation fails.

F. "ed mp mini", followed by an exception.

12. Given:

          2. abstract interface Pixie {
          3.   abstract void sprinkle();
          4.   static int dust = 3;
          5. }
          6. abstract class TinkerBell implements Pixie {
          7.   String fly() { return "flying "; }
          8. }
          9. public class ForReal extends TinkerBell {
         10.   public static void main(String[] args) {
         11.      new ForReal().sprinkle();
         12.   }
         13.   public void sprinkle() { System.out.println(fly() + " " + dust); }
         14. }

What is the result? (Choose all that apply.)

A. flying 3

B. Compilation fails because TinkerBell doesn’t properly implement Pixie.

C. Compilation fails because ForReal doesn’t properly extend TinkerBell.

D. Compilation fails because Pixie is not a legal interface.

E. Compilation fails because ForReal doesn’t properly implement Pixie.

F. Compilation fails because TinkerBell is not a legal abstract class.

13. Given:

         2. public class Errrrr {
         3.   static String a = null;
         4.   static String s = "";
         5.   public static void main(String[] args) {
         6.     try {
         7.       a = args[0];
         8.       System.out.print(a);
         9.      s += "t1 ";
         10.    }
         11.    catch (RuntimeException re) { s += "c1 "; }
         12.    finally { s += "f1 "; }
         13.    System.out.println(" " + s);
         14.   } }

And two command-line invocations:

         java Errrrr
         java Errrrr x

What is the result?

A. First: f1, then: x t1

B. First: f1, then: x t1 f1

C. First: c1, then: x t1

D. First: c1, then: x t1 f1

E. First: c1 f1, then: x t1

F. First: c1 f1, then: x t1 f1

G. Compilation fails.

14. Given:

         51. String s = "4.5x4.a.3";
         52. String[] tokens = s.split("\s");
         53. for(String o: tokens)
         54.   System.out.print(o + " ");
   55.
         56. System.out.print(" ");
         57. tokens = s.split("\..");
         58. for(String o: tokens)
         59.   System.out.print(o + " ");

What is the result?

A. 4 x4

B. 4 x4 .

C. 4 x4 3

D. 4 5x4 a 3 4 x4

E. 4.5x4.a.3 4 x4

F. 4.5x4.a.3 4 x4 .

15. Given:

         2. abstract class Tool {
         3.   int SKU;
         4.   abstract void getSKU();
         5. }
         6. public class Hammer {
         7.   // insert code here
         8. }

Which line(s), inserted independently at line 7, will compile? (Choose all that apply.)

A. void getSKU() { ; }

B. private void getSKU() { ; }

C. protected void getSKU() { ; }

D. public void getSKU() { ; }

16. Given:

          3. public class Stubborn implements Runnable {
          4.   static Thread t1;
          5.   static int x = 5;
          6.   public void run() {
          7.     if(Thread.currentThread().getId() == t1.getId()) shove();
          8.     else push();
          9.   }
         10.   static synchronized void push() { shove(); }
         11.   static void shove() {
         12.     synchronized(Stubborn.class) {
         13.       System.out.print(x-- + " ");
         14.       try { Thread.sleep(2000); } catch (Exception e) { ; }
         15.       if(x > 0) push();
         16.   } }
         17.   public static void main(String[] args) {
         18.     t1 = new Thread(new Stubborn());
         19.     t1.start();
         20.     new Thread(new Stubborn()).start();
         21.   } }

Which are true? (Choose all that apply.)

A. Compilation fails.

B. The output is 5 4 3 2 1

C. The output is 5 4 3 2 1 0

D. The program could deadlock.

E. The output could be 5, followed by deadlock.

F. If the sleep() invocation was removed, the chance of deadlock would decrease.

G. As it stands, the program can’t deadlock, but if shove() was changed to synchronized, then the program could deadlock.

17. Given:

          2. class Super {
          3.   static String os = "";
          4.   void doStuff() { os += "super "; }
          5. }
          6. public class PolyTest extends Super {
          7.   public static void main(String[] args) { new PolyTest().go(); }
          8.   void go() {
          9.     Super s = new PolyTest();
         10.     PolyTest p = (PolyTest)s;
         11.     p.doStuff();
         12.     s.doStuff();
         13.     p.doPoly();

         14.     s.doPoly(); 
         15.     System.out.println(os); 
         16.   }
         17.   void doStuff() { os += "over "; }
         18.   void doPoly() { os += "poly "; }
         19. }

What is the result?

A. Compilation fails.

B. over over poly poly

C. over super poly poly

D. super super poly poly

E. An exception is thrown at runtime.

18. Given two files:

         1. package com.wickedlysmart;
         2. import com.wickedlysmart2.*;
         3. public class Launcher {
         4.   public static void main(String[] args) {
         5.     Utils u = new Utils();
         6.     u.do1();
         7.     u.do2();
         8.     u.do3();
         9. } }

and the correctly compiled and located:

         1. package com.wickedlysmart2;
         2. class Utils {
         3.   void do1() { System.out.print("do1 "); }
         4.   protected void do2() { System.out.print("do2 "); }
         5.   public void do3() { System.out.print("do3 "); }
         6. }

What is the result for Launcher? (Choose all that apply.)

A. do1 do2 do3

B. "do1 ", followed by an exception

C. Compilation fails due to an error on line 5 of Launcher.

D. Compilation fails due to an error on line 6 of Launcher.

E. Compilation fails due to an error on line 7 of Launcher.

F. Compilation fails due to an error on line 8 of Launcher.

19. Given:

          2. public class Wacky {
          3.   public static void main(String[] args) {
          4.     int x = 5;
          5.     int y = (x < 6) ? 7 : 8;
          6.     System.out.print(y + " ");
          7.     boolean b = (x < 6) ? false : true;
          8.     System.out.println(b + " ");
          9.     assert(x < 6) : "bob";
         10.     assert( ((x < 6) ? false : true) ) : "fred";
         11. } }

And, if the code compiles, the invocation:

         java -ea Wacky

Which will be contained in the output? (Choose all that apply.)

A. 7

B. 8

C. bob

D. fred

E. true

F. false

G. Compilation fails.

20. Given:

          3. class MotorVehicle {
          4.   protected int doStuff(int x) { return x * 2; }
          5. }
          6. class Bicycle {
          7.   void go(MotorVehicle m) {
          8.     System.out.print(m.doStuff(21) + " ");
          9. } }
         10. public class Beemer extends MotorVehicle {
         11.   public static void main(String[] args) {
         12.      System.out.print(new Beemer().doStuff(11) + " ");
         13.      new Bicycle().go(new Beemer());
         14.      new Bicycle().go(new MotorVehicle());
         15.   }
         16.   int doStuff(int x) { return x * 3; }
         17. }

What is the result? (Choose all that apply.)

A. 22 42 42

B. 22 63 63

C. 33 42 42

D. 33 63 42

E. Compilation fails.

F. An exception is thrown at runtime.

21. Which are true? (Choose all that apply.)

A. A single JAR file can contain only files from a single package.

B. A JAR file can be used only on the machine on which it was created.

C. When javac is using files within a JAR file, it will unJAR the file during compilation.

D. The Java SDK installation directory tree on a Java developer’s computer usually includes a subdirectory tree named jre/lib/ext.

E. A JAR file is structured so that all the files and directories that you’ve “JARed” will be subdirectory(ies) of the META-INF directory.

F. When javac is invoked with a classpath option, and you need to find files in a JAR file, the name of the JAR file must be included at the end of the path.

22. Given the proper import(s), and given:

         13. class NameCompare implements Comparator<Stuff> {
         14.   public int compare(Stuff a, Stuff b) {
         15.     return b.name.compareTo(a.name);
         16. } }
         18. class ValueCompare implements Comparator<Stuff> {
         19.   public int compare(Stuff a, Stuff b) {
         20.     return (a.value - b.value);
         21. } }

Which are true? (Choose all that apply.)

A. This code does not compile.

B. This code allows you to use instances of Stuff as keys in Maps.

C. These two classes properly implement the Comparator interface.

D. NameCompare allows you to sort a collection of Stuff instances alphabetically.

E. ValueCompare allows you to sort a collection of Stuff instances in ascending numeric order.

F. If you changed both occurrences of "compare()" to "compareTo()", the code would compile.

23. Given:

          3. public class Chopper {
          4.   String a = "12b";
          5.   public static void main(String[] args) {
          6.     System.out.println(new Chopper().chop(args[0]));
          7.   }
          8.   int chop(String a) {
          9.     if(a == null) throw new IllegalArgumentException();
         10.     return Integer.parseInt(a);
         11. } }

And, if the code compiles, the invocation:

        java Chopper

What is the result?

A. 12

B. 12b

C. Compilation fails.

D. A NullPointerException is thrown.

E. A NumberFormatException is thrown.

F. An IllegalArgumentException is thrown.

G. An ArrayIndexOutOfBoundsException is thrown.

24. Which, concerning command-line options, are true? (Choose all that apply.)

A. The -D flag is used in conjunction with a name-value pair.

B. The -D flag can be used with javac to set a system property.

C. The -d flag can be used with java to disable assertions.

D. The -d flag can be used with javac to specify where to place .class files.

E. The -d flag can be used with javac to document the locations of deprecated APIs in source files.

25. Given that "it, IT" is the locale code for Italy and that "pt, BR" is the locale code for Brazil, and given:

         51. Date d = new Date();
         52. DateFormat df = DateFormat.getDateInstance(DateFormat.FULL);
         53. Locale[] la = {new Locale("it", "IT"), new Locale("pt", "BR")}; 
         54. for(Locale l: la) {
         55.   df.setLocale(l);
         56.   System.out.println(df.format(d));
         57. }

Which are true? (Choose all that apply.)

A. An exception is thrown at runtime.

B. Compilation fails due to an error on line 53.

C. Compilation fails due to an error on line 55.

D. Compilation fails due to an error on line 56.

E. The output will contain only today’s date for the Italian locale.

F. The output will contain today’s date for both Italian and Brazilian locales.

26. Given this code in a method:

         4. Integer[][] la = {{1,2}, {3,4,5}};
         5. Number[] na = la[1];
         6. Number[] na2 = (Number[])la[0];
         7. Object o = na2;
         8. la[1] = (Number[])o;
         9. la[0] = (Integer[])o;

What is the result? (Choose all that apply.)

A. Compilation succeeds.

B. Compilation fails due to an error on line 4.

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.

H. Compilation succeeds but an exception is thrown at runtime.

27. Given:

          3. public class Ice {
          4.   Long[] stockings = {new Long(3L), new Long(4L), new Long(5L)};
          5.   static int count = 0;
          6.   public static void main(String[] args) {
          7.     new Ice().go();
          8.     System.out.println(count);
          9.   }
         10.   void go() {
         11.     for(short x = 0; x < 5; x++) {
         12.       if(x == 2) return;
         13.       for(long ell: stockings) {
         14.         count++;
         15.         if(ell == 4) break;
         16. } } } }

What is the result? (Choose all that apply.)

A. 2

B. 4

C. 8

D. 12

E. Compilation fails due to an error on line 13.

F. Compilation fails due to an error on line 16.

28. Given the proper import statement(s) and:

         4. Console c = System.console();
         5. char[] pw;
         6. if(c == null) return;
         7. pw = c.readPassword("%s", "pw: ");
         8. System.out.println(c.readLine("%s", "input: "));

Which statements are true? (Choose all that apply.)

A. An exception will be thrown on line 8.

B. The variable pw must be a String, not a char[].

C. No import statements are necessary for the code to compile.

D. If the code compiles and is invoked, line 7 might never run.

E. Without a third argument, the readPassword() method will echo the password the user types.

F. If line 4 was replaced with the following code, the program would still compile: "Console c = new Console();".

29. Given that:

Exception is the superclass of IOException, and

IOException is the superclass of FileNotFoundException, and

          3. import java.io.*;
          4. class Physicist {
          5.   void think() throws IOException { }
          6. }
          7. public class Feynman extends Physicist {
          8.   public static void main(String[] args) {
          9.     new Feynman().think();
         10.   }
         11.   // insert method here
         12. }

Which of the following methods, inserted independently at line 11, compiles? (Choose all that apply.)

A. void think() throws Exception { }

B. void think() throws FileNotFoundException { }

C. public void think() { }

D. protected void think() throws IOException { }

E. private void think() throws IOException { }

F. void think() { int x = 7/0; }

30. Given:

          4. public class Zingseng extends Thread {
          5.   public static void main(String[] args) throws Exception {
          6.     Thread t1 = new Thread(new Zingseng());
          7.     t1.start();
          8.     // insert code here
          9.     for(int i = 0; i < 1000; i++) // Loop #1
         10.        System.out.print(Thread.currentThread().getName() + " ");
         11.  }
         12.  public void run() {
         13.      for(int i = 0; i < 1000; i++) // Loop #2
         14.        System.out.print(Thread.currentThread().getName() + " ");
         15.   } }

Which code, inserted independently at line 8, will make most (or all) of Loop #2’s iterations run before most (or all) of Loop #1’s iterations? (Choose all that apply.)

A. // just a comment

B. t1.join();

C. t1.yield();

D. t1.sleep(1000);

31. Given:

          2. public class Kant extends Philosopher {
          3.   // insert code here
          5.   public static void main(String[] args) {
          6.     new Kant("Homer");
          7.     new Kant();
          8.   }
          9. }
         10. class Philosopher {
         11.   Philosopher(String s) { System.out.print(s + " "); }
         12. }

Which set(s) of code, inserted independently at line 3, produce the output "Homer Bart"? (Choose all that apply.)

A. Kant() { this("Bart"); }

Kant(String s) { super(s); }

B. Kant() { super("Bart"); }

Kant(String s) { super(s); }

C. Kant() { super(); }

Kant(String s) { super(s); }

D. Kant() { super("Bart"); }

Kant(String s) { this(); }

E. Kant() { super("Bart"); }

Kant(String s) { this("Homer"); }

32. Given:

         2. public class Epoch {
         3.   static int jurassic = 0;
         4.   public static void main(String[] args) {
         5.     assert(doStuff(5));
         6.   }
         7.   static boolean doStuff(int x) {
         8.     jurassic += x;
         9.     return true;
        10. } }

Which are true? (Choose all that apply.)

A. The code compiles using javac -ea Epoch.java

B. The assert statement on line 5 is used appropriately.

C. The code compiles using javac -source 1.3 Epoch.java

D. The code compiles using javac -source 1.4 Epoch.java

E. The code compiles using javac -source 1.6 Epoch.java

F. The code will not compile using any version of the javac compiler.

33. Given

         1. public class Kaput {
         2.   Kaput myK;
         3.   String degree = "0";
         4.   public static void main(String[] args) {
         5.     Kaput k1 = new Kaput();
         6.     go(k1);
         7.     System.out.println(k1.degree);
         8. }

         9. static Kaput go(Kaput k) {
        10.  final Kaput k1 = new Kaput();
        11.  k.myK = k1;
        12.  k.myK.degree = "7";
        13.  return k.myK;
        14. } }

What is the result?

A. 0

B. 7

C. null

D. Compilation fails.

E. An exception is thrown at runtime.

34. Given:

          3. class Holder {
          4.   enum Gas { ARGON, HELIUM };
          5. }
          6. public class Basket extends Holder {
          7.   public static void main(String[] args) {
          8.     short s = 7; long l = 9L; float f = 4.0f;
          9.     int i = 3; char c = 'c'; byte b = 5;
         10.     // insert code here
         11.       default: System.out.println("howdy");
         12. } } }

Which line(s) of code (if any), inserted independently at line 10, will compile? (Choose all that apply.)

A. switch (s) {

B. switch (l) {

C. switch (f) {

D. switch (i) {

E. switch (c) {

F. switch (b) {

G. switch (Gas.ARGON) {

H. The code will not compile due to additional error(s).

35. Given

         2. class Horse {
         3.   static String s = "";
         4.   void beBrisk() { s += "trot "; }
         5. }

         6. public class Andi extends Horse {
         7.   void beBrisk() { s += "tolt "; }
         8.   public static void main(String[] args) {
         9.     Horse x0 = new Horse();
        10.     Horse x1 = new Andi(); x1.beBrisk();
        11.     Andi x2 = (Andi)x1; x2.beBrisk(); 
        12.     Andi x3 = (Andi)x0; x3.beBrisk();
        13.     System.out.println(s);
        14. } }

What is the result?

A. tolt tolt tolt

B. trot tolt trot

C. trot tolt tolt

D. Compilation fails.

E. An exception is thrown at runtime.

36. Given:

          3. class Tire {
          4.   private static int x = 6;
          5.   public static class Wheel {
          6.     void go() { System.out.print("roll " + x++); }
          7. } }
          8. public class Car {
          9.   public static void main(String[] args) {
         10.     // insert code here
         11. } }

And the three code fragments:

I. new Tire.Wheel().go();

II. Tire t = new Tire(); t.Wheel().go();

III. Tire.Wheel w = new Tire.Wheel(); w.go();

Assuming we insert a single fragment at line 10, which are true? (Choose all that apply.)

A. Once compiled, the output will be "roll 6"

B. Once compiled, the output will be "roll 7"

C. Fragment I, inserted at line 10, will compile.

D. Fragment II, inserted at line 10, will compile.

E. Fragment III, inserted at line 10, will compile.

F. Taken separately, class Tire will not compile on its own.

37. Given:

         3. public class Fortran {
         4.   static int bump(int i) { return i + 2; }
         5.   public static void main(String[] args) {
         6.     for(int x = 0; x < 5; bump(x))
         7.       System.out.print(x + " ");
         8. } }

What is the result?

A. 2 4

B. 0 2 4

C. 2 4 6

D. 0 2 4 6

E. Compilation fails.

F. Some other result occurs.

38. You need the three classes and/or interfaces to work together to produce the output "Kara Charis". Fill in the blanks using the following fragments so the correct is-a and has-a relationships are established to produce this output. Note: You may not need to fill in every empty blank space, but a blank space can hold only one fragment. Also note that not all the fragments will be used, and that fragments CAN be used more than once.

Code to complete:

Image

Fragments:

Image

39. Given:

         1. import java.util.*;
         2. public class PirateTalk {
         3.   public static void main(String... arrrrgs) {
         4.   Properties p = System.getProperties();
         5.   p.setProperty("pirate", "scurvy");
         6.   String s = p.getProperty("argProp") + " ";
         7.   s += p.getProperty("pirate");
         8.   System.out.println(s);
         9. } }

And the command-line invocation:

         java PirateTalk -DargProp="dog,"

What is the result?

A. dog, scurvy

B. null scurvy

C. scurvy dog,

D. scurvy null

E. Compilation fails.

F. An exception is thrown at runtime.

G. An “unrecognized option” command-line error is thrown.

40. Given:

          2. import java.util.*;
          3. public class VLA implements Comparator<VLA> {
          4.   int dishSize;
          5.   public static void main(String[] args) {
          6.     VLA[] va = {new VLA(40), new VLA(200), new VLA(60)};
    7.
          8.     for(VLA v: va) System.out.print(v.dishSize + " ");
          9.     int index = Arrays.binarySearch(va, new VLA(60), va[0]);
         10.     System.out.print(index + " ");
         11.     Arrays.sort(va);
         12.     for(VLA v: va) System.out.print(v.dishSize + " ");
         13.     index = Arrays.binarySearch(va, new VLA(60), va[0]);
         14.     System.out.println(index);
         15.   }
         16.   public int compare(VLA a, VLA b) {
         17.     return a.dishSize - b.dishSize;
         18.   }
         19.   VLA(int d) { dishSize = d; }
         20. }

Which result is most likely?

A. Compilation fails.

B. "40 200 60 2 40 60 200 1"

C. "40 200 60 -2 40 60 200 1"

D. "40 200 60", followed by an exception.

E. "40 200 60 -2", followed by an exception.

41. Given:

          2. class RainCatcher {
          3.   static StringBuffer s;
          4.   public static void main(String[] args) {
          5.     Integer i = new Integer(42);
          6.     for(int j = 40; j < i; i--)
          7.       switch(i) {
          8.          case 41: s.append("41 ");
          9.          default: s.append("def ");
         10.          case 42: s.append("42 ");
         11.       }
         12.     System.out.println(s);
         13. } }

What is the result?

A. 41 def

B. 41 def 42

C. 42 41 def 42

D. An exception is thrown at runtime.

E. Compilation fails due to an error on line 6.

F. Compilation fails due to an error on line 7.

42. Given:

         2. import java.util.*;
         3. class Snack {
         4.   static List<String> s1 = new ArrayList<String>();
         5. }
         6. public class Chips extends Snack {
         7.   public static void main(String[] args) {
         8.     List c1 = new ArrayList();
         9.     s1.add("1"); s1.add("2");
        10.     c1.add("3"); c1.add("4");
        11.     getStuff(s1, c1);
        12.   }
        13.  static void getStuff(List<String> a1, List a2) {
        14.    for(String s1: a1) System.out.print(s1 + " ");
        15.    for(String s2: a2) System.out.print(s2 + " ");
        16. } }

What is the result? (Choose all that apply.)

A. "1 2 3 4"

B. "1 2", followed by an exception

C. An exception is thrown with no other output.

D. Compilation fails due to an error on line 13.

E. Compilation fails due to an error on line 14.

F. Compilation fails due to an error on line 15.

43. Given that Calendar.MONTH starts with January == 0, and given:

          3. import java.util.*;
          4. public class Wise {
          5.   public static void main(String[] args) {
          6.     Calendar c = Calendar.getInstance();
          7.     c.set(1999,11,25);
          8.     c.roll(Calendar.MONTH, 3);
          9.     c.add(Calendar.DATE, 10);
         10.     System.out.println(c.getTime());
         11. } }

And, if the program compiles, what date is represented in the output?

A. March 4, 1999

B. April 4, 1999

C. March 4, 2000

D. April 4, 2000

E. Compilation fails.

F. An exception is thrown at runtime.

44. Given the following two files containing Light.java and Dark.java:

          2. package ec.ram;
  3. public class Light{}
  4. class Burn{}
  2. package ec.ram;
  3. public class Dark{}
  4. class Melt{}

And if those two files are located in the following directory structure:

Image

And the following commands are executed, in order, from the ROOT directory:

     javac Light.java -cp checker/dira -d checker/dirb
javac Dark.java -cp checker/dirb -d checker/dira
jar -cf checker/dira/a.jar checker

A new JAR file is created after executing the above commands. Which of the following files will exist inside that JAR file? (Choose all that apply.)

A. [JAR]/dira/ec/ram/Melt.class

B. [JAR]/checker/dirb/ec/ram/Burn.class

C. [JAR]/dirb/ec/ram/Melt.class

D. [JAR]/checker/dira/ec/ram/Burn.class

E. [JAR]/dira/a.jar

F. [JAR]/checker/dira/a.jar

45. Given:

         42. String s1 = " ";
 43. StringBuffer s2 = new StringBuffer(" ");
 44. StringBuilder s3 = new StringBuilder(" ");
 45. for(int i = 0; i < 1000; i++) // Loop #1
 46.   s1 = s1.concat("a");
 47. for(int i = 0; i < 1000; i++) // Loop #2
 48.   s2.append("a");
 49. for(int i = 0; i < 1000; i++) // Loop #3
 50.   s3.append("a");

Which statements will typically be true? (Choose all that apply.)

A. Compilation fails.

B. Loop #3 will tend to execute faster than Loop #2.

C. Loop #1 will tend to use less memory than the other two loops.

D. Loop #1 will tend to use more memory than the other two loops.

E. All three loops will tend to use about the same amount of memory.

F. In order for this code to compile, the java.util package is required.

G. If multiple threads need to use an object, the s2 object should be safer than the s3 object.

46. Given:

    2. public class Payroll {
  3.   int salary;
  4.   int getSalary() { return salary; }
  5.   void setSalary(int s) {
  6.     assert(s > 30000);
  7.     salary = s;
          8. } }

Which are true? (Choose all that apply.)

A. Compilation fails.

B. The class is well encapsulated as it stands.

C. Removing line 6 would weaken the class’s degree of cohesion.

D. Removing line 6 would weaken the class’s degree of encapsulation.

E. If the salary variable was private, the class would be well encapsulated.

F. Removing line 6 would make the class’s use of assertions more appropriate.

47. Given:

          1. class GardenTool {
  2.   static String s = "";
  3.   String name = "Tool ";
  4.   GardenTool(String arg) { this(); s += name; }
  5.   GardenTool() { s += "gt "; }
  6. }
  7. public class Rake extends GardenTool {
  8.   { name = "Rake "; }
  9.   Rake(String arg) { s += name; }
 10.   public static void main(String[] args) {
 11.     new GardenTool("hey ");
 12.     new Rake("hi ");
 13.     System.out.println(s);
 14.  }
 15.  { name = "myRake "; }
 16. }

What is the result?

A. Tool Rake

B. Tool myRake

C. gt Tool Rake

D. gt Tool myRake

E. gt Tool gt Rake

F. gt Tool gt myRake

G. gt Tool gt Tool myRake

H. Compilation fails.

48. Given:

          3. public class Race {
          4.   public static void main(String[] args) {
          5.     Horse h = new Horse();
          6.     Thread t1 = new Thread(h, "Andi");
          7.     Thread t2 = new Thread(h, "Eyra");
          8.     new Race().go(t2);
          9.     t1.start();
         10.     t2.start();
         11.   }
         12.   void go(Thread t) { t.start(); }
         13. }
         14. class Horse implements Runnable {
         15.   public void run() {
         16.     System.out.print(Thread.currentThread().getName() + " ");
         17. } }

What is the result? (Choose all that apply.)

A. Compilation fails.

B. No output is produced.

C. The output could be: "Andi Eyra "

D. The output could be: "Eyra Andi Eyra "

E. The output could be: "Eyra ", followed by an exception.

F. The output could be: "Eyra Andi ", followed by an exception.

49. Given the proper import statement(s) and given:

          4. Map<String, String> h = new Hashtable<String, String>();
          5. String[] k = {"1", "2", "3", null};
          6. String[] v = {"a", "b", null, "d"};
  7.
          8. for(int i=0; i<4; i++) {
          9.    h.put(k[i], v[i]);
         10. System.out.print(h.get(k[i]) + " ");
         11. }
         12. System.out.print(h.size() + " " + h.values() + " ");

What result is most likely?

A. Compilation fails.

B. "a b d 3 [b, d, a]"

C. "a b null d 3 [b, d, a]"

D. "a b null d 4 [b, d, null, a]"

E. "a b", followed by an exception.

F. "a b null", followed by an exception.

50. Given:

          3. public class Fiji {
          4.   static Fiji base;
          5.   Fiji f;
          6.   public static void main(String[] args) {
          7.     new Fiji().go();
          8.     // do more stuff
          9. }
         10. void go() {
         11.   Fiji f1 = new Fiji();
         12.   base = f1;
         13.   Fiji f2 = new Fiji();
         14.   f1.f = f2;
         15.   Fiji f3 = f1.f;
         16.   f2.f = f1;
         17.   base = null; f1 = null; f2 = null;
         18.   // do stuff
         19. } }

Which are true? (Choose all that apply.)

A. At line 8, one object is eligible for garbage collection.

B. At line 8, two objects are eligible for garbage collection.

C. At line 8, three objects are eligible for garbage collection.

D. At line 18, 0 objects are eligible for garbage collection.

E. At line 18, two objects are eligible for garbage collection.

F. At line 18, three objects are eligible for garbage collection.

51. Your company makes compute-intensive, 3D rendering software for the movie industry. Your chief scientist has just discovered a new algorithm for several key methods in a commonly used utility class. The new algorithm will decrease processing time by 15 percent, without having to change any method signatures. After you change these key methods, and in the course of rigorous system testing, you discover that the changes have introduced no new bugs into the software.

In terms of your software’s overall design, which are probably true? (Choose all that apply.)

A. Your software is well encapsulated.

B. Your software demonstrated low cohesion.

C. Your software demonstrated high cohesion.

D. Your software demonstrated loose coupling.

E. Your software demonstrated tight coupling.

52. Which are true about the classes and interfaces in java.util? (Choose all that apply.)

A. LinkedHashSet is-a Collection.

B. Vector is-a List.

C. LinkedList is-a Queue.

D. LinkedHashMap is-a Collection.

E. TreeMap is-a SortedMap.

F. Queue is-a Collection.

G. Hashtable is-a Set.

53. Given that File.createNewFile() throws IOException, and that FileNotFoundException extends IOException, use the following fragments to fill in the blanks so that the code compiles. You must use all the fragments, and each fragment can be used only once. Note: As in the real exam, some drag-and-drop style questions might have several correct answers. You will receive full credit for ANY one of the correct answers.

         import java.io.*;
         public class SticksMud {
   public static void main(String[] args) {
             try {
             File dir = new File("dir");
             dir.createNewFile();
             }

Image

          } }

Fragments:

Image

54. Given:

          2. class Grandfather {
          3.   static String name = "gf ";
          4.   String doStuff() { return "grandf "; }
          5. }
          6. class Father extends Grandfather {
          7.   static String name = "fa ";
          8.   String doStuff() { return "father "; }
          9. }
         10. public class Child extends Father {
         11.   static String name = "ch ";
         12.   String doStuff() { return "child "; }
         13.   public static void main(String[] args) {
         14.     Father f = new Father();
         15.     System.out.print(((Grandfather)f).name
                                + ((Grandfather)f).doStuff()) ;
         16.     Child c = new Child();
         17.     System.out.println(((Grandfather)c).name
                                + ((Grandfather)c).doStuff() + ((Father)c).doStuff());
         18. } }

What is the result? (Choose all that apply.)

A. Compilation fails.

B. fa father ch child child

C. gf father gf child child

D. fa grandf ch grandf father

E. gf grandf gf grandf father

F. An exception is thrown at runtime.

55. Given:

          1. class MyClass { }

And given that MyClass2 has properly overridden equals() and hashCode(), objects from which classes make good hashing keys? (Choose all that apply.)

A. MyClass

B. MyClass2

C. java.lang.String

D. java.lang.Integer

E. java.lang.StringBuilder

56. Given:

          2. public class Buffalo {
          3.   static int x;
          4.   int y;
          5.   public static int getX() { return x; }
          6.   public static void setX(int newX) { x = newX; }
          7.   public int getY() { return y; }
          8.   public void setY(int newY) { y = newY; }
          9. }

Which lines of code need to be changed to make the class thread safe? (Choose all that apply.)

A. Line 2

B. Line 3

C. Line 4

D. Line 5

E. Line 6

F. Line 7

G. Line 8

57. Given:

          2. class Animal { }
          3. class Dog extends Animal { }
          4. class Cat extends Animal { }
          5. public class Mixer<A extends Animal> {
          6.   public <C extends Cat> Mixer<? super Dog> useMe(A a, C c) {
          7.     //Insert Code Here
          8. } }

Which, inserted independently at line 7, compile? (Choose all that apply.)

A. return null;

B. return new Mixer<Dog>();

C. return new Mixer<Animal>();

D. return new Mixer<A>();

E. return new Mixer<a>();

F. return new Mixer<c>();

58. Given:

          2. class Chilis {
          3.   Chilis(String c, int h) { color = c; hotness = h; }
          4.   String color;
          5.   private int hotness;
          6.   public boolean equals(Object o) {
          7.     Chilis c = (Chilis)o;
          8.     if(color.equals(c.color) && (hotness == c.hotness)) return true;
          9.     return false;
         10.   }
         11.   // insert code here
         12. }

Which, inserted independently at line 11, fulfill the equals() and hashCode() contract for Chilis? (Choose all that apply.)

A. public int hashCode() { return 7; }

B. public int hashCode() { return hotness; }

C. public int hashCode() { return color.length(); }

D. public int hashCode() { return (int)(Math.random() * 200); }

E. public int hashCode() { return (color.length() + hotness); }

59. Given the proper import(s), and this code in a method:

          4. List<String> x = new LinkedList<String>();
          5. Set<String> hs = new HashSet<String>();
          6. String[] v = {"a", "b", "c", "b", "a"};
          7. for(String s: v) {
          8.   x.add(s); hs.add(s);
          9. }
         10. System.out.print(hs.size() + " " + x.size() + " ");
         11. HashSet hs2 = new HashSet(x);
         12. LinkedList x2 = new LinkedList(hs);
         13. System.out.println(hs2.size() + " " + x2.size());

What is the result?

A. 3 3 3 3

B. 3 5 3 3

C. 3 5 3 5

D. 5 5 3 3

E. 5 5 5 5

F. Compilation fails.

G. An exception is thrown at runtime.

60. Given the proper import(s), and given:

          4. public static void main(String[] args) {
          5.   List<Integer> x = new ArrayList<Integer>();
          6.   x.add(new Integer(3));
          7.   doStuff(x);
          8.   for(Integer i: x)
          9.    System.out.print(i + " ");
         10. }
         11. static void doStuff(List y) {
         12.   y.add(new Integer(4));
         13.   y.add(new Float(3.14f));
         14. }

What is the result? (Choose all that apply.)

A. Compilation fails.

B. The output will be "4 "

C. The output will be "3 4 "

D. The output will be "3 4 3.14 "

E. The output will be "3 3.14 4 "

F. The output will be "3 4 ", followed by an exception.

G. An exception will be thrown before any output is produced.

Quick Answer Key

1. B, C

2. F

3. E

4. D

5. C, D

6. C

7. C, E, G

8. B, F

9. D

10. A

11. B

12. A

13. F

14. E

15. A, B, C, D

16. C

17. A

18. C, D, E, F

19. A, D, F

20. E

21. D, F

22. C, E

23. G

24. A, D

25. C

26. F

27. B

28. D

29. B, C, D, F

30. B, D

31. A, B

32. D, E

33. A

34. A, D, E, F, G

35. E

36. A, C, E

37. F

38. Drag and Drop

39. B

40. E

41. D

42. F

43. B

44. B

45. B, D, G

46. E, F

47. F

48. E, F

49. E

50. C, D

51. A, D

52. A, B, C, E, F

53. Drag and Drop

54. C

55. B, C, D

56. B through G

57. A, B, C

58. A, B, C, E

59. B

60. F

PRACTICE EXAM 2: ANSWERS

1. Concerning Java’s Garbage Collector (GC), which are true? (Choose all that apply.)

A. If Object X has a reference to Object Y, then Object Y cannot be GCed.

B. A Java program can request that the GC runs, but such a request does NOT guarantee that the GC will actually run.

C. If the GC decides to delete an object, and if finalize() has never been invoked for that object, it is guaranteed that the GC will invoke finalize() for that object before the object is deleted.

D. Once the GC invokes finalize() on an object, it is guaranteed that the GC will delete that object once finalize() has completed.

E. When the GC runs, it decides whether to remove objects from the heap, the stack, or both.

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.

3. Given:

          42. String s = "";
          43. if(011 == 9) s += 4;
          44. if(0x11 == 17) s += 5;
          45. Integer I = 12345;
          46. if(I.intValue() == Integer.valueOf("12345")) s += 6;
          47. System.out.println(s);

What is the result?

A. 5

B. 45

C. 46

D. 56

E. 456

F. Compilation fails.

G. 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.

5. Given:

          2. public class Self extends Thread {
          3.   public static void main(String[] args) {
          4.     try {
          5.       Thread t = new Thread(new Self());
          6.       t.start();
          7.       t.start();
          8.     } catch (Exception e) { System.out.print("e "); }
          9.   }
         10.   public void run() {
         11.     for(int i = 0; i < 2; i++) 
         12.       System.out.print(Thread.currentThread().getName() + " ");
         13. } }

Which are true? (Choose all that apply.)

A. Compilation fails.

B. No output is produced.

C. The output could be Thread-1 Thread-1 e

D. The output could be Thread-1 e Thread-1

E. The output could be Thread-1 Thread-1 Thread-2 Thread-2

F. The output could be Thread-1 Thread-2 Thread-1 Thread-2

G. The output could be Thread-1 Thread-1 Thread-1 Thread-1

6. Given:

          3. class Stereo { void makeNoise() { assert true; } }
          4. public class BoomBox2 extends Stereo {
          5.   public static void main(String[] args) {
          6.     new BoomBox2().go(args);
          7.   }
          8.   void go(String[] args) {
          9.     if(args.length > 0) makeNoise();
         10.     if(args[0].equals("x")) System.out.print("x ");
         11.     if(args[0] == "x") System.out.println("x2 ");
         12. } }

And (if the code compiles), the invocation:

         java -ea Boombox2 x

What is the result?

A. x

B. x x2

C. An error is thrown at runtime.

D. Compilation fails due to an error on line 3.

E. Compilation fails due to an error on line 8.

F. Compilation fails due to an error on line 9.

7. Given:

          2. import java.util.*;
          3. public class Olives {
          4.   public static void main(String[] args) {
          5.     Set<Integer> s = new TreeSet<Integer>();
          6.     s.add(23); s.add(42); s.add(new Integer(5));
          7.     Iterator i = s.iterator();
          8.     // while(System.out.print(i.next())) { }
          9.     // for(Integer i2: i) System.out.print(i2);
         10.     // for(Integer i3: s) System.out.print(i3);
         11.     // while(i.hasNext()) System.out.print(i.get());
         12.     // while(i.hasNext()) System.out.print(i.next());
         13. } }

If lines 8–12 are uncommented, independently, which are true? (Choose all that apply.)

A. Line 8 will compile.

B. Line 9 will compile.

C. Line 10 will compile.

D. Line 11 will compile.

E. Line 12 will compile.

F. Of those that compile, the output will be 23425

G. Of those that compile, the output will be 52342

8. Given the proper import statements and:

         23. try {
         24.   File file = new File("myFile.txt");
         25.   PrintWriter pw = new PrintWriter(file); 
         26.   pw.println("line 1");
         27.   pw.close();
         28.   PrintWriter pw2 = new PrintWriter("myFile.txt");
         29.   pw2.println("line 2");
         30.   pw2.close();
         31. } catch (IOException e) { }

What is the result? (Choose all that apply.)

A. No file is created.

B. A file named "myFile.txt" is created.

C. Compilation fails due to an error on line 24.

D. Compilation fails due to an error on line 28.

E. "myFile.txt" contains only one line of data, "line 1"

F. "myFile.txt" contains only one line of data, "line 2"

G. "myFile.txt" contains two lines of data, "line 1" then "line 2"

9. Given this code in a method:

          3. String s = "-";
          4. boolean b = false;
          5. int x = 7, y = 8;
          6. if((x < 8) ∧ (b = true))         s += "^";
          7. if(!(x > 8) | ++y > 5)           s += "|";
          8. if(++y > 9 && b == true)         s += "&&";
          9. if(y % 8 > 1 || y / (x - 7) > 1) s += "%";
         10. System.out.println(s);

What is the result?

A. -

B. -|%

C. -^|%

D. -|&&%

E. -^|&&%

F. Compilation fails.

G. An exception is thrown at runtime.

10. Given:

          3. public class Limits {
          4.   private int x = 2;
          5.   protected int y = 3;
          6.   private static int m1 = 4;
          7.   protected static int m2 = 5;
          8.   public static void main(String[] args) {
          9.     int x = 6; int y = 7;
         10.     int m1 = 8; int m2 = 9;
         11.     new Limits().new Secret().go();
         12.   }
         13.   class Secret {
         14.     void go() { System.out.println(x + " " + y + " " + m1 + " " + m2); }
         15. } }

What is the result?

A. 2 3 4 5

B. 2 7 4 9

C. 6 3 8 4

D. 6 7 8 9

E. Compilation fails due to multiple errors.

F. Compilation fails due only to an error on line 11.

G. Compilation fails due only to an error on line 14.

11. Note: This question concerns Serialization. As of this writing, the Serialization topic was officially removed from the objectives, BUT we were still getting reports that some testing centers were using versions of the exam that included Serialization questions. If you choose to ignore the Serialization topic, give yourself a free “correct answer” for this question. (FWIW, your authors believe it’s a good topic to understand.)

Given:

          3. import java.io.*;
          4. class ElectronicDevice { ElectronicDevice() { System.out.print("ed "); }}
          5. class Mp3player extends ElectronicDevice implements Serializable {
          6.   Mp3player() { System.out.print("mp "); }
          7. }

          8. class MiniPlayer extends Mp3player {
          9. MiniPlayer() { System.out.print("mini "); }
         10. public static void main(String[] args) {
         11.   MiniPlayer m = new MiniPlayer();
         12.   try {
         13.     FileOutputStream fos = new FileOutputStream("dev.txt");
         14.     ObjectOutputStream os = new ObjectOutputStream(fos);
         15.     os.writeObject(m); os.close();
         16.     FileInputStream fis = new FileInputStream("dev.txt");
         17.     ObjectInputStream is = new ObjectInputStream(fis);
         18.     MiniPlayer m2 = (MiniPlayer) is.readObject(); is.close();
         19.   } catch (Exception x) { System.out.print("x "); }
         20. } }

What is the result?

A. ed mp mini

B. ed mp mini ed

C. ed mp mini ed mini

D. ed mp mini ed mp mini

E. Compilation fails.

F. "ed mp mini", followed by an exception.

12. Given:

          2. abstract interface Pixie {
          3.   abstract void sprinkle();
          4.   static int dust = 3;
          5. }
          6. abstract class TinkerBell implements Pixie {
          7.   String fly() { return "flying "; }
          8. }
          9. public class ForReal extends TinkerBell {
         10.   public static void main(String[] args) {
         11.     new ForReal().sprinkle();
         12.   }
         13.   public void sprinkle() { System.out.println(fly() + " " + dust); }
         14. }

What is the result? (Choose all that apply.)

A. flying 3

B. Compilation fails because TinkerBell doesn’t properly implement Pixie.

C. Compilation fails because ForReal doesn’t properly extend TinkerBell.

D. Compilation fails because Pixie is not a legal interface.

E. Compilation fails because ForReal doesn’t properly implement Pixie.

F. Compilation fails because TinkerBell is not a legal abstract class.

13. Given:

          2. public class Errrrr {
          3.   static String a = null;
          4.   static String s = "";
          5.   public static void main(String[] args) {
          6.     try {
          7.       a = args[0];
          8.       System.out.print(a);
          9.       s += "t1 ";
         10.   }
         11.   catch (RuntimeException re) { s += "c1 "; }
         12.   finally { s += "f1 "; }
         13.   System.out.println(" " + s);
         14. } }

And two command-line invocations:

        java Errrrr
        java Errrrr x

What is the result?

A. First: f1, then: x t1

B. First: f1, then: x t1 f1

C. First: c1, then: x t1

D. First: c1, then: x t1 f1

E. First: c1 f1, then: x t1

F. First: c1 f1, then: x t1 f1

G. Compilation fails.

14. Given:

         51. String s = "4.5x4.a.3";
         52. String[] tokens = s.split("\s");
         53. for(String o: tokens)
         54.   System.out.print(o + " ");
         55.
         56. System.out.print(" ");
         57. tokens = s.split("\..");
         58. for(String o: tokens)
         59.   System.out.print(o + " ");

What is the result?

A. 4 x4

B. 4 x4 .

C. 4 x4 3

D. 4 5x4 a 3 4 x4

E. 4.5x4.a.3 4 x4

F. 4.5x4.a.3 4 x4 .

15. Given:

          2. abstract class Tool {
          3.   int SKU;
          4.   abstract void getSKU();
          5. }
          6. public class Hammer {
          7.   // insert code here
          8. }

Which line(s), inserted independently at line 7, will compile? (Choose all that apply.)

A. void getSKU() { ; }

B. private void getSKU() { ; }

C. protected void getSKU() { ; }

D. public void getSKU() { ; }

16. Given:

          3. public class Stubborn implements Runnable {
          4.   static Thread t1;
          5.   static int x = 5;
          6.   public void run() {
          7.     if(Thread.currentThread().getId() == t1.getId()) shove();
          8.     else push();
          9.   }
         10.   static synchronized void push() { shove(); }
         11.   static void shove() {
         12.     synchronized(Stubborn.class) {
         13.     System.out.print(x-- + " ");
         14.     try { Thread.sleep(2000); } catch (Exception e) { ; }
         15.     if(x > 0) push();
         16.   } }
         17.   public static void main(String[] args) {
         18.     t1 = new Thread(new Stubborn());
         19.     t1.start();
         20.     new Thread(new Stubborn()).start();
         21. } }

Which are true? (Choose all that apply.)

A. Compilation fails.

B. The output is 5 4 3 2 1

C. The output is 5 4 3 2 1 0

D. The program could deadlock.

E. The output could be 5, followed by deadlock.

F. If the sleep() invocation was removed, the chance of deadlock would decrease.

G. As it stands, the program can’t deadlock, but if shove() was changed to synchronized, then the program could deadlock.

17. Given:

          2. class Super {
          3.   static String os = "";
          4.   void doStuff() { os += "super "; }
          5. }
          6. public class PolyTest extends Super {
          7.   public static void main(String[] args) { new PolyTest().go(); }
          8.     void go() {
          9.       Super s = new PolyTest(); 
         10.       PolyTest p = (PolyTest)s;

         11.       p.doStuff();
         12.       s.doStuff();
         13.       p.doPoly();
         14.       s.doPoly();
         15.       System.out.println(os);
         16.    }
         17.    void doStuff() { os += "over "; }
         18.    void doPoly() { os += "poly "; }
         19.  }

What is the result?

A. Compilation fails.

B. over over poly poly

C. over super poly poly

D. super super poly poly

E. An exception is thrown at runtime.

18. Given two files:

          1. package com.wickedlysmart;
          2. import com.wickedlysmart2.*;
          3. public class Launcher {
          4.   public static void main(String[] args) {
          5.     Utils u = new Utils();
          6.     u.do1();
          7.     u.do2();
          8.     u.do3();
          9. } }

and the correctly compiled and located:

          1. package com.wickedlysmart2;
          2. class Utils {
          3.   void do1() { System.out.print("do1 "); }
          4.   protected void do2() { System.out.print("do2 "); }
          5.   public void do3() { System.out.print("do3 "); }
          6. }

What is the result for Launcher? (Choose all that apply.)

A. do1 do2 do3

B. "do1 ", followed by an exception

C. Compilation fails due to an error on line 5 of Launcher.

D. Compilation fails due to an error on line 6 of Launcher.

E. Compilation fails due to an error on line 7 of Launcher.

F. Compilation fails due to an error on line 8 of Launcher.

19. Given:

         2. public class Wacky {
         3.   public static void main(String[] args) {
         4.     int x = 5;
         5.     int y = (x < 6) ? 7 : 8;
         6.     System.out.print(y + " ");
         7.     boolean b = (x < 6) ? false : true;
         8.     System.out.println(b + " ");
         9.     assert(x < 6) : "bob";
        10.     assert( ((x < 6) ? false : true) ) : "fred";
        11. } }

And, if the code compiles, the invocation:

        java -ea Wacky

Which will be contained in the output? (Choose all that apply.)

A. 7

B. 8

C. bob

D. fred

E. true

F. false

G. Compilation fails.

20. Given:

         3. class MotorVehicle {
         4.   protected int doStuff(int x) { return x * 2; }
         5. }
         6. class Bicycle {
         7.   void go(MotorVehicle m) {
         8.     System.out.print(m.doStuff(21) + " ");
         9. } }
        10. public class Beemer extends MotorVehicle {
        11.   public static void main(String[] args) {
        12.     System.out.print(new Beemer().doStuff(11) + " ");
        13.     new Bicycle().go(new Beemer());
        14.     new Bicycle().go(new MotorVehicle());
        15.   }
        16.   int doStuff(int x) { return x * 3; }
        17. }

What is the result? (Choose all that apply.)

A. 22 42 42

B. 22 63 63

C. 33 42 42

D. 33 63 42

E. Compilation fails.

F. An exception is thrown at runtime.

21. Which are true? (Choose all that apply.)

A. A single JAR file can contain only files from a single package.

B. A JAR file can be used only on the machine on which it was created.

C. When javac is using files within a JAR file, it will unJAR the file during compilation.

D. The Java SDK installation directory tree on a Java developer’s computer usually includes a subdirectory tree named jre/lib/ext.

E. A JAR file is structured so that all the files and directories that you’ve “JARed” will be subdirectory(ies) of the META-INF directory.

F. When javac is invoked with a classpath option, and you need to find files in a JAR file, the name of the JAR file must be included at the end of the path.

22. Given the proper import(s), and given:

         13. class NameCompare implements Comparator<Stuff> {
         14.   public int compare(Stuff a, Stuff b) {
         15.     return b.name.compareTo(a.name);
         16. } }
         18. class ValueCompare implements Comparator<Stuff> {
         19.   public int compare(Stuff a, Stuff b) {
         20.     return (a.value - b.value);
         21. } }

Which are true? (Choose all that apply.)

A. This code does not compile.

B. This code allows you to use instances of Stuff as keys in Maps.

C. These two classes properly implement the Comparator interface.

D. NameCompare allows you to sort a collection of Stuff instances alphabetically.

E. ValueCompare allows you to sort a collection of Stuff instances in ascending numeric order.

F. If you changed both occurrences of "compare()" to "compareTo()", the code would compile.

23. Given:

         3. public class Chopper {
         4.   String a = "12b";
         5.   public static void main(String[] args) {
         6.     System.out.println(new Chopper().chop(args[0]));
         7.   }
         8.   int chop(String a) {
         9.     if(a == null) throw new IllegalArgumentException();
        10.     return Integer.parseInt(a);
        11. } }

And, if the code compiles, the invocation:

        java Chopper

What is the result?

A. 12

B. 12b

C. Compilation fails.

D. A NullPointerException is thrown.

E. A NumberFormatException is thrown.

F. An IllegalArgumentException is thrown.

G. An ArrayIndexOutOfBoundsException is thrown.

24. Which, concerning command-line options, are true? (Choose all that apply.)

A. The -D flag is used in conjunction with a name-value pair.

B. The -D flag can be used with javac to set a system property.

C. The -d flag can be used with java to disable assertions.

D. The -d flag can be used with javac to specify where to place .class files.

E. The -d flag can be used with javac to document the locations of deprecated APIs in source files.

25. Given that "it, IT" is the locale code for Italy and that "pt, BR" is the locale code for Brazil, and given:

        51. Date d = new Date();
        52. DateFormat df = DateFormat.getDateInstance(DateFormat.FULL);
        53. Locale[] la = {new Locale("it", "IT"), new Locale("pt", "BR")}; 
        54. for(Locale l: la) {
        55.   df.setLocale(l);
        56.   System.out.println(df.format(d));
        57. }

Which are true? (Choose all that apply.)

A. An exception is thrown at runtime.

B. Compilation fails due to an error on line 53.

C. Compilation fails due to an error on line 55.

D. Compilation fails due to an error on line 56.

E. The output will contain only today’s date for the Italian locale.

F. The output will contain today’s date for both Italian and Brazilian locales.

26. Given this code in a method:

          4.    Integer[][] la = {{1,2}, {3,4,5}};
          5.    Number[] na = la[1];
          6.    Number[] na2 = (Number[])la[0];
          7.    Object o = na2;
          8.    la[1] = (Number[])o;
          9.    la[0] = (Integer[])o;

What is the result? (Choose all that apply.)

A. Compilation succeeds.

B. Compilation fails due to an error on line 4.

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.

H. Compilation succeeds but an exception is thrown at runtime.

27. Given:

          3. public class Ice {
          4.   Long[] stockings = {new Long(3L), new Long(4L), new Long(5L)};
          5.   static int count = 0;
          6.   public static void main(String[] args) {
          7.     new Ice().go();

          8.   System.out.println(count);
          9. }
         10. void go() {
         11.   for(short x = 0; x < 5; x++) {
         12.   if(x == 2) return;
         13.   for(long ell: stockings) {
         14.     count++;
         15.     if(ell == 4) break;
         16. } } } }

What is the result? (Choose all that apply.)

A. 2

B. 4

C. 8

D. 12

E. Compilation fails due to an error on line 13.

F. Compilation fails due to an error on line 16.

28. Given the proper import statement(s) and:

         4.     Console c = System.console();
         5.     char[] pw;
         6.     if(c == null) return;
         7.     pw = c.readPassword("%s", "pw: ");
         8.     System.out.println(c.readLine("%s", "input: "));

Which statements are true? (Choose all that apply.)

A. An exception will be thrown on line 8.

B. The variable pw must be a String, not a char[].

C. No import statements are necessary for the code to compile.

D. If the code compiles and is invoked, line 7 might never run.

E. Without a third argument, the readPassword() method will echo the password the user types.

F. If line 4 was replaced with the following code, the program would still compile: "Console c = new Console();".

29. Given that:

Exception is the superclass of IOException, and

IOException is the superclass of FileNotFoundException, and

          3. import java.io.*;
          4. class Physicist {
          5.   void think() throws IOException { }
          6. }
          7. public class Feynman extends Physicist {
          8.   public static void main(String[] args) {
          9.     new Feynman().think();
         10.   }
         11.   // insert method here
         12. }

Which of the following methods, inserted independently at line 11, compiles? (Choose all that apply.)

A. void think() throws Exception { }

B. void think() throws FileNotFoundException { }

C. public void think() { }

D. protected void think() throws IOException { }

E. private void think() throws IOException { }

F. void think() { int x = 7/0; }

30. Given:

           4. public class Zingseng extends Thread {
           5.   public static void main(String[] args) throws Exception {
           6.     Thread t1 = new Thread(new Zingseng());
           7.     t1.start();
           8.     // insert code here
           9.     for(int i = 0; i < 1000; i++) // Loop #1
          10.       System.out.print(Thread.currentThread().getName() + " ");
          11.  }
          12.  public void run() {
          13.    for(int i = 0; i < 1000; i++) // Loop #2
          14.      System.out.print(Thread.currentThread().getName() + " ");
          15. } }

Which code, inserted independently at line 8, will make most (or all) of Loop #2’s iterations run before most (or all) of Loop #1’s iterations? (Choose all that apply.)

A. // just a comment

B. t1.join();

C. t1.yield();

D. t1.sleep(1000);

31. Given:

          2. public class Kant extends Philosopher {
          3.   // insert code here
          5.   public static void main(String[] args) {
          6.     new Kant("Homer");
          7.     new Kant();
          8.   }
          9. }
         10. class Philosopher {
         11.   Philosopher(String s) { System.out.print(s + " "); }
         12. }

Which set(s) of code, inserted independently at line 3, produce the output "Homer Bart "? (Choose all that apply.)

A. Kant() { this("Bart"); }

Kant(String s) { super(s); }

B. Kant() { super("Bart"); }

Kant(String s) { super(s); }

C. Kant() { super(); }

Kant(String s) { super(s); }

D. Kant() { super("Bart"); }

Kant(String s) { this(); }

E. Kant() { super("Bart"); }

Kant(String s) { this("Homer"); }

32. Given:

          2. public class Epoch {
          3.   static int jurassic = 0;
          4.   public static void main(String[] args) {
          5.     assert(doStuff(5));
          6.   }

          7.   static boolean doStuff(int x) {
          8.     jurassic += x;
          9.     return true;
         10. } }

Which are true? (Choose all that apply.)

A. The code compiles using javac -ea Epoch.java

B. The assert statement on line 5 is used appropriately.

C. The code compiles using javac -source 1.3 Epoch.java

D. The code compiles using javac -source 1.4 Epoch.java

E. The code compiles using javac -source 1.6 Epoch.java

F. The code will not compile using any version of the javac compiler.

33. Given

         1. public class Kaput {
         2.   Kaput myK;
         3.   String degree = "0";
         4.   public static void main(String[] args) {
         5.     Kaput k1 = new Kaput();
         6.     go(k1);
         7.     System.out.println(k1.degree);
         8.   }
         9.   static Kaput go(Kaput k) {
        10.     final Kaput k1 = new Kaput();
        11.     k.myK = k1;
        12.     k.myK.degree = "7";
        13.     return k.myK;
        14. } }

What is the result?

A. 0

B. 7

C. null

D. Compilation fails.

E. An exception is thrown at runtime.

34. Given:

         3. class Holder {
         4.   enum Gas { ARGON, HELIUM };
         5. }
         6. public class Basket extends Holder {
         7.   public static void main(String[] args) {
         8.     short s = 7; long 1 = 9L; float f = 4.0f;
         9.     int i = 3; char c = 'c'; byte b = 5;
        10.     // insert code here
        11.      default: System.out.println("howdy");
        12. } } }

Which line(s) of code (if any), inserted independently at line 10, will compile? (Choose all that apply.)

A. switch (s) {

B. switch (l) {

C. switch (f) {

D. switch (i) {

E. switch (c) {

F. switch (b) {

G. switch (Gas.ARGON) {

H. The code will not compile due to additional error(s).

35. Given

          2. class Horse {
          3.   static String s = "";
          4.   void beBrisk() { s += "trot "; }
          5. }
          6. public class Andi extends Horse {
          7.   void beBrisk() { s += "tolt "; }
          8.   public static void main(String[] args) {
          9.     Horse x0 = new Horse();
         10.     Horse x1 = new Andi(); x1.beBrisk();
         11.     Andi x2 = (Andi)x1; x2.beBrisk(); 
         12.     Andi x3 = (Andi)x0; x3.beBrisk();
         13.     System.out.println(s);
         14. } }

What is the result?

A. tolt tolt tolt

B. trot tolt trot

C. trot tolt tolt

D. Compilation fails.

E. An exception is thrown at runtime.

36. Given:

         3. class Tire {
         4.   private static int x = 6;
         5.   public static class Wheel {
         6.     void go() { System.out.print("roll " + x++); }
         7. } }
         8. public class Car {
         9.   public static void main(String[] args) {
        10.   // insert code here
        11. } }

And the three code fragments:

I. new Tire.Wheel().go();

II. Tire t = new Tire(); t.Wheel().go();

III. Tire.Wheel w = new Tire.Wheel(); w.go();

Assuming we insert a single fragment at line 10, which are true? (Choose all that apply.)

A. Once compiled, the output will be "roll 6"

B. Once compiled, the output will be "roll 7"

C. Fragment I, inserted at line 10, will compile.

D. Fragment II, inserted at line 10, will compile.

E. Fragment III, inserted at line 10, will compile.

F. Taken separately, class Tire will not compile on its own.

37. Given:

          3. public class Fortran {
          4.   static int bump(int i) { return i + 2; }
          5.   public static void main(String[] args) {
          6.     for(int x = 0; x < 5; bump(x))
          7.       System.out.print(x + " ");
          8. } }

What is the result?

A. 2 4

B. 0 2 4

C. 2 4 6

D. 0 2 4 6

E. Compilation fails.

F. Some other result occurs.

38. You need the three classes and/or interfaces to work together to produce the output "Kara Charis". Fill in the blanks using the following fragments so the correct is-a and has-a relationships are established to produce this output. Note: You may not need to fill in every empty blank space, but a blank space can hold only one fragment. Also note that not all the fragments will be used, and that fragments CAN be used more than once.

Code to complete:

Image

Fragments:

Image

39. Given:

          1. import java.util.*;
          2. public class PirateTalk {
          3.   public static void main(String... arrrrgs) {
          4.     Properties p = System.getProperties();
          5.     p.setProperty("pirate", "scurvy");
          6.     String s = p.getProperty("argProp") + " ";
          7.     s += p.getProperty("pirate");
          8.     System.out.println(s);
          9. } }

And the command-line invocation:

         java PirateTalk -DargProp="dog,"

What is the result?

A. dog, scurvy

B. null scurvy

C. scurvy dog,

D. scurvy null

E. Compilation fails.

F. An exception is thrown at runtime.

G. An “unrecognized option” command-line error is thrown.

40. Given:

          2. import java.util.*;
          3. public class VLA implements Comparator<VLA> {
          4.   int dishSize;
          5.   public static void main(String[] args) {
          6.     VLA[] va = {new VLA(40), new VLA(200), new VLA(60)};
          7.
          8.     for(VLA v: va) System.out.print(v.dishSize + " ");
          9.     int index = Arrays.binarySearch(va, new VLA(60), va[0]);
         10.     System.out.print(index + " ");
         11.     Arrays.sort(va);
         12.     for(VLA v: va) System.out.print(v.dishSize + " ");
         13.     index = Arrays.binarySearch(va, new VLA(60), va[0]);
         14.     System.out.println(index);
         15.   }
         16.   public int compare(VLA a, VLA b) {
         17.     return a.dishSize - b.dishSize;
         18.   }
         19.   VLA(int d) { dishSize = d; }
         20. }

Which result is most likely?

A. Compilation fails.

B. "40 200 60 2 40 60 200 1"

C. "40 200 60 -2 40 60 200 1"

D. "40 200 60", followed by an exception.

E. "40 200 60 -2", followed by an exception.

41. Given:

          2. class RainCatcher {
          3.   static StringBuffer s;
          4.   public static void main(String[] args) {
          5.     Integer i = new Integer(42);
          6.     for(int j = 40; j < i; i--)
          7.       switch(i) {
          8.         case 41: s.append("41 ");
          9.         default: s.append("def ");
         10.       case 42: s.append("42 ");
         11.      }
         12.    System.out.println(s);
         13. } }

What is the result?

A. 41 def

B. 41 def 42

C. 42 41 def 42

D. An exception is thrown at runtime.

E. Compilation fails due to an error on line 6.

F. Compilation fails due to an error on line 7.

42. Given:

          2. import java.util.*;
          3. class Snack {
          4.   static List<String> s1 = new ArrayList<String>();
          5. }
          6. public class Chips extends Snack {
          7.   public static void main(String[] args) {
          8.     List c1 = new ArrayList();
          9.     s1.add("1"); s1.add("2");
         10.     c1.add("3"); c1.add("4");
         11.     getStuff(s1, c1);
         12.   }
         13.   static void getStuff(List<String> a1, List a2) {
         14.     for(String s1: a1) System.out.print(s1 + " ");
         15.     for(String s2: a2) System.out.print(s2 + " ");
         16. } }

What is the result? (Choose all that apply.)

A. "1 2 3 4"

B. "1 2", followed by an exception

C. An exception is thrown with no other output.

D. Compilation fails due to an error on line 13.

E. Compilation fails due to an error on line 14.

F. Compilation fails due to an error on line 15.

43. Given that Calendar.MONTH starts with January == 0, and given:

          3. import java.util.*;
          4. public class Wise {
          5.   public static void main(String[] args) {
          6.     Calendar c = Calendar.getInstance();
          7.     c.set(1999,11,25);
          8.     c.roll(Calendar.MONTH, 3);
          9.     c.add(Calendar.DATE, 10);
         10.     System.out.println(c.getTime());
         11. } }

And, if the program compiles, what date is represented in the output?

A. March 4, 1999

B. April 4, 1999

C. March 4, 2000

D. April 4, 2000

E. Compilation fails.

F. An exception is thrown at runtime.

44. Given the following two files containing Light.java and Dark.java:

          2. package ec.ram;
          3. public class Light{}
          4. class Burn{}

          2. package ec.ram;
          3. public class Dark{}
          4. class Melt{}

And if those files are located in the following directory structure:

          $ROOT
             |-- Light.java
             |-- Dark.java
             |-- checker
                     |-- dira
                     |-- dirb

And the following commands are executed, in order, from the ROOT directory:

          javac Light.java -cp checker/dira -d checker/dirb
          javac Dark.java -cp checker/dirb -d checker/dira
          jar -cf checker/dira/a.jar checker

A new JAR file is created after executing the above commands. Which of the following files will exist inside that JAR file? (Choose all that apply.)

A. [JAR]/dira/ec/ram/Melt.class

B. [JAR]/checker/dirb/ec/ram/Burn.class

C. [JAR]/dirb/ec/ram/Melt.class

D. [JAR]/checker/dira/ec/ram/Burn.class

E. [JAR]/dira/a.jar

F. [JAR]/checker/dira/a.jar

45. Given:

         42. String s1 = " ";
         43. StringBuffer s2 = new StringBuffer(" ");
         44. StringBuilder s3 = new StringBuilder(" ");
         45. for(int i = 0; i < 1000; i++) // Loop #1
         46.   s1 = s1.concat("a");
         47. for(int i = 0; i < 1000; i++) // Loop #2
         48.   s2.append("a");
         49. for(int i = 0; i < 1000; i++) // Loop #3
         50.   s3.append("a");

Which statements will typically be true? (Choose all that apply.)

A. Compilation fails.

B. Loop #3 will tend to execute faster than Loop #2.

C. Loop #1 will tend to use less memory than the other two loops.

D. Loop #1 will tend to use more memory than the other two loops.

E. All three loops will tend to use about the same amount of memory.

F. In order for this code to compile, the java.util package is required.

G. If multiple threads need to use an object, the s2 object should be safer than the s3 object.

46. Given:

          2. public class Payroll {
          3.   int salary;
          4.   int getSalary() { return salary; }
          5.   void setSalary(int s) {
          6.     assert(s > 30000);
          7.     salary = s;
          8. } }

Which are true? (Choose all that apply.)

A. Compilation fails.

B. The class is well encapsulated as it stands.

C. Removing line 6 would weaken the class’s degree of cohesion.

D. Removing line 6 would weaken the class’s degree of encapsulation.

E. If the salary variable was private, the class would be well encapsulated.

F. Removing line 6 would make the class’s use of assertions more appropriate.

47. Given:

          1. class GardenTool {
          2.   static String s = "";
          3.   String name = "Tool ";

          4.   GardenTool(String arg) { this(); s += name; }
          5.   GardenTool() { s += "gt "; }
          6. }
          7. public class Rake extends GardenTool {
          8.   { name = "Rake "; }
          9.   Rake(String arg) { s += name; }
         10.   public static void main(String[] args) {
         11.     new GardenTool("hey ");
         12.     new Rake("hi ");
         13.     System.out.println(s);
         14.   }
         15.   { name = "myRake "; }
         16. }

What is the result?

A. Tool Rake

B. Tool myRake

C. gt Tool Rake

D. gt Tool myRake

E. gt Tool gt Rake

F. gt Tool gt myRake

G. gt Tool gt Tool myRake

H. Compilation fails.

48. Given:

          3. public class Race {
          4.   public static void main(String[] args) {
          5.     Horse h = new Horse();

          6.     Thread t1 = new Thread(h, "Andi");
          7.     Thread t2 = new Thread(h, "Eyra");
          8.     new Race().go(t2);
          9.     t1.start();
          0.     t2.start();
         11.   }
         12.   void go(Thread t) { t.start(); }
         13. }
         14. class Horse implements Runnable {
         15.   public void run() {
         16.     System.out.print(Thread.currentThread().getName() + " ");
         17. } }

What is the result? (Choose all that apply.)

A. Compilation fails.

B. No output is produced.

C. The output could be: "Andi Eyra"

D. The output could be: "Eyra Andi Eyra"

E. The output could be: "Eyra", followed by an exception.

F. The output could be: "Eyra Andi", followed by an exception.

49. Given the proper import statement(s) and given:

          4.   Map<String, String> h = new Hashtable<String, String>();
          5.   String[] k = {"1", "2", "3", null};
          6.   String[] v = {"a", "b", null, "d"};
          7.
          8.   for(int i=0; i<4; i++) {
          9.     h.put(k[i], v[i]);
         10.     System.out.print(h.get(k[i]) + " ");
         11.   }
         12.   System.out.print(h.size() + " " + h.values() + " ");

What result is most likely?

A. Compilation fails.

B. "a b d 3 [b, d, a]"

C. "a b null d 3 [b, d, a]"

D. "a b null d 4 [b, d, null, a]"

E. "a b", followed by an exception.

F. "a b null", followed by an exception.

50. Given:

          3. public class Fiji {
          4.   static Fiji base;
          5.   Fiji f;
          6.   public static void main(String[] args) {
          7.     new Fiji().go();
          8.     // do more stuff
          9.   }
         10.   void go() {
         11.     Fiji f1 = new Fiji();
         12.     base = f1;
         13.     Fiji f2 = new Fiji();
         14.     f1.f = f2;
         15.     Fiji f3 = f1.f;
         16.     f2.f = f1;
         17.     base = null; f1 = null; f2 = null;
         18.    // do stuff
         19. } }

Which are true? (Choose all that apply.)

A. At line 8, one object is eligible for garbage collection.

B. At line 8, two objects are eligible for garbage collection.

C. At line 8, three objects are eligible for garbage collection.

D. At line 18, 0 objects are eligible for garbage collection.

E. At line 18, two objects are eligible for garbage collection.

F. At line 18, three objects are eligible for garbage collection.

51. Your company makes compute-intensive, 3D rendering software for the movie industry. Your chief scientist has just discovered a new algorithm for several key methods in a commonly used utility class. The new algorithm will decrease processing time by 15 percent, without having to change any method signatures. After you change these key methods, and in the course of rigorous system testing, you discover that the changes have introduced no new bugs into the software.

In terms of your software’s overall design, which are probably true? (Choose all that apply.)

A. Your software is well encapsulated.

B. Your software demonstrated low cohesion.

C. Your software demonstrated high cohesion.

D. Your software demonstrated loose coupling.

E. Your software demonstrated tight coupling.

52. Which are true about the classes and interfaces in java.util? (Choose all that apply.)

A. LinkedHashSet is-a Collection

B. Vector is-a List

C. LinkedList is-a Queue

D. LinkedHashMap is-a Collection

E. TreeMap is-a SortedMap

F. Queue is-a Collection

G. Hashtable is-a Set

53. Given that File.createNewFile() throws IOException, and that FileNotFoundException extends IOException, use the following fragments to fill in the blanks so that the code compiles. You must use all the fragments, and each fragment can be used only once. Note: As in the real exam, some drag-and-drop style questions might have several correct answers. You will receive full credit for ANY one of the correct answers.

Image

Fragments:

Image

54. Given:

          2. class Grandfather {
          3.   static String name = "gf ";
          4.   String doStuff() { return "grandf "; }
          5. }
          6. class Father extends Grandfather {
          7.   static String name = "fa ";
          8.   String doStuff() { return "father "; }
          9. }
         10. public class Child extends Father {
         11.   static String name = "ch ";
         12.   String doStuff() { return "child "; }
         13.   public static void main(String[] args) {
         14.     Father f = new Father();
         15.     System.out.print(((Grandfather)f).name
                                 + ((Grandfather)f).doStuff()) ;
         16.     Child c = new Child();
         17.     System.out.println(((Grandfather)c).name
                                 + ((Grandfather)c).doStuff() + ((Father)c).doStuff());
         18. } }

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset