D. Somewhere in the code, a second instance of Piston must be instantiated.

E. There are errors in the code that must be fixed, outside of lines 4, 12, and 13.

31. Given:

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

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 Thread-1 Thread-3 Thread-3

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

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

32. Given:

          3. class Department {
          4.   Department getDeptName() { return new Department(); }
          5. }
          6. class Accounting extends Department {
          7.   Accounting getDeptName() { return new Accounting(); }
          8.   // insert code here
         13. }

And the following four code fragments:

I. String getDeptName(int x) { return "mktg"; }

II. void getDeptName(Department d) { ; }

III. void getDeptName(long x) throws NullPointerException {

throw new NullPointerException();

}

IV. Department getDeptName() throws NullPointerException {

throw new NullPointerException();

return new Department();

}

Which are true? (Choose all that apply.)

A. If fragment I is inserted at line 8, the code compiles.

B. If fragment II is inserted at line 8, the code compiles.

C. If fragment III is inserted at line 8, the code compiles.

D. If fragment IV is inserted at line 8, the code compiles.

E. If none of the fragments are inserted at line 8, the code compiles.

33. Given:

          2. import java.util.*;
          3. interface Canine { }
          4. class Dog implements Canine { }
          5. public class Collie extends Dog {
          6.   public static void main(String[] args) {
          7.     List<Dog> d = new ArrayList<Dog>();
          8.     List<Collie> c = new ArrayList<Collie>();
          9.     d.add(new Collie());
         10.     c.add(new Collie());
         11.     do1(d); do1(c);
         12.     do2(d); do2(c);
         13.   }
         14.   static void do1(List<? extends Dog> d2) {
         15.     d2.add(new Collie());
         16.     System.out.print(d2.size());
         17.   }
         18.   static void do2(List<? extends Canine> c2) { }
         19. }

Which are true? (Choose all that apply.)

A. Compilation succeeds.

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

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

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

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

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

G. Compilation fails due to errors on lines 11 and 12.

34. Given:

         42. void go() {
         43.   int cows = 0;
         44.   int[] twisters = {1,2,3};
         45.   for(int i = 0; i < 4; i++)
         46.     switch(twisters[i]) {
         47.        case 2: cows++;
         48.        case 1: cows += 10;
         49.        case 0: go();
         50.     }
         51.   System.out.println(cows);
         52. }

What is the result?

A. 11

B. 21

C. 22

D. Compilation fails.

E. A StackOverflowError is thrown at runtime.

F. An ArrayIndexOutOfBoundsException is thrown at runtime.

35. Given:

          2. class Robot { }
          3. interface Animal { }
          4. class Feline implements Animal { }
          5. public class BarnCat extends Feline {
          6.   public static void main(String[] args) {
          7.     Animal af = new Feline();
          8.     Feline ff = new Feline();
          9.     BarnCat b = new BarnCat();
         10.     Robot r = new Robot();
         11.     if(af instanceof Animal) System.out.print("1 ");
         12.     if(af instanceof BarnCat) System.out.print("2 ");
         13.     if(b instanceof Animal) System.out.print("3 ");
         14.     if(ff instanceof BarnCat) System.out.print("4 ");
         15.     if(r instanceof Animal) System.out.print("5 ");
         16.   }
         17. }

What is the result?

A. 1

B. 1 3

C. 1 2 3

D. 1 3 4

E. 1 2 3 4

F. Compilation fails.

G. An exception is thrown at runtime.

36. Given:

          2. public class Sunny extends Weather {
          3.   public static void main(String[] args) {
          4.     try {
          5.       new Sunny().do1();
          6.       new Sunny().do2();
          7.       new Sunny().do3();
          8.     }
          9.     catch(Throwable t) { System.out.print("exc "); }
         10. } }
         11. class Weather {
         12.   void do1() { System.out.print("do1 "); }
         13.   private void do2() { System.out.print("do2 "); }
         14.   protected void do3() { System.out.print("do3 "); }
         15. }

What is the result?

A. do1 exc

B. do1 do2 exc

C. do1 do2 do3

D. Compilation fails.

E. An exception is thrown at runtime.

37. Given that FileNotFoundException extends IOException and given:

          2. import java.io.*;
          3. public class Changeup {
          4.   public static void main(String[] args) throws IOException {
          5.     new Changeup().go();
          6.     new Changeup().go2();
          7.     new Changeup().go3();
          8.   }
          9.   void go() { throw new IllegalArgumentException(); }
         10.
         11.   void go2() throws FileNotFoundException { }
         12.
         13.   void go3() {
         14.     try { throw new Exception(); }
         15.     catch (Throwable th) { throw new NullPointerException(); }
         16. } }

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

A. An IOException is thrown at runtime.

B. A NullPointerException is thrown at runtime.

C. An IllegalArgumentException is thrown at runtime.

D. Compilation fails due to an error at line 4.

E. Compilation fails due to an error at line 9.

F. Compilation fails due to an error at line 11.

G. Compilation fails due to an error at line 15.

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

A. If class A is-a class B, then class A cannot be considered well encapsulated.

B. If class A has-a class B, then class A cannot be considered well encapsulated.

C. If class A is-a class B, then the two classes are said to be cohesive.

D. If class A has-a class B, then the two classes are said to be cohesive.

E. If class A is-a class B, it’s possible for them to still be loosely coupled.

F. If class A has-a class B, it’s possible for them to still be loosely coupled.

39. Given:

          2. import java.util.*;
          3. public class Volleyball {
          4.   public static void main(String[] args) {
          5.     TreeSet<String> s = new TreeSet<String>();
          6.     s.add("a"); s.add("f"); s.add("b");
          7.     System.out.print(s + " ");
          8.     Collections.reverse(s);
          9.     System.out.println(s);
         10. } }

What is the result?

A. Compilation fails.

B. [a, b, f] [a, b, f]

C. [a, b, f] [f, b, a]

D. [a, f, b] [b, f, a]

E. [a, b, f], followed by an exception.

F. [a, f, b], followed by an exception.

40. Given:

          2. public class Boggy {
          3.   final static int mine = 7;
          4.   final static Integer i = 57;
          5.   public static void main(String[] args)

          6.     int x = go(mine);
          7.     System.out.print(mine + " " + x + " ");
          8.     x += mine;
          9.     Integer i2 = i;
         10.     i2 = go(i);
         11.     System.out.println(x + " " + i2);
         12.     i2 = new Integer(60);
         13.   }
         14.   static int go(int x) { return ++x; }
         15. }

What is the result?

A. 7 7 14 57

B. 7 8 14 57

C. 7 8 15 57

D. 7 8 15 58

E. 7 8 16 58

F. Compilation fails.

G. An exception is thrown at runtime.

41. Given:

          3. import java.io.*;
          4. public class Kesey {
          5.   public static void main(String[] args) throws Exception {
          6.     File file = new File("bigData.txt");
          7.     FileWriter w = new FileWriter(file);
          8.     w.println("lots o′ data");
          9.     w.flush();
         10.     w.close();
         11. } }

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

A. An empty file named "bigData.txt" is created.

B. Compilation fails due only to an error on line 5.

C. Compilation fails due only to an error on line 6.

D. Compilation fails due only to an error on line 7.

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

F. Compilation fails due to errors on multiple lines.

G. A file named "bigData.txt" is created, containing one line of data.

42. Given:

         3. class Wanderer implements Runnable {
         4.   public void run() {
         5.     for(int i = 0; i < 2; i++)
         6.       System.out.print(Thread.currentThread().getName() + " ");
         7. } }
         8. public class Wander {
         9.   public static void main(String[] args) {
        10.     Wanderer w = new Wanderer();
        11.     Thread t1 = new Thread();
        12.     Thread t2 = new Thread(w);
        13.     Thread t3 = new Thread(w, "fred");
        14.     t1.start(); t2.start(); t3.start();
        15. } }

Which are true? (Choose all that apply.)

A. Compilation fails.

B. No output is produced.

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

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

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

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

43. Given:

         2. import java.util.*;
         3. public class MyFriends {
         4.   String name;
         5.   MyFriends(String s) { name = s; }
         6.   public static void main(String[] args) {
         7.     Set<MyFriends> ms = new HashSet<MyFriends>();
         8.     ms.add(new MyFriends("Bob"));
         9.     System.out.print(ms + " ");
        10.     ms.add(new MyFriends("Bob"));
        11.     System.out.print(ms + " ");
        12.     ms.add(new MyFriends("Eden"));
        13.     System.out.print(ms + " ");
        14.   }
        15.   public String toString() { return name; }
        16. }

What is the most likely result?

A. Compilation fails.

B. [Bob] [Bob] [Eden, Bob]

C. [Bob] [Bob] [Eden, Bob, Bob]

D. [Bob], followed by an exception.

E. [Bob] [Bob, Bob] [Eden, Bob, Bob]

44. Given the proper imports, and given:

         17. public void go() {
         18.   NumberFormat nf, nf2;
         19.   Number n;
         20.   Locale[] la = NumberFormat.getAvailableLocales();
         21.   for(int x=0; x < 10; x++) {
         22.     nf = NumberFormat.getCurrencyInstance(la[x]);
         23.     System.out.println(nf.format(123.456f));
         24.   }
         25.   nf2 = NumberFormat.getInstance();
         26.   n = nf2.parse("123.456f");
         27.   System.out.println(n);
         28. }

Given that line 20 is legal, which are true? (Choose all that apply.)

A. Compilation fails.

B. An exception is thrown at runtime.

C. The output could contain "123.46"

D. The output could contain "123.456"

E. The output could contain "$123.46"

45. Given:

         59. Integer i1 = 2001; // set 1
         60. Integer i2 = 2001;
         61. System.out.println((i1 == i2) + " " + i1.equals(i2)); // output 1
         62. Integer i3 = 21; // set 2
         63. Integer i4 = new Integer(21);
         64. System.out.println((i3 == i4) + " " + i3.equals(i4)); // output 2
         65. Integer i5 = 21; // set 3
         66. Integer i6 = 21;
         67. System.out.println((i5 == i6) + " " + i5.equals(i6)); // output 3

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

A. Compilation fails.

B. An exception is thrown at runtime.

C. All three sets of output will be the same.

D. The last two sets of output will be the same.

E. The first two sets of output will be the same.

F. The first and last sets of output will be the same.

46. Given:

          2. public class Skip {
          3.   public static void main(String[] args) throws Exception {
          4.     Thread t1 = new Thread(new Jump());
          5.     Thread t2 = new Thread(new Jump());
          6.     t1.start(); t2.start();
          7.     t1.join(500);
          8.     new Jump().run();
          9. } }
         10. class Jump implements Runnable {
         11.   public void run() {
         12.     for(int i = 0; i < 5; i++) {
         13.       try { Thread.sleep(200); }
         14.       catch (Exception e) { System.out.print("e "); }
         15.       System.out.print(Thread.currentThread().getId() + "-" + i + " ");
         16. } } }

What is the result?

A. Compilation fails.

B. The main thread will run mostly before t1 runs.

C. The main thread will run after t1, but together with t2.

D. The main thread will run after t2, but together with t1.

E. The main thread will run after both t1 and t2 are mostly done.

F. The main thread’s execution will overlap with t1 and t2’s execution.

47. Given:

          1. import java.util.*;
          2. public class Piles {
          3.   public static void main(String[] args) {
          4.     TreeMap<String, String> tm = new TreeMap<String, String>();
          5.     TreeSet<String> ts = new TreeSet<String>();
          6.     String[] k = {"1", "b", "4", "3"};
          7.     String[] v = {"a", "d", "3", "b"};
          8.     for(int i=0; i<4; i++) {
          9.       tm.put(k[i], v[i]);
         10.       ts.add(v[i]);
         11.     }
         12.     System.out.print(tm.values() + " ");
         13.     Iterator it2 = ts.iterator();
         14.     while(it2.hasNext()) System.out.print(it2.next() + "-");
         15. } }

Which of the following could be a part of the output? (Choose two.)

A. [a, b, 3, d]

B. [d, a, b, 3]

C. [3, a, b, d]

D. [a, b, d, 3]

E. [1, 3, 4, b]

F. [b, 1, 3, 4]

G. 3-a-b-d-

H. a-b-d-3-

I. a-d-3-b-

48. Given this code in a method:

         5.   String s = "dogs. with words.";
         6.   // insert code here
         7.   for(String o: output)
         8.     System.out.print(o + " ");

Which of the following, inserted independently at line 6, will produce output that contains the String "dogs"? (Choose all that apply.)

A. String[] output = s.split("s");

B. String[] output = s.split("d");

C. String[] output = s.split("\d");

D. String[] output = s.split("\s");

E. String[] output = s.split("\w");

F. String[] output = s.split("\.");

49. Given the design implied by this partially implemented class:

         2. public class RobotDog {
         3.   int size;
         4.   void bark() { /* do barking */ }
         5.   int getSize() { return size; }
         6.   { size = 16; }
         7.   int getNetworkPrinterID() {
         8.     /* do lookup */
         9.     return 37;
        10.   }
        11.   void printRobotDogStuff(int printerID) { /* print RobotDog stuff */ }
        12. }

Which are true? (Choose all that apply.)

A. Compilation fails.

B. To improve cohesion, the size variable should be declared private.

C. To improve cohesion, the initialization block should be placed inside a constructor.

D. To improve cohesion, printRobotDogStuff() should be moved to a different class.

E. To improve cohesion, getNetworkPrinterID() should be moved to a different class.

50. Given:

          2. import java.util.*;
          3. public class Foggy extends Murky {
          4.   public static void main(String[] args) {
          5.     final List<String> s = new ArrayList<String>();
          6.     s.add("a"); s.add("f"); s.add("a");
          7.     new Foggy().mutate(s);
          8.     System.out.println(s);
          9.   }
         10.   List<String> mutate(List<String> s) {
         11.     List<String> ms = s;
         12.     ms.add("c");
         13.     return s;
         14.   }
         15. }
         16. class Murky {
         17.   final void mutate(Set s) { }
         18. }

What is the most likely result?

A. [a, f]

B. [a, f, a]

C. [a, f, c]

D. [a, f, a, c]

E. Compilation fails.

F. An exception is thrown at runtime.

51. Given:

          1. import java.util.*;
          2. enum Heroes { GANDALF, HANS, ENDER }
          3. public class MyStuff {
          4.   public static void main(String[] args) {
          5.     List<String> stuff = new ArrayList<String>();
          6.     stuff.add("Bob"); stuff.add("Fred");
          7.     new MyStuff().go();
          8.   }
          9.   Heroes myH = Heroes.ENDER;
         10.   void go() {
         11.     for(Heroes h: Heroes.values())
         12.     if(h == myH) System.out.println(myH);
         13. } }

Which are true? (Choose all that apply.)

A. Compilation fails.

B. main() has-a List

C. MyStuff has-a List

D. MyStuff has-a Heroes

E. The output is "ENDER"

F. The output is "Heroes.ENDER"

G. An exception is thrown at runtime.

52. Given:

          2. public class Pregnant extends Thread {
          3.   int x = 0;
          4.   public static void main(String[] args) {
          5.     Runnable r1 = new Pregnant();
          6.     new Thread(r1).start();
          7.     new Thread(r1).start();
          8.   }
          9.   public void run() {
         10.     for(int j = 0; j < 3; j++) {
         11.       x = x + 1;
         12.       x = x + 10;
         13.       System.out.println(x + " ");
         14.       x = x + 100;
         15. } } }

If the code compiles, which value(s) could appear in the output? (Choose all that apply.)

A. 12

B. 22

C. 122

D. 233

E. 244

F. 566

G. Compilation fails.

53. Fill in the blanks using the following fragments, so that the code compiles and the invocation "java Enchilada green 4" produces the output "wow". Note: You might not need to fill in all of the blanks, you won’t use all of the fragments, and each fragment can be used more than once.

Code:

       import java.util.*;
       public class Enchilada {
         public static void main(String[] args) {
           Map<Chilis, String> m = new HashMap<Chilis, String>();
           Chilis myC = new Chilis("green", 4);
           ________(new Chilis("red", 4), "4 alarm");
           ________(new Chilis("green", 2), "mild");
           ________(myC, "wow");
           Chilis c = new Chilis(_______, ___________________(________));
           System.out.println(_____________);
       } }
       class Chilis {
         Chilis(String c, int h) { color = c; hotness = h; }
         String color;
         private int hotness;
         public ___________ equals(___________) {
           __________________________
           if(__________________ __ ____________________) return _____;
           return __________;
         }
         public _____________ hashCode() { return ____________; }
       }

Fragments:

Image

54. Given:

          2. public class Toolbox {
          3.   static Toolbox st;
          4.   public static void main(String[] args) {
          5.     new Toolbox().go();
          6.     // what′s eligible?
          7.   }
          8.   void go() {
          9.     MyInner in = new MyInner();
         10.     Integer i3 = in.doInner();
         11.     Toolbox t = new Toolbox();
         12.     st = t;
         13.     System.out.println(i3);
         14.   }
         15.   class MyInner {
         16.     public Integer doInner() { return new Integer(34); }
         17.   }
         18. }

When the code reaches line 6, which are eligible for garbage collection? (Choose all that apply.)

A. st

B. in

C. i3

D. The object created on line 5.

E. The object created on line 9.

F. The object created on line 10.

G. The object created on line 11.

55. Given:

         2. class Ball {
         3.   static String s = "";
         4.   void doStuff() { s += "bounce "; }
         5. }
         6. class Basketball extends Ball {
         7.   void doStuff() { s += "swish "; }
         8. }
         9. public class Golfball extends Ball {
        10.   public static void main(String[] args) {
        11.     Ball b = new Golfball();
        12.     Basketball bb = (Basketball)b;
        13.     b.doStuff();
        14.     bb.doStuff();
        15.     System.out.println(s);
        16.   }
        17.   void doStuff() { s += "fore "; }
        18. }

What is the result?

A. fore fore

B. fore swish

C. bounce swish

D. bounce bounce

E. Compilation fails.

F. An exception is thrown at runtime.

56. Given the following three files:

          2. package apollo;
          3. import apollo.modules.Lunar;
          4. public class Saturn {
          5.   public static void main(String[] args){
          6.     Lunar lunarModule = new Lunar();
          7.     System.out.println(lunarModule);
          8. } }

          2. package apollo.modules;
          3. public interface Module { /* more code */ }

          2. package apollo.modules;
          3. public class Lunar implements Module { /* more code */ }

And given that Module.java and Lunar.java were successfully compiled and the directory structure is shown below:

Image

Which are correct about compiling and running the Saturn class from the $ROOT directory? (Choose all that apply.)

A. The command for compiling is javac -d . -cp . Saturn.java

B. The command for compiling is javac -d . -cp controls.jar Saturn.java

C. The command for compiling is javac -d . -cp .:controls.jar Saturn.java

D. The command for running is java -cp . apollo.Saturn

E. The command for running is java -cp controls.jar apollo.Saturn

F. The command for running is java -cp .:controls.jar apollo.Saturn

G. The command for running is java -cp controls.jar -cp . apollo.Saturn

57. Given:

          5. class OOthing { void doStuff() { System.out.print("oo "); } }
          6. class GuiThing extends OOthing {
          7.   void doStuff() { System.out.print("gui "); }
          8. }
          9. public class Button extends GuiThing {
         10.   void doStuff() { System.out.print("button "); }
         11.   public static void main(String[] args) { new Button().go(); }
         12.   void go() {
         13.     GuiThing g = new GuiThing();
         14.     // this.doStuff();
         15.     // super.doStuff();
         16.     // g.super.doStuff();
         17.     // super.g.doStuff();
         18.    // super.super.doStuff();
         19. } }

If the commented lines are uncommented independently, which are true? (Choose all that apply.)

A. If line 14 is uncommented, "button" will be in the output.

B. If line 15 is uncommented, "gui" will be in the output.

C. If line 16 is uncommented, "oo" will be in the output.

D. If line 17 is uncommented, "oo" will be in the output.

E. If line 18 is uncommented, "oo" will be in the output.

58. Given:

          2. import java.util.*;
          3. public class Salt {
          4.   public static void main(String[] args) {
          5.     Set s1 = new HashSet();
          6.     s1.add(0);
          7.     s1.add("1");
          8.     doStuff(s1);
          9.   }
         10.   static void doStuff(Set<Number> s) {
         11.     do2(s);
         12.     Iterator i = s.iterator();
         13.     while(i.hasNext()) System.out.print(i.next() + " ");
         14.     Object[] oa = s.toArray();
         15.     for(int x = 0; x < oa.length; x++)
         16.       System.out.print(oa[x] + " ");
         17.     System.out.println(s.contains(1));
         18.   }
         19.   static void do2(Set s2) { System.out.print(s2.size() + " "); }
         20. }

What is the most likely result?

A. 2 0 1 0 1 true

B. 2 0 1 0 1 false

C. Compilation fails.

D. An exception is thrown at line 8.

E. An exception is thrown at line 13.

F. An exception is thrown at line 14.

G. An exception is thrown at line 19.

59. Given:

         1. public class Begin {
         2.   static int x;
         3.   { int[] ia2 = {4,5,6}; }
         4.   static {
         5.     int[] ia = {1,2,3};
         6.     for(int i = 0; i < 3; i++)
         7.       System.out.print(ia[i] + " ");
         8.     x = 7;
         9.     System.out.print(x + " ");
        10. } }

And, if the code compiles, the invocation:

        java Begin

What is the result?

A. Compilation fails.

B. "1 2 3 7", with no exception thrown.

C. "1 2 3 7", followed by an exception.

D. "1 2 3", followed by an ExceptionInInitializerError

E. ExceptionInInitializerError is thrown before any output.

F. Some other exception is thrown before any other output.

60. Given:

          2. public class Alamo {
          3.   public static void main(String[] args) {
          4.     try {
          5.       assert(!args[0].equals("x")): "kate";
          6.     } catch(Error e) { System.out.print("ae "); }
          7.     finally {
          8.       try {
          9.         assert(!args[0].equals("y")): "jane";
         10.       } catch(Exception e2) { System.out.print("ae2 "); }
         11.       finally {
         12.         throw new IllegalArgumentException();
         13. } } } }

And, if the code compiles, the invocation:

        java -ea Alamo y

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

A. ae

B. ae2

C. kate

D. jane

E. AssertionError

F. IllegalArgumentException

G. There is no output because compilation fails.

Analyzing Your Results

It’s hard to know for sure, but our feeling is that the exam you just finished might be a bit easier than Exam 2. Because of that, the recommended plans listed in Table 6-1 might seem a little harsher. Again, a passing score on the OCP Java SE Programmer exam is 58.33 percent (35 out of 60 questions).

Image

Table 6-1 What Your Score Means

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

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