PRACTICE EXAM 4

The real exam has 60 questions and you are given three hours to complete it. 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. Given:

         1. abstract class Vibrate {
         2.   static String s = "-";
         3.   Vibrate() {  s += "v"; }
         4. }
         5. public class Echo extends Vibrate {
         6.   Echo() { this(7); s += "e"; }
         7.   Echo(int x) { s += "e2"; }
         8.   public static void main(String[] args) {
         9.     System.out.print("made " + s + " ");
        10.  }
        11.  static {
        12.    Echo e = new Echo();
        13.    System.out.print("block " + s + " ");
        14. } }

What is the result?

A. made -ve2e

B. block -ee2v

C. block -ve2e

D. made -eve2 block -eve2

E. made -ve2e block -ve2e

F. block -ve2e made -ve2e

G. block -ve2e made -ve2eve2e

H. Compilation fails.

2. Given:

         3. public class KaChung {
         4.   public static void main(String[] args) {
         5.     String s = "";
         6.     if(Integer.parseInt("011") == Integer.parseInt("9")) s += 1;
         7.     if(021 == Integer.valueOf("17")) s += 2;
         8.     if(1024 == new Integer(1024)) s += 3;
         9.     System.out.println(s);
        10. } }

What is the result?

A. 2

B. 3

C. 13

D. 23

E. 123

F. Compilation fails.

G. An exception is thrown at runtime.

3. Given the invocation "java GiveUp" and:

         2. public class GiveUp {
         3.   public static void main(String[] args) throws Exception {
         4.     try {
         5.       assert false;
         6.       System.out.print("t ");
         7.  }
         8.  catch (Error e) {
         9.       System.out.print("c ");
        10.      throw new Exception();
        11.  }
        12.  finally { System.out.print("f "); }
        13. } }

What is the result?

A. c

B. c f

C. t f

D. Compilation fails.

E. "c f " followed by an uncaught exception.

4. Given:

         3. import java.util.*;
         4. public class VC {
         5.   public static void main(String[] args) {
         6.     List<Integer> x = new ArrayList<Integer>();
         7.     Integer[] a = {3, 1, 4, 1};
         8.     x = Arrays.asList(a);
         9.     a[3] = 2;
        10.    x.set(0, 7);
        11.    for(Integer i: x) System.out.print(i + " ");
        12.    x.add(9);
        13.    System.out.println(x);
        14. } }

What is the result?

A. Compilation fails.

B. 3 1 4 2 [7, 1, 4, 1]

C. 3 1 4 2 [7, 1, 4, 2]

D. 7 1 4 2 [7, 1, 4, 2]

E. 3 1 4 2 [7, 1, 4, 1, 9]

F. 3 1 4 2 [7, 1, 4, 2, 9]

G. 7 1 4 2, followed by an exception.

H. 3 1 4 2, followed by an exception.

5. Given:

         3. public class Honcho {
         4.   static boolean b1 = false;
         5.   static int z = 7;
         6.   static Long y;
         7.   public static void main(String[] args) {
         8.     for(int i = 0; i < 4; i++)
         9.       go(i);
        10.  }
        11.  static void go(int x) {
        12.    try {
        13.      if((x == 0) && (!b1 && z == 7)) System.out.print("0 ");
        14.      if(x < 2 ^ x < 10) System.out.print("1 ");
        15.      if((x == 2) &&  p>(y == null | (y.longValue() == 0))) System.out.print("2 ");
        16.      if(z <= (x + 4)) System.out.print("3 ");
        17.  }
        18.  catch(Exception e) { System.out.print("e "); }
        19.   } }

What is the result?

A. 0 1 2 3

B. 1 e 1 3

C. 0 1 e 1 3

D. 0 1 1 1 1 3

E. 1 1 1 2 1 3

F. 0 1 1 1 2 1 3

G. Compilation fails.

6. Given:

         2. class Chilis {
         3.   Chilis(String c, int h) { color = c; hotness = h; }
         4.   String color;
         5.   int hotness;
         6.   public boolean equals(Object o) {
         7.     Chilis c = (Chilis)o;
         8.     if(this.color == c.color) return true;
         9.     return false;
        10.  }
        11.  public int hashCode() { return (color.length() + hotness); }
        12. }

If instances of class Chilis are to be used as keys in a Map, which are true? (Choose all that apply.)

A. The code will not compile.

B. The hashCode() and equals() contracts have been supported.

C. The equals() method is reflexive, symmetric, and transitive.

D. The Chilis class CANNOT be used to create keys to reliably add and retrieve entries in a Map.

E. If the hashCode() method is not considered, the equals() method could be an override that supports the equals() contract.

F. If the equals() method is not considered, the hashCode() method could be an override that supports the hashCode() contract.

7. Given:

         2. package pack.clients;
         3. import pack.banking.Bank;
         4. public class Client{
         5.   public static void main(String[] args){
         6.     Bank bank = new Bank();
         7.     System.out.println(bank.getMoney(2000L));
         8. } }

And given that Client.java resides in the $ROOT directory and is not yet compiled. There is another class named pack.banking.Bank, which has a method called getMoney(long) that returns a value. Bank class is compiled and deployed into a JAR file called pack.jar, as shown in the following directory structure

Image

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

A. To compile, use javac -cp pack.jar -d . Client.java

B. To compile, use javac -cp pack.jar#pack.banking.Bank -d . Client.java

C. To compile, use javac -cp * -d . Client.java

D. To run, use java -cp pack.jar pack.clients.Client

E. To run, use java -cp . -d pack.jar pack.clients.Client

F. To run, use java -cp . -cp pack.jar pack.clients.Client

G. To run, use java -cp .:pack.jar pack.clients.Client

8. Given:

         2. class Bird {
         3.   public static String s = "";
         4.   public static void fly() { s += "fly "; }
         5. }
         6. public class Hummingbird extends Bird {
         7.   public static void fly() { s += "hover "; }
         8.   public static void main(String[] args) {
         9.     Bird b1 = new Bird();
        10.    Bird b2 = new Hummingbird();
        11.    Bird b3 = (Hummingbird)b2;
        12.    Hummingbird b4 = (Hummingbird)b2;
        13.
        14.    b1.fly();  b2.fly();  b3.fly();  b4.fly();
        15.    System.out.println(s);
        16. } }

What is the result?

A. fly fly fly fly

B. fly fly fly hover

C. fly fly hover hover

D. fly hover hover hover

E. hover hover hover hover

F. Compilation fails.

G. An exception is thrown at runtime.

9. Given two files:

         1. package com;
         2. public class Extramuros {
         3.   public static void howdy() { System.out.print("howdy "); }
         4.   public static final Extramuros ex = new Extramuros();
         5.   public int instVar = 42;
         6.   public enum avout {OROLO, JAD};
         7. }
       
         1. // insert code here
        …
         6. public class Theorics {
         7.   public static void main(String[] args) {
         8.     Extramuros.howdy();
         9.     System.out.print(Extramuros.avout.OROLO + " ");
        10.    howdy();
        11.    System.out.print(ex.instVar + " ");
        12. } }

Which are the minimum line(s) of code to add at "insert code here" for the files to compile? (Choose all that apply.)

A. import static com.*;

B. import com.Extramuros;

C. import com.Extramuros.*;

D. import static com.Extramuros;

E. import static com.Extramuros.*;

F. Even with correct imports, the code will not compile due to other errors.

10. Given:

         2. import java.text.*;
         3. import java.util.*;
         4. public class Vogue {
         5.   public static void main(String[] args) {
         6.     DateFormat df1 = DateFormat.getInstance();
         7.     DateFormat df2 = DateFormat.getInstance(DateFormat.SHORT);
         8.     DateFormat df3 = DateFormat.getDateInstance(DateFormat.FULL);
         9.     DateFormat df4 = DateFormat.getDateInstance(DateFormat.EXTENDED);
        10. } }

Which are true? (Choose all that apply.)

A. Line 2 is not necessary.

B. Line 3 is not necessary.

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

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.

11. Given:

         2. public class Cult extends Thread {
         3.   static int count = 0;
         4.   public void run() {
         5.     for(int i = 0; i < 100; i++) {
         6.       if(i == 5 && count < 3) {
         7.         Thread t = new Cult(names[count++]);
         8.         t.start();
         9.         // insert code here
        10.      }
        11.      System.out.print(Thread.currentThread().getName() + " ");
        12.    }
        13.  }
        14.  public static void main(String[] args) {
        15.    new Cult("t0").start();
        16.  }
        17.  Cult(String s) { super(s); }
        18.  String[] names = {"t1", "t2", "t3"};
        19. }

And these two fragments:

I. try { t.join(); } catch(Exception e) { }

II. try { Thread.currentThread().join(); } catch(Exception e) { }

When each fragment is inserted independently at line 9, which are true? (Choose all that apply.)

A. With fragment I, t0 completes last.

B. With fragment I, t3 completes last.

C. With fragment II, t0 completes last.

D. With fragment II, t3 completes last.

E. With both fragments, compilation fails.

F. With fragment I, the code never completes.

G. With fragment II, the code never completes.

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

A. If a class’s member’s values can be retrieved, but not changed, without using the class’s API, the class is not cohesive.

B. If a class’s member’s values can be retrieved, but not changed, without using the class’s API, tight coupling could occur.

C. If a class’s member’s values can be retrieved, but not changed, without using the class’s API, the class is not well encapsulated.

D. If a class’s member’s values can be updated only through the use of its API, or by an inner class, the class is well encapsulated.

E. If a class’s member’s values can be updated only through the use of its API, or by an inner class, the class is NOT well encapsulated.

13. Given:

         3. public class BigData {
         4.   static BigData bd;
         5.   public static void main(String[] args) {
         6.     new BigData().doStuff();
         7.     // do lots of memory intensive stuff
        …      // JVM finds an eligible BigData object for GC
        …     // JVM invokes finalize()
        …    // do more stuff
        48.  }
        49.  void doStuff() {  }
        50.  // insert code here
        51.    bd = this;
        52.  }
        53. }
        54. class MyException extends Exception { }

and the following four fragments:

I. protected void finalize() throws Throwable {

II. protected void finalize() {

III. protected void finalize() throws MyException {

IV. void finalize() {

If the fragments are inserted, independently, at line 50, which are true? (Choose all that apply.)

A. Fragment I compiles.

B. Fragment II compiles.

C. Fragment III compiles.

D. Fragment IV compiles.

E. Of those that compile, the GC will collect any given object after the JVM has called finalize() on that object.

F. Because of the way finalize() has been overridden, the GC will never collect eligible objects of type BigData.

14. Given:

         2. interface Clickable {  void click();  }
         3. // insert code here

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

A. interface Keyboard extends Clickable { }

B. interface Keyboard extends Clickable { void click(); }

C. interface Keyboard extends Clickable { void doClick(); }

D. interface Keyboard extends Clickable { void click() { ; } }

E. interface Keyboard extends Clickable { protected void click(); }

F. interface Keyboard extends Clickable { void click(); void doClick(); }

15. Given:

         1. public class Argue {
         2.   static boolean b;
         3.   static int x = 0;
         4.   public static void main(String[] args) {
         5.     int guess = (int)(Math.random() * 5);
         6.     if(guess < 0) assert false;
         7.     assert b = true;
         8.     assert x = 0;
         9.     assert x == 0;
        10. } }

Which are true? (Choose all that apply.)

A. The code compiles.

B. The assert on line 6 is appropriate.

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

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.

16. Given two files:

         1. package com.wickedlysmart2;
         2. public 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. }

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

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

A. do1 do2 do3

B. "do1 ", followed by an exception.

C. Compilation fails due to an error on line 4 of UPS.

D. Compilation fails due to an error on line 5 of UPS.

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

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

17. Given:

         4. class Electricity { int getCharge() { return 24; } }
         5. public class Voltage extends Electricity {
         6.   enum volts {twelve, twentyfour, oneten};
         7.   public static void main(String[] args) {
         8.     volts v = volts.twentyfour;
         9.     switch (v) {
        10.      case twelve:
        11.        System.out.print("12 ");
        12.      default:
        13.        System.out.print(getCharge() + " ");
        14.      case oneten:
        15.        System.out.print("110 ");
        16. } } }

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

A. 24

B. 24 110

C. 24 110 12

D. Compilation fails due to a misuse of enums.

E. Compilation fails due to a non-enum issue.

18. Given:

         2. import java.io.*;
         3. public class Uboat {
         4.   public static void main(String[] args) {

         5.   try {
         6.     File f1 = new File("sub1");
         7.     f1.mkdir();
         8.     File f2 = new File(f1,"sub2");
         9.     File f3 = new File(f1,"sub3");
        10.    PrintWriter pw = new PrintWriter(f3);
        11.  } catch (Exception e) { System.out.println("ouch"); }
        12. } }

And, if the code compiles, what is the result if "java Uboat" is invoked TWICE? (Choose all that apply.)

A. Compilation fails.

B. The second invocation produces the output "ouch"

C. The second invocation creates at least one new file as a peer to the new directory.

D. The first invocation creates a new directory and one new file in that directory.

E. The first invocation creates a new directory and two new files in that directory.

F. The first invocation creates a new directory and at least one new file as a peer to it.

19. Given:

         2. class Car {
         3.   private Car() { }
         4.   protected Car(int x) { }
         5. }
         6. public class MG extends Car {
         7.   // MG(int x) { }
         8.   // MG(int x) { super(); }
         9.   // MG(int x) { super(x); }
        10.   // private MG(int x) { super(x); }
        11.   // MG() { }
        12.   // MG() { this(); }
        13.   // MG() { this(6); }
        14.   // MG() { super(7); }
        15.   public static void main(String[] args) {
        16.     new MG(7);
        17.     new MG();
        18. } }

Which sets of constructors can be uncommented for the code to compile? (Choose all that apply.)

A. Line 7 and line 11.

B. Line 7 and line 14.

C. Line 8 and line 12.

D. Line 8 and line 13.

E. Line 9 and line 12.

F. Line 9 and line 13.

G. Line 10 and line 11.

H. Line 10 and line 14.

20. Given:

         2. interface Gadget {
         3.   int patent = 12345;
         4.   Gadget doStuff();
         5. }
         6. public class TimeMachine implements Gadget {
         7.   int patent = 34567;
         8.   public static void main(String[] args) {
         9.     new TimeMachine().doStuff();
        10.  }
        11.  TimeMachine doStuff() {
        12.    System.out.println( ++patent);
        13.    return new TimeMachine();
        14. } }

If javac is invoked twice:

        javac -source 1.4 TimeMachine.java
        javac TimeMachine.java

And, if "java TimeMachine" is invoked whenever TimeMachine compiles, what is the result?

A. First, compilation fails, then 12346

B. First, compilation fails, then 34568

C. First, 12346, then compilation fails.

D. First, 34568, then compilation fails.

E. First, 12346, then 34568

F. Compilation fails on both javac invocations.

21. Given:

         1. import java.util.*;
         2. public class LogSplitter {
         3.   public static void main(String[] args) {
         4.     for(int x = 1; x < args.length; x++)
         5.     System.out.print(args[0].split(args[x]).length + " ");
         6. } }

And the command-line invocation:

        java LogSplitter "x1 23 y #" "d" "s" "w"

What is the result?

A. 4 4 6

B. 3 3 6

C. 5 4 6

D. 4 6 4

E. 3 6 3

F. 5 6 4

G. Compilation fails.

H. An exception is thrown at runtime.

22. Given this code in a method:

         3.  int y, count = 0;
         4.  for(int x = 3; x < 6; x++) {
         5.    try {
         6.      switch(x) {
         7.        case 3: count++;
         8.        case 4: count++;
         9.        case 7: count++;
        10.       case 9: { y = 7 / (x - 4); count += 10; }
        11.      }
        12.     } catch (Exception ex) { count++; }
        13.   }
        14.   System.out.println(count);

What is the result?

A. 2

B. 15

C. 16

D. 25

E. 26

F. Compilation fails.

G. An exception is thrown with no other output.

23. Given:

         5. abstract class Thing { static String s = ""; Thing() { s += "t "; } }
         6. class Steel extends Thing {
         7.   Steel() { s += "s "; }
         8.   Steel(String s1) {
         9.     s += s1;
        10.     new Steel();

        11.   }
        12. }
        13. public class Tungsten extends Steel {
        14.   Tungsten(String s1) {
        15.     s += s1;
        16.     new Steel(s);
        17.   }
        18.   public static void main(String[] args) {
        19.     new Tungsten("tu ");
        20.     System.out.println(s);
        21. } }

What is the result?

A. s tu s tu s

B. t s tu t s t s

C. t s tu t t s tu t s

D. t tu t s tu t t tu t s tu t s

E. Compilation fails.

F. An exception is thrown at runtime.

24. You want to add a capability to MyStuff so that collections of MyStuff objects can be sorted in a “natural” order. Use the fragments to fill in the blanks to create this capability. Note: Not all the fragments will be used, not all the blanks have to be filled (maybe), and you can use each fragment more than once

Second note: As in the real exam’s Drag-and-Drop questions, there may be more than one correct answer. Here, give yourself bonus points if you create more than one correct answer. In the real exam, you’ll get full credit for ANY correct answer.

         import java.util.*;
         class MyStuff  _______________  _____________________________ {
           MyStuff(String n, int v) { name = n; value = v; }
           String name; int value;
           ____________  _________  ________________  _____________________  {
           return __________________  ___________________ ;
          }
          public String toString() { return name + " " + value + " "; }
         }

Fragments:

Image

25. Given:

         2. public class Swanky {
         3.   Swanky s;
         4.   public static void main(String[] args) {
         5.     Swanky s1 = new Swanky();
         6.     s1.s = new Swanky();
         7.     go(s1.s);
         8.     Swanky s2 = go(s1);
         9.     if(s2.s.equals(s1)) System.out.print("1 ");
        10.     if(s2.equals(s1)) System.out.print("2 ");
        11.     if(s1.s.equals(s1)) System.out.print("3 ");
        12.     if(s2.s.equals(s2)) System.out.print("4 ");
        13.  }
        14.  static Swanky go(Swanky s) {
        15.    Swanky gs = new Swanky();
        16.    gs.s = s;
        17.    return gs;
        18. } }

What is the result?

A. 1

B. 1 2

C. 1 2 4

D. Compilation fails.

E. "1 ", followed by an exception.

F. "1 2 ", followed by an exception.

26. Given:

         2. import java.util.*;
         3. public class AndOver {
         4.   public static void main(String[] args) {
         5.     List g = new ArrayList();
         6.     g.add(new Gaited("Eyra"));
         7.     g.add(new Gaited("Vafi"));
         8.     g.add(new Gaited("Andi"));
         9.     Iterator i2 = g.iterator();

        10.    while(i2.hasNext()) {
        11.      System.out.print(i2.next().name + " ");
        12. } } }
        13. class Gaited {
        14.   public String name;
        15.   Gaited(String n) { name = n; }
        16. }

What is the result?

A. Vafi Andi

B. Andi Eyra Vafi

C. Andi Vafi Eyra

D. Eyra Vafi Andi

E. Compilation fails.

F. The output order is unpredictable.

27. Given:

         2. interface Plant {
         3.   int greenness = 7;
         4.   void grow();
         5. }
         6. class Grass implements Plant {
         7.   // static int greenness = 5;
         8.   // int greenness = 5;
         9.   public static void main(String[] args) {
        10.    int greenness = 2;
        11.    new Grass().grow();
        12.  }
        13.  public void grow() {
        14.    System.out.println(++greenness);
        15. } }

Which are true? (Choose all that apply.)

A. As the code stands, the output is 3.

B. As the code stands, the output is 8.

C. As the code stands, it will NOT compile.

D. If line 7 is un-commented, the output is 6.

E. If line 8 is un-commented, the output is 6.

F. If line 7 is un-commented, the output is 8.

G. If line 7 is un-commented, the code will NOT compile.

28. Given:

         2. class Grab {
         3.   static int x = 5;
         4.   synchronized void adjust(Grab y) {
         5.     System.out.print(x-- + " ");
         6.     y.view(y);
         7.   }
         8.   synchronized void view(Grab z) { if(x > 0) z.adjust(z); }
         9. }
        10. public class Grapple implements Runnable {
        11.   static Thread t1;
        12.   static Grab g, g2;
        13.   public void run() {
        14.     if(Thread.currentThread().getId() == t1.getId()) g.adjust(g2);
        15.     else g2.view(g);
        16.  }
        17.  public static void main(String[] args) {
        18.    g = new Grab();
        19.    g2 = new Grab();
        20.    t1 = new Thread(new Grapple());
        21.    t1.start();
        22.    new Thread(new Grapple()).start();
        23. } }

Which are true? (Choose all that apply.)

A. Compilation fails.

B. The output could be 5 4 3 2 1

C. The output could be 5 4 3 2 1 0

D. The program could produce thousands of lines of output.

E. The program could deadlock before producing any output.

F. The output could be "5 ", followed by the program deadlocking.

29. Given:

         3. import java.util.*;
         4. public class Corner {
         5.   public static void main(String[] args) {
         6.     TreeSet<String> t1 = new TreeSet<String>();
         7.     TreeSet<String> t2 = new TreeSet<String>();
         8.     t1.add("b");  t1.add("7");
         9.     t2 = (TreeSet)t1.subSet("5", "c");
        10.     try {
        11.       t1.add("d");
        12.       t2.add("6");

        13.       t2.add("3");
        14.    }
        15.    catch (Exception e) { System.out.print("ex "); }
        16.    System.out.println(t1 + " " + t2);
        17. } }

What is the result?

A. [6, 7, b] [6, 7, b]

B. [6, 7, b, d] [6, 7, b]

C. ex [6, 7, b] [6, 7, b]

D. ex [6, 7, b, d] [6, 7, b]

E. [3, 6, 7, b, d] [6, 7, b]

F. ex [6, 7, b, d] [6, 7, b, d]

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

30. Given the current directory is bigApp, and the directory structure:

Image

And the three files:

         public class Cloned {
         public static void main(String[] args) { System.out.println("classes"); }
         }
         public class Cloned {
         public static void main(String[] args) { System.out.println("com"); }
         }
         public class Cloned {
         public static void main(String[] args) { System.out.println("ws"); }
         }

Have been compiled into the classes, com, and wickedlysmart directories, respectively. Which will produce the output "ws"? (Choose all that apply.)

A. java -cp wickedlysmart:. Cloned

B. java -cp classes/com/wickedlysmart Cloned

C. java -cp classes/com/wickedlysmart:classes Cloned

D. java -cp classes:classes/com/wickedlysmart Cloned

E. java -cp .:classes/com/wickedlysmart:classes Cloned

F. java -cp .:classes/com:classes/com/wickedlysmart Cloned

31. Given:

         1. import java.io.*;
         2. public class Edgy {
         3.   public static void main(String[] args) {
         4.     try {
         5.       wow();
         6.       // throw new IOException();
         7.     } finally {
         8.       // throw new Error();
         9.       // throw new IOException();
        10.    }
        11.  }
        12.  static void wow() {
        13.      // throw new IllegalArgumentException();
        14.      // throw new IOException();
        15. } }

And given that IOException is a direct subclass of java.lang.Exception: and that IllegalArgumentException is a runtime exception, which of the following, if uncommented independently, will compile? (Choose all that apply.)

A. Line 6

B. Line 8

C. Line 9

D. Line 13

E. Line 14

F. The code will NOT compile as is.

32. Given two files:

         1. package com;
         2. public class MyClass {
         3.   public static void howdy() { System.out.print("howdy "); }
         4.   public static final int myConstant = 343;
         5.   public static final MyClass mc = new MyClass();
         6.   public int instVar = 42;
         7. }

        11. import com.MyClass;
        12. public class TestImports2 {

        13.  public static void main(String[] args) {
        14.    MyClass.howdy();
        15.    System.out.print(MyClass.myConstant + " ");
        16.    System.out.print(myConstant + " ");
        17.    howdy();
        18.    System.out.print(mc.instVar + " ");
        19.    System.out.print(instVar + " ");
        20. } }

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

A. howdy 343 343 howdy 42 42

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

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

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

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

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

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

33. Given this code inside a method:

        13.  int count = 0;
        14.  outer:
        15.  for(int x = 0; x < 5; x++) {
        16.     middle:
        17.     for(int y = 0; y < 5; y++) {
        18.       if(y == 1) continue middle;
        19.       if(y == 3) break middle;
        20.       count++;
        21.     }
        22.     if(x > 2) continue outer;
        23.       count = count + 10;
        24.  }
        25.  System.out.println("count: " + count);

What is the result?

A. count: 33

B. count: 40

C. count: 45

D. count: 65

E. Compilation fails.

F. The code runs in an endless loop.

34. Given:

         2. public class Organic<E> {
         3.   void react(E e) { }
         4.   static void main(String[] args) {
         5.     // Organic<? extends Organic> compound = new Aliphatic<Organic>();
         6.     // Organic<? super Aliphatic> compound = new Aliphatic<Organic>();
         7.     compound.react(new Organic());
         8.     compound.react(new Aliphatic());
         9.     compound.react(new Hexane());
        10. } }
        11. class Aliphatic<F> extends Organic<F> { }
        12. class Hexane<G> extends Aliphatic<G> { }

Which, taken independently, are true? (Choose all that apply.)

A. If line 5 is uncommented, compilation fails due to an error at line 7.

B. If line 5 is uncommented, compilation fails due to an error at line 8.

C. If line 5 is uncommented, compilation fails due to an error at line 9.

D. If line 6 is uncommented, compilation fails due to an error at line 7.

E. If line 6 is uncommented, compilation fails due to an error at line 8.

F. If line 6 is uncommented, compilation fails due to an error at line 9.

35. Given:

         4. public class Hemlock {
         5.   static StringBuffer sb;
         6.   StringBuffer sb2;
         7.   public static void main(String[] args) {
         8.     sb = sb.append(new Hemlock().go(new StringBuffer("hey")));
         9.     System.out.println(sb);
        10.  }
        11.  { sb2 = new StringBuffer("hi "); }
        12.  StringBuffer go(StringBuffer s) {
        13.    System.out.print(s + " oh " + sb2);
        14.    return new StringBuffer("ey");
        15.  }
        16.  static { sb = new StringBuffer("yo "); }
        17. }

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

QUICK ANSWER KEY

1. F

2. D

3. C

4. G

5. C

6. C, D, E, F

7. A, G

8. B

9. B, E

10. B, D, F

11. A, G

12. B, C, D

13. A, B, C

14. A, B, C, F

15. B, E

16. D, E

17. E

18. D

19. F, H

20. F

21. A

22. C

23. C

24. Drag and Drop

25. A

26. E

27. C, D, E

28. B, C, F

29. D

30. B, C, E

31. B, D

32. D, E, F, G

33. B

34. A, B, C, D

35. E

36. A, C, D, F

37. D

38. B, C, F

39. A, B, F, G

40. C

41. Drag and Drop

42. A

43. D

44. B, E, F

45. C, D, G

46. B

47. D

48. B

49. D

50. D

51. C

52. B, C, F

53. E

54. D

55. B, E

56. A

57. C, D

58. D

59. F

60. D

PRACTICE EXAM 4: ANSWERS

1. Given:

         1. abstract class Vibrate {
         2.   static String s = "-";
         3.   Vibrate() {  s += "v"; }
         4. }
         5. public class Echo extends Vibrate {
         6.   Echo() { this(7); s += "e"; }
         7.   Echo(int x) { s += "e2"; }
         8.   public static void main(String[] args) {
         9.     System.out.print("made " + s + " ");
        10.   }
        11.   static {
        12.     Echo e = new Echo();
        13.     System.out.print("block " + s + " ");
        14. } }

What is the result?

A. made -ve2e

B. block -ee2v

C. block -ve2e

D. made -eve2 block -eve2

E. made -ve2e block -ve2e

F. block -ve2e made -ve2e

G. block -ve2e made -ve2eve2e

H. Compilation fails

2. Given:

         3. public class KaChung {
         4.   public static void main(String[] args) {
         5.     String s = "";
         6.     if(Integer.parseInt("011") == Integer.parseInt("9")) s += 1;
         7.     if(021 == Integer.valueOf("17")) s += 2;
         8.     if(1024 == new Integer(1024)) s += 3;
         9.     System.out.println(s);
        10. } }

What is the result?

A. 2

B. 3

C. 13

D. 23

E. 123

F. Compilation fails.

G. An exception is thrown at runtime.

3. Given the invocation "java GiveUp" and:

         2. public class GiveUp {
         3.   public static void main(String[] args) throws Exception {
         4.     try {
         5.       assert false;
         6.       System.out.print("t ");
         7.     }
         8.     catch (Error e) {
         9.       System.out.print("c ");
        10.       throw new Exception();
        11.     }
        12.     finally { System.out.print("f "); }
        13. } }

What is the result?

A. c

B. c f

C. t f

D. Compilation fails.

E. "c f " followed by an uncaught exception.

4. Given:

         3. import java.util.*;
         4. public class VC {
         5.   public static void main(String[] args) {
         6.     List<Integer> x = new ArrayList<Integer>();
         7.     Integer[] a = {3, 1, 4, 1};
         8.     x = Arrays.asList(a);
         9.     a[3] = 2;
        10.     x.set(0, 7);
        11.     for(Integer i: x) System.out.print(i + " ");
        12.     x.add(9);
        13.     System.out.println(x);
        14. } }

What is the result?

A. Compilation fails.

B. 3 1 4 2 [7, 1, 4, 1]

C. 3 1 4 2 [7, 1, 4, 2]

D. 7 1 4 2 [7, 1, 4, 2]

E. 3 1 4 2 [7, 1, 4, 1, 9]

F. 3 1 4 2 [7, 1, 4, 2, 9]

G. 7 1 4 2, followed by an exception.

H. 3 1 4 2, followed by an exception.

5. Given:

         3. public class Honcho {
         4.   static boolean b1 = false;
         5.   static int z = 7;
         6.   static Long y;
         7.   public static void main(String[] args) {
         8.     for(int i = 0; i < 4; i++)
         9.       go(i);
        10.   }
        11.   static void go(int x) {
        12.     try {
        13.       if((x == 0) && (!b1 && z == 7)) System.out.print("0 ");
        14.       if(x < 2 ^ x < 10) System.out.print("1 ");
        15.       if((x == 2) &&
                    (y == null | (y.longValue() == 0))) System.out.print("2 ");
        16.       if(z <= (x + 4)) System.out.print("3 ");
        17.  }
        18.  catch(Exception e) { System.out.print("e "); }
        19. } }

What is the result?

A. 0 1 2 3

B. 1 e 1 3

C. 0 1 e 1 3

D. 0 1 1 1 1 3

E. 1 1 1 2 1 3

F. 0 1 1 1 2 1 3

G. Compilation fails.

6. Given:

         2. class Chilis {
         3.   Chilis(String c, int h) { color = c; hotness = h; }
         4.   String color;
         5.   int hotness;
         6.   public boolean equals(Object o) {
         7.     Chilis c = (Chilis)o;
         8.     if(this.color == c.color) return true;
         9.     return false;
        10.  }
        11.  public int hashCode() { return (color.length() + hotness); }
        12. }

If instances of class Chilis are to be used as keys in a Map, which are true? (Choose all that apply.)

A. The code will not compile.

B. The hashCode() and equals() contracts have been supported.

C. The equals() method is reflexive, symmetric, and transitive.

D. The Chilis class CANNOT be used to create keys to reliably add and retrieve entries in a Map.

E. If the hashCode() method is not considered, the equals() method could be an override that supports the equals() contract.

F. If the equals() method is not considered, the hashCode() method could be an override that supports the hashCode() contract.

7. Given:

         2. package pack.clients;
         3. import pack.banking.Bank;
         4. public class Client{
         5.   public static void main(String[] args){
         6.     Bank bank = new Bank();
         7.     System.out.println(bank.getMoney(2000L));
         8. } }

And given that Client.java resides in the $ROOT directory and is not yet compiled. There is another class named pack.banking.Bank, which has a method called getMoney(long) that returns a value. Bank class is compiled and deployed into a JAR file called pack.jar, as shown in the following directory structure

Image

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

A. To compile, use javac -cp pack.jar -d . Client.java

B. To compile, use javac -cp pack.jar#pack.banking.Bank -d . Client.java

C. To compile, use javac -cp * -d . Client.java

D. To run, use java -cp pack.jar pack.clients.Client

E. To run, use java -cp . -d pack.jar pack.clients.Client

F. To run, use java -cp . -cp pack.jar pack.clients.Client

G. To run, use java -cp .:pack.jar pack.clients.Client

8. Given:

         2. class Bird {
         3.   public static String s = "";
         4.   public static void fly() { s += "fly "; }
         5. }
         6. public class Hummingbird extends Bird {
         7.   public static void fly() { s += "hover "; }
         8.   public static void main(String[] args) {
         9.     Bird b1 = new Bird();
        10.     Bird b2 = new Hummingbird();
        11.     Bird b3 = (Hummingbird)b2;
        12.     Hummingbird b4 = (Hummingbird)b2;
        13.
        14.     b1.fly();  b2.fly();  b3.fly();  b4.fly();
        15.     System.out.println(s);
        16. } }

What is the result?

A. fly fly fly fly

B. fly fly fly hover

C. fly fly hover hover

D. fly hover hover hover

E. hover hover hover hover

F. Compilation fails.

G. An exception is thrown at runtime.

9. Given two files:

         1. package com;
         2. public class Extramuros {
         3.   public static void howdy() { System.out.print("howdy "); }
         4.   public static final Extramuros ex = new Extramuros();
         5.   public int instVar = 42;
         6.   public enum avout {OROLO, JAD};
         7. }

         1. // insert code here
        …
         6. public class Theorics {
         7.   public static void main(String[] args) {
         8.     Extramuros.howdy();
         9.     System.out.print(Extramuros.avout.OROLO + " ");
        10.     howdy();
        11.     System.out.print(ex.instVar + " ");
        12. } }

Which are the minimum line(s) of code to add at "insert code here" for the files to compile? (Choose all that apply.)

A. import static com.*;

B. import com.Extramuros;

C. import com.Extramuros.*;

D. import static com.Extramuros;

E. import static com.Extramuros.*;

F. Even with correct imports, the code will not compile due to other errors.

10. Given:

         2. import java.text.*;
         3. import java.util.*;
         4. public class Vogue {
         5.   public static void main(String[] args) {
         6.     DateFormat df1 = DateFormat.getInstance();
         7.     DateFormat df2 = DateFormat.getInstance(DateFormat.SHORT);
         8.     DateFormat df3 = DateFormat.getDateInstance(DateFormat.FULL);
         9.     DateFormat df4 = DateFormat.getDateInstance(DateFormat.EXTENDED);
        10. } }

Which are true? (Choose all that apply.)

A. Line 2 is not necessary.

B. Line 3 is not necessary.

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

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.

11. Given:

         2. public class Cult extends Thread {
         3.   static int count = 0;
         4.     public void run() {
         5.       for(int i = 0; i < 100; i++) {
         6.         if(i == 5 && count < 3) {
         7.           Thread t = new Cult(names[count++]);
         8.           t.start();
         9.           // insert code here
        10.         }
        11.         System.out.print(Thread.currentThread().getName() + " ");
        12.      }
        13.    }
        14.    public static void main(String[] args) {
        15.      new Cult("t0").start();
        16.   }
        17.   Cult(String s) { super(s); }
        18.   String[] names = {"t1", "t2", "t3"};
        19. }

And these two fragments:

I. try { t.join(); } catch(Exception e) { }

II. try { Thread.currentThread().join(); } catch(Exception e) { }

When each fragment is inserted independently at line 9, which are true? (Choose all that apply.)

A. With fragment I, t0 completes last.

B. With fragment I, t3 completes last.

C. With fragment II, t0 completes last.

D. With fragment II, t3 completes last.

E. With both fragments, compilation fails.

F. With fragment I, the code never completes.

G. With fragment II, the code never completes.

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

A. If a class’s member’s values can be retrieved, but not changed, without using the class’s API, the class is not cohesive.

B. If a class’s member’s values can be retrieved, but not changed, without using the class’s API, tight coupling could occur.

C. If a class’s member’s values can be retrieved, but not changed, without using the class’s API, the class is not well encapsulated.

D. If a class’s member’s values can be updated only through the use of its API, or by an inner class, the class is well encapsulated.

E. If a class’s member’s values can be updated only through the use of its API, or by an inner class, the class is NOT well encapsulated.

13. Given:

         3. public class BigData {
         4.   static BigData bd;
         5.   public static void main(String[] args) {
         6.     new BigData().doStuff();
         7.     // do lots of memory intensive stuff
          …      // JVM finds an eligible BigData object for GC
          …      // JVM invokes finalize()
          …      // do more stuff
        48.  }
        49.   void doStuff() {  }
        50.  // insert code here
        51.    bd = this;
        52.  }
        53. }
        54. class MyException extends Exception { }

and the following four fragments:

I. protected void finalize() throws Throwable {

II. protected void finalize() {

III. protected void finalize() throws MyException {

IV. void finalize() {

If the fragments are inserted, independently, at line 50, which are true? (Choose all that apply.)

A. Fragment I compiles.

B. Fragment II compiles.

C. Fragment III compiles.

D. Fragment IV compiles.

E. Of those that compile, the GC will collect any given object after the JVM has called finalize() on that object.

F. Because of the way finalize() has been overridden, the GC will never collect eligible objects of type BigData.

14. Given:

         2. interface Clickable {  void click();  }
         3. // insert code here

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

A. interface Keyboard extends Clickable { }

B. interface Keyboard extends Clickable { void click(); }

C. interface Keyboard extends Clickable { void doClick(); }

D. interface Keyboard extends Clickable { void click() { ; } }

E. interface Keyboard extends Clickable { protected void click(); }

F. interface Keyboard extends Clickable { void click(); void doClick(); }

15. Given:

         1. public class Argue {
         2.   static boolean b;
         3.   static int x = 0;
         4.   public static void main(String[] args) {
         5.     int guess = (int)(Math.random() * 5);
         6.     if(guess < 0) assert false;
         7.     assert b = true;
         8.     assert x = 0;
         9.     assert x == 0;
        10. } }

Which are true? (Choose all that apply.)

A. The code compiles.

B. The assert on line 6 is appropriate.

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

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.

16. Given two files:

         1. package com.wickedlysmart2;
         2. public 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. }

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

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

A. do1 do2 do3

B. "do1 ", followed by an exception.

C. Compilation fails due to an error on line 4 of UPS.

D. Compilation fails due to an error on line 5 of UPS.

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

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

17. Given:

         4. class Electricity { int getCharge() { return 24; } }
         5. public class Voltage extends Electricity {
         6.   enum volts {twelve, twentyfour, oneten};
         7.   public static void main(String[] args) {
         8.     volts v = volts.twentyfour;
         9.     switch (v) {
        10.       case twelve:
        11.         System.out.print("12 ");
        12.       default:
        13.         System.out.print(getCharge() + " ");
        14.       case oneten:
        15.         System.out.print("110 ");
        16. } } }

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

A. 24

B. 24 110

C. 24 110 12

D. Compilation fails due to a misuse of enums

E. Compilation fails due to a non-enum issue.

18. Given:

         2. import java.io.*;
         3. public class Uboat {
         4.   public static void main(String[] args) {
         5.     try {
         6.       File f1 = new File("sub1");
         7.       f1.mkdir();
         8.       File f2 = new File(f1,"sub2");
         9.       File f3 = new File(f1,"sub3");
        10.      PrintWriter pw = new PrintWriter(f3);
        11.    } catch (Exception e) { System.out.println("ouch"); }
        12. } }

And, if the code compiles, what is the result if "java Uboat" is invoked TWICE? (Choose all that apply.)

A. Compilation fails.

B. The second invocation produces the output "ouch".

C. The second invocation creates at least one new file as a peer to the new directory.

D. The first invocation creates a new directory and one new file in that directory.

E. The first invocation creates a new directory and two new files in that directory.

F. The first invocation creates a new directory and at least one new file as a peer to it.

19. Given:

         2. class Car {
         3.   private Car() { }
         4.   protected Car(int x) { }
         5. }
         6. public class MG extends Car {
         7.   // MG(int x) { }
         8.   // MG(int x) { super(); }
         9.   // MG(int x) { super(x); }
        10.   // private MG(int x) { super(x); }
        11.   // MG() { }
        12.   // MG() { this(); }
        13.   // MG() { this(6); }
        14.   // MG() { super(7); }
        15.   public static void main(String[] args) {
        16.     new MG(7);
        17.     new MG();
        18. } }

Which sets of constructors can be uncommented for the code to compile? (Choose all that apply.)

A. Line 7 and line 11.

B. Line 7 and line 14.

C. Line 8 and line 12.

D. Line 8 and line 13.

E. Line 9 and line 12.

F. Line 9 and line 13.

G. Line 10 and line 11.

H. Line 10 and line 14.

20. Given:

         2. interface Gadget {
         3.   int patent = 12345;
         4.   Gadget doStuff();
         5. }
         6. public class TimeMachine implements Gadget {
         7.   int patent = 34567;
         8.   public static void main(String[] args) {
         9.     new TimeMachine().doStuff();
        10.  }
        11.  TimeMachine doStuff() {
        12.    System.out.println( ++patent);
        13.    return new TimeMachine();
        14. } }

If javac is invoked twice:

        javac -source 1.4 TimeMachine.java
        javac TimeMachine.java

And, if "java TimeMachine" is invoked whenever TimeMachine compiles, what is the result?

A. First, compilation fails, then 12346

B. First, compilation fails, then 34568

C. First, 12346, then compilation fails.

D. First, 34568, then compilation fails.

E. First, 12346, then 34568

F. Compilation fails on both javac invocations.

21. Given:

         1. import java.util.*;
         2. public class LogSplitter {
         3.   public static void main(String[] args) {
         4.     for(int x = 1; x < args.length; x++)
         5.       System.out.print(args[0].split(args[x]).length + " ");
         6. } }

And the command-line invocation:

         java LogSplitter "x1 23 y #" "d" "s" "w"

What is the result?

A. 4 4 6

B. 3 3 6

C. 5 4 6

D. 4 6 4

E. 3 6 3

F. 5 6 4

G. Compilation fails.

H. An exception is thrown at runtime.

22. Given this code in a method:

         3.  int y, count = 0;
         4.  for(int x = 3; x < 6; x++) {
         5.    try {
         6.      switch(x) {
         7.        case 3: count++;
         8.        case 4: count++;
         9.        case 7: count++;
        10.       case 9: { y = 7 / (x - 4); count += 10; }
        11.      }
        12.    } catch (Exception ex) { count++; }
        13.  }
        14.  System.out.println(count);

What is the result?

A. 2

B. 15

C. 16

D. 25

E. 26

F. Compilation fails.

G. An exception is thrown with no other output.

23. Given:

         5. abstract class Thing { static String s = ""; Thing() { s += "t "; } }
         6. class Steel extends Thing {
         7.   Steel() { s += "s "; }
         8.   Steel(String s1) {
         9.     s += s1;
        10.    new Steel();
        11.  }
        12. }
        13. public class Tungsten extends Steel {
        14.   Tungsten(String s1) {
        15.     s += s1;
        16.     new Steel(s);
        17.  }
        18.  public static void main(String[] args) {
        19.    new Tungsten("tu ");
        20.    System.out.println(s);
        21. } }

What is the result?

A. s tu s tu s

B. t s tu t s t s

C. t s tu t t s tu t s

D. t tu t s tu t t tu t s tu t s

E. Compilation fails.

F. An exception is thrown at runtime.

24. You want to add a capability to MyStuff so that collections of MyStuff objects can be sorted in a “natural” order. Use the fragments to fill in the blanks to create this capability. Note: Not all the fragments will be used, not all the blanks have to be filled (maybe), and you can use each fragment more than once

Second note: As in the real exam’s Drag-and-Drop questions, there may be more than one correct answer. Here, give yourself bonus points if you create more than one correct answer. In the real exam, you’ll get full credit for ANY correct answer.

         import java.util.*; class MyStuff  _______________  _____________________________ {
         MyStuff(String n, int v) { name = n; value = v; }
           String name; int value;
           ____________  _________  ________________  _____________________  {
           return __________________  ___________________ ;
           }
           public String toString() { return name + " " + value + " "; }
       }

Fragments:

Image

25. Given:

         2. public class Swanky {
         3.   Swanky s;
         4.   public static void main(String[] args) {
         5.     Swanky s1 = new Swanky();
         6.     s1.s = new Swanky();
         7.     go(s1.s);
         8.     Swanky s2 = go(s1);
         9.     if(s2.s.equals(s1)) System.out.print("1 ");
        10.    if(s2.equals(s1)) System.out.print("2 ");
        11.    if(s1.s.equals(s1)) System.out.print("3 ");
        12.    if(s2.s.equals(s2)) System.out.print("4 ");
        13.  }
        14.  static Swanky go(Swanky s) {
        15.    Swanky gs = new Swanky();
        16.    gs.s = s;
        17.    return gs;
        18. } }

What is the result?

A. 1

B. 1 2

C. 1 2 4

D. Compilation fails.

E. "1 ", followed by an exception.

F. "1 2 ", followed by an exception.

26. Given:

         2. import java.util.*;
         3. public class AndOver {
         4.   public static void main(String[] args) {
         5.     List g = new ArrayList();

         6.     g.add(new Gaited("Eyra"));
         7.     g.add(new Gaited("Vafi"));
         8.     g.add(new Gaited("Andi"));
         9.     Iterator i2 = g.iterator();
        10.    while(i2.hasNext()) {
        11.      System.out.print(i2.next().name + " ");
        12. } } }
        13. class Gaited {
        14.   public String name;
        15.   Gaited(String n) { name = n; }
        16. }

What is the result?

A. Vafi Andi

B. Andi Eyra Vafi

C. Andi Vafi Eyra

D. Eyra Vafi Andi

E. Compilation fails.

F. The output order is unpredictable.

27. Given:

         2. interface Plant {
         3.   int greenness = 7;
         4.   void grow();
         5. }
         6. class Grass implements Plant {
         7.   // static int greenness = 5;
         8.   // int greenness = 5;
         9.   public static void main(String[] args) {
        10.    int greenness = 2;
        11.    new Grass().grow();
        12.  }
        13.  public void grow() {
        14.    System.out.println(++greenness);
        15. } }

Which are true? (Choose all that apply.)

A. As the code stands, the output is 3.

B. As the code stands, the output is 8.

C. As the code stands, it will NOT compile.

D. If line 7 is un-commented, the output is 6.

E. If line 8 is un-commented, the output is 6.

F. If line 7 is un-commented, the output is 8.

G. If line 7 is un-commented, the code will NOT compile.

28. Given:

         2. class Grab {
         3.   static int x = 5;
         4.   synchronized void adjust(Grab y) {
         5.     System.out.print(x-- + " ");
         6.     y.view(y);
         7.   }
         8.   synchronized void view(Grab z) { if(x > 0) z.adjust(z); }
         9. }
        10. public class Grapple implements Runnable {
        11.   static Thread t1;
        12.   static Grab g, g2;
        13.   public void run() {
        14.     if(Thread.currentThread().getId() == t1.getId()) g.adjust(g2);
        15.     else g2.view(g);
        16.  }
        17.  public static void main(String[] args) {
        18.    g = new Grab();
        19.    g2 = new Grab();
        20.    t1 = new Thread(new Grapple());
        21.    t1.start();
        22.    new Thread(new Grapple()).start();
        23. } }

Which are true? (Choose all that apply.)

A. Compilation fails.

B. The output could be 5 4 3 2 1

C. The output could be 5 4 3 2 1 0

D. The program could produce thousands of lines of output.

E. The program could deadlock before producing any output.

F. The output could be "5 ", followed by the program deadlocking.

29. Given:

         3. import java.util.*;
         4. public class Corner {
         5.   public static void main(String[] args) {
         6.     TreeSet<String> t1 = new TreeSet<String>();
         7.     TreeSet<String> t2 = new TreeSet<String>();
         8.     t1.add("b");  t1.add("7");
         9.     t2 = (TreeSet)t1.subSet("5", "c");
        10.     try {
        11.       t1.add("d");
        12.       t2.add("6");
        13.       t2.add("3");
        14.    }
        15.    catch (Exception e) { System.out.print("ex "); }
        16.    System.out.println(t1 + " " + t2);
        17. } }

What is the result?

A. [6, 7, b] [6, 7, b]

B. [6, 7, b, d] [6, 7, b]

C. ex [6, 7, b] [6, 7, b]

D. ex [6, 7, b, d] [6, 7, b]

E. [3, 6, 7, b, d] [6, 7, b]

F. ex [6, 7, b, d] [6, 7, b, d]

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

30. Given the current directory is bigApp, and the directory structure:

Image

And the three files:

         public class Cloned {
           public static void main(String[] args) { System.out.println("classes"); }
         }
         public class Cloned {
           public static void main(String[] args) { System.out.println("com"); }
         }
         public class Cloned {
           public static void main(String[] args) { System.out.println("ws"); }
        }

Have been compiled into the classes, com, and wickedlysmart directories, respectively. Which will produce the output "ws"? (Choose all that apply.)

A. java -cp wickedlysmart:. Cloned

B. java -cp classes/com/wickedlysmart Cloned

C. java -cp classes/com/wickedlysmart:classes Cloned

D. java -cp classes:classes/com/wickedlysmart Cloned

E. java -cp .:classes/com/wickedlysmart:classes Cloned

F. java -cp .:classes/com:classes/com/wickedlysmart Cloned

31. Given:

         1. import java.io.*;
         2. public class Edgy {
         3.   public static void main(String[] args) {
         4.     try {
         5.       wow();
         6.       // throw new IOException();
         7.  } finally {
         8.       // throw new Error();
         9.       // throw new IOException();
        10.    }
        11.  }
        12.  static void wow() {
        13.      // throw new IllegalArgumentException();
        14.      // throw new IOException();
        15. } }

And given that IOException is a direct subclass of java.lang.Exception, and that IllegalArgumentException is a runtime exception, which of the following, if uncommented independently, will compile? (Choose all that apply.)

A. Line 6

B. Line 8

C. Line 9

D. Line 13

E. Line 14

F. The code will NOT compile as is.

32. Given two files:

         1. package com;
         2. public class MyClass {
         3.   public static void howdy() { System.out.print("howdy "); }
         4.   public static final int myConstant = 343;
         5.   public static final MyClass mc = new MyClass();
         6.   public int instVar = 42;
         7. }
        11. import com.MyClass;
        12. public class TestImports2 {
        13.   public static void main(String[] args) {
        14.   MyClass.howdy();
        15.   System.out.print(MyClass.myConstant + " ");
        16.   System.out.print(myConstant + " ");
        17.   howdy();
        18.   System.out.print(mc.instVar + " ");
        19.   System.out.print(instVar + " ");
        20. } }

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

A. howdy 343 343 howdy 42 42

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

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

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

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

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

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

33. Given this code inside a method:

        13.  int count = 0;
        14.  outer:
        15.  for(int x = 0; x < 5; x++) {

        16.  middle:
        17.  for(int y = 0; y < 5; y++) {
        18.    if(y == 1) continue middle;
        19.    if(y == 3) break middle;
        20.    count++;
        21.  }
        22.  if(x > 2) continue outer;
        23.     count = count + 10;
        24.  }
        25.  System.out.println("count: " + count);

What is the result?

A. count: 33

B. count: 40

C. count: 45

D. count: 65

E. Compilation fails.

F. The code runs in an endless loop.

34. Given:

         2. public class Organic<E> {
         3.   void react(E e) { }
         4.   static void main(String[] args) {
         5.     // Organic<? extends Organic> compound = new Aliphatic<Organic>();
         6.     // Organic<? super Aliphatic> compound = new Aliphatic<Organic>();
         7.     compound.react(new Organic());
         8.     compound.react(new Aliphatic());
         9.     compound.react(new Hexane());
        10. } }
        11. class Aliphatic<F> extends Organic<F> { }
        12. class Hexane<G> extends Aliphatic<G> { }

Which, taken independently, are true? (Choose all that apply.)

A. If line 5 is uncommented, compilation fails due to an error at line 7.

B. If line 5 is uncommented, compilation fails due to an error at line 8.

C. If line 5 is uncommented, compilation fails due to an error at line 9.

D. If line 6 is uncommented, compilation fails due to an error at line 7.

E. If line 6 is uncommented, compilation fails due to an error at line 8.

F. If line 6 is uncommented, compilation fails due to an error at line 9.

35. Given:

         4. public class Hemlock {
         5.   static StringBuffer sb;
         6.   StringBuffer sb2;
         7.   public static void main(String[] args) {
         8.     sb = sb.append(new Hemlock().go(new StringBuffer("hey")));
         9.     System.out.println(sb);
        10.  }
        11.  { sb2 = new StringBuffer("hi "); }
        12.  StringBuffer go(StringBuffer s) {
        13.    System.out.print(s + " oh " + sb2);
        14.    return new StringBuffer("ey");
        15.  }
        16.  static { sb = new StringBuffer("yo "); }
        17. }

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

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