What is the result?

A. yo ey

B. hey oh hi

C. hey oh hi ey

D. oh hi hey

E. hey oh hi yo ey

F. yo hey oh hi ey

G. Compilation fails.

H. An exception is thrown at runtime.

36. Given:

         1. class Hotel {
         2.   static void doStuff(int x) {
         3.     assert (x < 0) : "hotel";
         4.   }
         5. }
         6. public class Motel13 extends Hotel {
         7.   public static void main(String[] args) {
         8.     doStuff(-5);
         9.     int y = 0;
        10.     assert (y < 0) : "motel";
        11. } }

Which of the following invocations will run without exception? (Choose all that apply.)

A. java Motel13

B. java -ea Motel13

C. java -da:Hotel Motel13

D. java -da:Motel13 Motel13

E. java -ea -da:Hotel Motel13

F. java -ea -da:Motel13 Motel13

37. Given:

         3. class One {
         4.   void go1() { System.out.print("1 ");  }
         5.   final void go2() { System.out.print("2 "); }
         6.   private void go3() { System.out.print("3 "); }
         7. }
         8. public class OneB extends One {
         9.   void go1() { System.out.print("1b "); }
        10.   void go3() { System.out.print("3b "); }
        11.
        12.  public static void main(String[] args) {
        13.    new OneB().go1();
        14.    new One().go1();
        15.    new OneB().go2();
        16.    new OneB().go3();
        17.    new One().go3();
        18. } }

What is the result?

A. 1 1 2 3 3

B. 1b 1 2 3b 3

C. 1b 1b 2 3b 3b

D. Compilation fails due to a single error.

E. Compilation fails due to errors on more than one line.

38. Given:

         3. public class Gauntlet {
         4.   public static void main(String[] args) {
         5.     String r = "0";
         6.     int x = 3, y = 4;
         7.     boolean test = false;
         8.     if((x > 2) || (test = true))
         9.     if((y > 5) || (++x == 4))
        10.     if((test == true) || (++y == 4))
        11.     r += "1";
        12.     else if(y == 5) r += "2";
        13.     else r += "3";
        14.     else r += "4";
        15.     // else r += "5";
        16.     System.out.println(r);
        17. } }

And given that, if necessary you can add line 15 to make the code compile, what is the result? (Choose all that apply.)

A. At line 10, test will equal true

B. At line 10, test will equal false

C. The output will be 02

D. The output will be 03

E. The output will be 023

F. The code will compile as is.

G. The code will only compile if line 15 is added.

39. Given:

         2. import rt.utils.Remote;
         3. public class Controller{
         4.   public static void main(String[] args){
         5.   Remote remote = new Remote();
         6. } }

And rt.utils.Remote class is properly bundled into a JAR file called rtutils.jar.

And given the following steps:

P. Place rtutils.jar in the $ROOT directory.

Q. Extract rtutils.jar and put rt directory with its subdirectories in the $ROOT directory.

R. Extract rtutils.jar and place Remote.class in the $ROOT directory.

S. Place rtutils.jar in the $JAVA_HOME/jre/lib/ext directory.

X. Compile using: javac -cp rtutils.jar Controller.java

Y. Compile using: javac Controller.java

Z. Compile using: javac -cp . Controller.java

If Controller.java resides in the $ROOT directory, which set(s) of steps will compile the Controller class? (Choose all that apply.)

A. P -> X

B. Q -> Y

C. R -> Z

D. P -> Z

E. R -> Y

F. S -> X

G. S -> Z

40. Given:

         2. public class Boot {
         3.   static String s;
         4.   static { s = ""; }
         5.   { System.out.print("shinier "); }

         6.   static { System.out.print(s.concat("better ")); }
         7.   Boot() { System.out.print(s.concat("bigger ")); }
         8.   public static void main(String[] args) {
         9.     new Boot();
        10.     System.out.println("boot");
        11. } }

What is the result?

A. better bigger boot

B. better bigger shinier boot

C. better shinier bigger boot

D. bigger shinier better boot

E. shinier better bigger boot

F. A NullPointerException is thrown at runtime.

G. An ExceptionInInitializationError is thrown at runtime.

41. Fill in the blanks using the following fragments, so the code compiles and produces the output: "0 4 "

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 only once.

Code:

          import __________________
          public class Latte {   public static void main(String[] args) {
            Pattern p = ________________
            Matcher m = p.matcher(__________________);
            while(_____________)
              System.out.print(______________ + " ");
         } }

Fragments:

Image

42. Given:

         3. public static void main(String[] args) {
         4.   try {
         5.     throw new Error();
         6.   }
         7.   catch (Error e) {
         8.     try { throw new RuntimeException(); }
         9.     catch (Throwable t) { }
        10.  }
        11.  System.out.println("phew");
        12. }

Which are true? (Choose all that apply.)

A. The output is phew

B. The code runs without output.

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

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

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

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

43. Given:

         1. import java.util.*;
         2. public class Analyzer {
         3.   static List<Exception> me;
         4.   Exception myEx;
         5.   public static void main(String[] args) {
         6.     Analyzer[] aa = {new Analyzer(), new Analyzer()};
         7.     me = new ArrayList<Exception>();
         8.     for(int i = 0; i < 2; i++) {
         9.       try {
        10.         if(i == 0) throw new Exception();
        11.         if(i == 1) throw new MyException();
        12.      }
        13.       catch(Exception e) {
        14.         me.add(e);
        15.         aa[i].myEx = e;
        16.      }
        17.   }
        18.   System.out.println(aa.length + " " + me.size());
        19.   aa = null; me = null;
        20.   // do more stuff
        21. } }
        22. class MyException extends Exception { }

When line 20 is reached, how many objects are eligible for garbage collection?

A. 2

B. 4

C. 5

D. 6

E. 7

F. 8

44. Given the following from the java.io.File API:

Field Summary: static String separator

Method Summary: static File[] listRoots()

And given:

         2. // insert code here
         3. // insert code here
         4. public class Eieio {
         5.   public static void main(String[] args) {
         6.     try {
         7.       String s = "subdir" + separator + "myFile.txt";
         8.       java.io.File f = new java.io.File(s);
         9.       java.io.FileReader fr = new java.io.FileReader(f);
        10.       java.io.File[] r = listRoots();
        11.       fr.close();
        12.     }
        13.     catch(Exception e) { }
        14. } }

And the following four fragments:

I. import java.io.*;

II. import static java.io.File.*;

III. import static java.io.File.separator;

IV. import static java.io.File.listRoots;

Which set(s) of fragments, inserted independently on lines 2 and/or 3, will compile? (Choose all that apply.)

A. Fragment I

B. Fragment II

C. Fragments I and III

D. Fragments I and IV

E. Fragments II and IV

F. Fragments III and IV

45. Given:

         1. import java.util.*;
         2. public class Bucket {
         3.   public static void main(String[] args) {
         4.     Set<String> hs = new HashSet<String>();
         5.     Set<String> lh = new LinkedHashSet<String>();
         6.     Set<String> ts = new TreeSet<String>();
         7.     List<String> al = new ArrayList<String>();
         8.     String[] v = {"1", "3", "1", "2"};
         9.     for(int i=0; i< v.length; i++) {
        10.       hs.add(v[i]);  lh.add(v[i]);  ts.add(v[i]);  al.add(v[i]);
        11.    }
        12.    Iterator it = hs.iterator();
        13.    while(it.hasNext()) System.out.print(it.next() + " ");
        14.    Iterator it2 = lh.iterator();
        15.    while(it2.hasNext()) System.out.print(it2.next() + " ");
        16.    Iterator it3 = ts.iterator();
        17.    while(it3.hasNext()) System.out.print(it3.next() + " ");
        18.    Iterator it5 = al.iterator();
        19.    while(it5.hasNext()) System.out.print(it5.next() + " ");
        20. } }

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

A. An exception is thrown at runtime.

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

C. "1 3 2" is only guaranteed to be in the output once.

D. "1 2 3" is only guaranteed to be in the output once.

E. "1 3 2" is guaranteed to be in the output more than once.

F. "1 2 3" is guaranteed to be in the output more than once.

G. "1 3 1 2" is guaranteed to be in the output at least once.

H. Compilation fails due to error(s) on lines other than line 18.

46. Given:

         2. public class Maize {
         3.   public static void main(String[] args) {
         4.     String s = "12";
         5.     s.concat("ab");
         6.     s = go(s);
         7.     System.out.println(s);
         8.   }
         9.   static String go(String s) {
        10.     s.concat("56");
        11.     return s;
        12. } }

What is the result?

A. ab

B. 12

C. ab56

D. 12ab

E. 1256

F. 12ab56

G. Compilation fails.

47. Given:

         4. class MySuper { protected MySuper() { System.out.print("ms "); } }
         5. public class MyTester extends MySuper {
         6.   private MyTester() { System.out.print("mt "); }
         7.   public static void main(String[] args) {
         8.     new MySuper();
         9.     class MyInner {
        10.       private MyInner() { System.out.print("mi "); }
        11.       { new MyTester(); }
        12.       { new MySuper(); }
        13.     }
        14.     new MyInner();
        15. } }

What is the result?

A. ms mi mt ms

B. ms mt ms mi

C. ms mi ms mt ms

D. ms ms mt ms mi

E. Compilation fails.

F. An exception is thrown at runtime.

48. Given:

         2. public class Tshirt extends Thread {
         3.   public static void main(String[] args) {
         4.     System.out.print(Thread.currentThread().getId() + " ");
         5.     Thread t1 = new Thread(new Tshirt());
         6.     Thread t2 = new Thread(new Tshirt());
         7.     t1.start();
         8.     t2.run();
         9.  }

        10.  public void run() {
        11.    for(int i = 0; i < 2; i++)
        12.      System.out.print(Thread.currentThread().getId() + " ");
        13. } }

Which are true? (Choose all that apply.)

A. No output is produced.

B. The output could be 1 1 9 9 1

C. The output could be 1 2 9 9 2

D. The output could be 1 9 9 9 9

E. An exception is thrown at runtime.

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

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

49. Given:

        14.  FileWriter fw1 =
               new FileWriter(new File("f1.txt"));
        15.  FileWriter fw2 =
               new FileWriter(new BufferedWriter(new PrintWriter("f2.txt")));
        16.  PrintWriter pw1 =
               new PrintWriter(new BufferedWriter(new FileWriter("f3.txt")));
        17.  PrintWriter pw2 =
               new PrintWriter(new FileWriter(new File("f4.txt")));

And given the proper imports and error handling, what is the result?

A. Compilation succeeds.

B. Compilation fails due to multiple errors.

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

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

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

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

50. Given:

         2. import java.util.*;
         3. public class GIS {
         4.   public static void main(String[] args) {
         5.   TreeMap<String, String> m1 = new TreeMap<String, String>();
         6.   m1.put("a", "amy"); m1.put("f", "frank");
         7.   NavigableMap<String, String> m2 = m1.descendingMap();
         8.   try {
         9.     m1.put("j", "john");
        10.     m2.put("m", "mary");
        11.   }
        12.   catch (Exception e) { System.out.print("ex "); }
        13.   m1.pollFirstEntry();
        14.   System.out.println(m1 + " " + m2);
        15. } }

What is the result?

A. {f=frank, j=john} {f=frank}

B. {f=frank, j=john} {m=mary, f=frank}

C. ex {f=frank, j=john} {f=frank}

D. {f=frank, j=john, m=mary} {m=mary, j=john, f=frank}

E. ex {f=frank, j=john, m=mary} {f=frank}

F. ex {f=frank, j=john, m=mary} {f=frank, a=amy}

G. {a=amy, f=frank, j=john, m=mary} {f=frank, a=amy}

H. Compilation fails due to error(s) in the code.

51. Given:

         2. public class Clover extends Harrier {
         3.   String bark() { return "feed me "; }
         4.   public static void main(String[] args) {
         5.     Dog[] dogs = new Dog[3];
         6.     dogs[0] = new Harrier();
         7.     dogs[1] = (Dog)new Clover();
         8.     dogs[2] = (Dog)new Harrier();
         9.     for(Dog d: dogs)  System.out.print(d.bark());
        10. } }
        11. class Dog { String bark() { return "bark "; } }
        12. class Harrier extends Dog { String bark() { return "woof "; } }

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

A. bark bark bark

B. woof bark bark

C. woof feed me woof

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.

52. Given:

         1. public class Egg<E extends Object> {
         2.   E egg;
         3.   public Egg(E egg) {
         4.     this.egg=egg;
         5.  }
         6.  public E getEgg() {
         7.    return egg;
         8.  }
         9.  public static void main(String[] args) {
        10.    Egg<Egg> egg1 = new Egg(42);
        11.    Egg egg2 = new Egg<Egg>(egg1.getEgg());
        12.    Egg egg3 = egg1.getEgg();
        13. } }

Which are true? (Choose all that apply).

A. Compilation fails.

B. An exception is thrown at runtime.

C. Line 10 compiles with a warning.

D. Line 10 compiles without warnings.

E. Line 11 compiles with a warning.

F. Line 11 compiles without warnings.

53. Given:

         2. public class Tolt {
         3.   public static void checkIt(int a) {
         4.     if(a == 1)  throw new IllegalArgumentException();
         5.  }

         6.  public static void main(String[] args) {
         7.    for(int x=0; x<2; x++)
         8.      try {
         9.        System.out.print("t ");
        10.        checkIt(x);
        11.        System.out.print("t2 ");
        12.     }
        13.     finally { System.out.print("f "); }
        14. } }

What is the result?

A. "t t2 f t "

B. "t t2 f t f "

C. "t t2 f t t2 f "

D. "t t2 f t ", followed by an exception.

E. "t t2 f t f ", followed by an exception.

F. "t t2 f t t2 f ", followed by an exception.

G. Compilation fails.

54. Given:

         2. class Noodle {
         3.   String name;
         4.   Noodle(String n) { name = n; }
         5. }
         6. class AsianNoodle extends Noodle {
         7.   public boolean equals(Object o) {
         8.     AsianNoodle n = (AsianNoodle)o;
         9.     if(name.equals(n.name))  return true;
        10.     return false;
        11.  }

        12.  public int hashCode() { return name.length(); }
        13.    AsianNoodle(String s) { super(s); }
        14. }
        15. public class Soba extends AsianNoodle {
        16.   public static void main(String[] args) {
        17.     Noodle n1 = new Noodle("bob");  Noodle n2 = new Noodle("bob");
        18.     AsianNoodle a1 = new AsianNoodle("fred");
        19.     AsianNoodle a2 = new AsianNoodle("fred");
        20.     Soba s1 = new Soba("jill");  Soba s2 = new Soba("jill");
        21.     System.out.print(n1.equals(n2) + " " + (n1 == n2) + " | ");
        22.     System.out.print(a1.equals(a2) + " " + (a1 == a2) + "| ");
        23.     System.out.println(s1.equals(s2) + " " + (s1 == s2));
        24.  }
        25.     Soba(String s) { super(s); }
        26. }

What is the result?

A. Compilation fails.

B. true true | true true | true true

C. true false | true false | true false

D. false false | true false | true false

E. false false | true false |false

F. false false | false false |false false

55. Given:

         2. public class Checkout2 implements Runnable {
         3.   void doStuff() { }
         4.   synchronized void doSynch() {
         5.      try { Thread.sleep(1000); }
         6.      catch (Exception e) { System.out.print("e "); }

         7.  }
         8.  public static void main(String[] args) {
         9.    long start = System.currentTimeMillis();
        10.    new Thread(new Checkout2()).start();
        11.      Thread t1 = new Thread(new Checkout2());
        12.      t1.start();
        13.      try { t1.join(); }
        14.      catch (Exception e) { System.out.print("e "); }
        15.      System.out.println("elapsed: " + (System.currentTimeMillis() - start));
        16.   }
        17.   public void run() {
        18.     for(int j = 0; j < 4; j++) {
        19.       doStuff();
        20.       try { Thread.sleep(1000); }
        21.         catch (Exception e) { System.out.print("e "); }
        22.         doSynch();
        23. } } }

Which are true? (Choose all that apply.)

A. Compilation fails.

B. Elapsed time will be about eight seconds.

C. Elapsed time will be about nine seconds.

D. Elapsed time will be about 12 seconds.

E. Changing doSynch() to be unsynchronized will change elapsed by only a few milliseconds.

F. Changing doSynch() to be unsynchronized will change elapsed by 450 or more milliseconds.

56. Your virtual-world simulator is designed such that Grobnets can have several Ooflas, and Yazells support the contract implied by Whompers. Grobnets are a type of Yazell, and can also act as Floordums or Whompers. Which of the following code fragments legally represent this design? (Choose all that apply.)

A. import java.util.*;

interface Floordum { }

interface Whomper { }

class Grobnet extends Yazell implements Floordum { List<Oofla> o; }

class Oofla { }

class Yazell implements Whomper { }

B. import java.util.*;

class Oofla { }

class Yazell implements Whomper { }

class Floordum { }

interface Whomper { }

class Grobnet extends Yazell, Floordum { List<Oofla> o; }

C. import java.util.*;

interface Floordum { }

interface Whomper { }

class Grobnet extends Yazell implements Floordum, Oofla { }

class Oofla { }

interface Yazell implements Whomper { }

D. import java.util.*;

interface Floordum { }

interface Whomper { }

class Grobnet extends Yazell implements Floordum { Set<Oofla> o; }

class Oofla { }

class Yazell { Whomper w; }

57. Given:

         1. import java.util.*;
         2. public class Ps {
         3.   public static void main(String[] args) {
         4.     PriorityQueue<String> pq = new PriorityQueue<String>();
         5.     pq.add("4");
         6.     pq.add("7");
         7.     pq.add("2");
         8.     // insert code here
         9. } }

Which code fragment(s), inserted independently at line 8, produce the output "2-4-7-"? (Choose all that apply.)

A. Iterator it2 = pq.iterator();

while(it2.hasNext()) System.out.print(it2.next() + "-");

System.out.println();

B. Arrays.sort(pq.toArray());

Iterator it3 = pq.iterator();

while(it3.hasNext()) System.out.print(it3.next() + "-");

System.out.println();

C. Object[] pqa = pq.toArray();

Arrays.sort(pqa);

for(Object o: pqa) System.out.print(o + "-");

System.out.println();

D. String s = pq.poll();

while (s != null) {

System.out.print(s + "-");

s = pq.poll();

}

E. String s = pq.peek();

while (s != null) {

System.out.print(s + "-");

s = pq.peek();

}

58. Given that the current directory is bigApp, and the following directory structure:

Image

And the code:

       package com.wickedlysmart;
       public class BigAppClass1 {
         int doStuff() { return 42; }
       }

And the following command-line invocations:

I. javac -d source/com/wickedlysmart/BigAppClass1.java

II. javac -d classes source/com/wickedlysmart/BigAppClass1.java

III. javac -d classes/com/wickedlysmart source/com/wickedlysmart/BigAppClass1.java

Which are true? (Choose all that apply.)

A. Invocation I will compile the file and place the .class file in the bigApp directory.

B. Invocation II will compile the file and place the .class file in the classes directory.

C. Invocation I will compile the file and place the .class file in the wickedlysmart directory.

D. Under the bigApp/classes directory, invocation II will build com/wickedlysmart subdirectories, and place the .class file in wickedlysmart.

E. Under the bigApp/classes directory, invocation III will build com/wickedlysmart subdirectories, and place the .class file in wickedlysmart.

59. Given:

         2. class Jog implements Runnable {
         3.   public void run() {
         4.     for(int i = 0; i < 8; i++) {
         5.       try { Thread.sleep(200); }
         6.       catch (Exception e) { System.out.print("exc "); }
         7.       System.out.print(i + " ");
         8. } } }
         9. public class Marathon {
        10.   public static void main(String[] args) throws Exception {
        11.     Jog j1 = new Jog();
        12.     Thread t1 = new Thread(j1);
        13.     t1.start();
        14.     t1.sleep(500);
        15.     System.out.print("pre ");
        16.     t1.interrupt();
        17.     t1.sleep(500);
        18.     System.out.print("post ");
        19. } }

Assuming that sleep() sleeps for about the amount of time specified in its argument, and that all other code runs almost instantly, which output is likely? (Choose all that apply.)

A. exc

B. 0 1 pre exc post

C. exc 0 1 2 3 4 5 6 7

D. pre post 0 1 2 3 4 5 6 7

E. pre exc 0 1 post 2 3 4 5 6 7

F. 0 1 pre exc 2 3 4 post 5 6 7

60. Given:

         2. class Explode {
         3.   static String s = "";
         4.   static { s += "sb1 "; }
         5.   Explode() { s += "e "; }
         6. }
         7. public class C4 extends Explode {
         8.   C4() {
         9.     s += "c4 ";
        10.    new Explode();
        11.  }
        12.  static {
        13.    new C4();
        14.    System.out.print(s);
        15.  }
        16.  { s += "i "; }
        17.  public static void main(String[] args) { }
        18. }

And given the command-line invocation "java C4", what is the result?

A. e c4 i

B. e i c4

C. e sb1 i c4

D. sb1 e i c4 e

E. sb1 e c4 i e

F. Compilation fails.

G. A StackOverflowError is thrown.

H. An exception other than StackOverflowError is thrown.

Analyzing Your Results

First, we have to come clean. We tried to make this last exam the toughest one in the book.

Table 7-1 is based partly on the assumption that this last exam is a bit tougher than the real thing. We hear from a lot of candidates, and we understand that everyone has different goals. If your goal is to score over 90 percent on the real exam, then you have to analyze your results differently than if your goal is to pass comfortably. The following table is based on a couple of assumptions:

Image This last exam is a bit tougher than the real thing.

Image Your goal is to pass comfortably. By that, we mean that when you take the real exam you don’t just barely squeak (squeal?) by with a passing score, but that you’re also pleased even if you don’t get 90 percent.

As we’ve said several times, as of this writing, a passing score on the OCP Java SE Programmer exam is 58.33 percent (35 out of 60 questions).

Image

TABLE 7-1 What Your Score Means If You Want to Pass Comfortably

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

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