PRACTICE EXAM 3

This practice 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:

          3. class Bonds {
          4.   Bonds force() { return new Bonds(); }
          5. }
          6. public class Covalent extends Bonds {
          7.   Covalent force() { return new Covalent(); }
          8.   public static void main(String[] args) {
          9.     new Covalent().go(new Covalent());
         10.   }
         11.   void go(Covalent c) {
         12.     go2(new Bonds().force(), c.force());
         13.   }
         14.   void go2(Bonds b, Covalent c) {
         15.     Covalent c2 = (Covalent)b;
         16.     Bonds b2 = (Bonds)c;
         17. } }

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

A. A ClassCastException is thrown at line 15.

B. A ClassCastException is thrown at line 16.

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

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

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

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

2. Given:

          1. import java.util.*;
          2. public class Drawers {
          3.   public static void main(String[] args) {
          4.     List<String> desk = new ArrayList<String>();
          5.     desk.add("pen"); desk.add("scissors"); desk.add("redStapler");
          6.     System.out.print(desk.indexOf("redStapler"));
          7.     Collection.reverse(desk);
          8.     System.out.print(" " + desk.indexOf("redStapler"));
          9.     Collection.sort(desk);
         10.     System.out.println(" " + desk.indexOf("redStapler"));
         11. } }

What is the result?

A. 1 1 1

B. 2 0 1

C. 2 0 2

D. 2 2 2

E. Compilation fails.

F. An exception is thrown at runtime.

3. Given:

          1. import java.util.*;
          2. class Radio {
          3.   String getFreq() { return "97.3"; }
          4.   static String getF() { return "97.3"; }
          5. }
          6. class Ham extends Radio {
          7.   String getFreq() { return "50.1"; }
          8.   static String getF() { return "50.1"; }
          9.   public static void main(String[] args) {
         10.     List<Radio> radios = new ArrayList<Radio>();
         11.     radios.add(new Radio());
         12.     radios.add(new Ham());
         13.     for(Radio r: radios)
         14.       System.out.print(r.getFreq() + " " + r.getF() + "  ");
         15. } }

What is the result?

A. 50.1 50.1 50.1 50.1

B. 50.1 97.3 50.1 97.3

C. 97.3 50.1 50.1 50.1

D. 97.3 97.3 50.1 50.1

E. 97.3 97.3 50.1 97.3

F. 97.3 97.3 97.3 97.3

G. Compilation fails.

H. An exception is thrown at runtime.

4. 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 static com.MyClass.*;
          12. public class TestImports {
          13.  public static void main(String[] args) {
          14.  System.out.print(myConstant + " ");
          15.  howdy();
          16.  System.out.print(mc.instVar + " ");
          17.  System.out.print(instVar + " ");
          18. } }

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

A. 343 howdy 42 42

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

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

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

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

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

5. Given this code in a method:

          4.  int x = 0;
          5.  int[] primes = {1,2,3,5};
          6.  for(int i: primes)
          7.    switch(i) {
          8.      case 1: x += i;
          9.      case 5: x += i;
         10.      default: x += i;
         11.      case 2: x += i;
         12.    }
         13.  System.out.println(x);

What is the result?

A. 11

B. 13

C. 24

D. 27

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

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

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

6. Given:

          1. import java.util.*;
          2. public class Elway {
          3.   public static void main(String[] args) {
          4.     ArrayList[] ls = new ArrayList[3];
          5.     for(int i = 0; i < 3; i++) {
          6.       ls[i] = new ArrayList();
          7.       ls[i].add("a" + i);
          8.     }
          9.     Object o = ls;
         10.     do3(ls);
         11.     for(int i = 0; i < 3; i++) {
         12.       // insert code here
         13.     }
         14.  }
         15.  static Object do3(ArrayList[] a) {
         16.    for(int i = 0; i < 3; i++)  a[i].add("e");
         17.    return a;
         18. } }

And the following fragments:

I. System.out.print(o[i] + " ");

II. System.out.print((ArrayList[])[i] + " ");

III. System.out.print( ((Object[])o)[i] + " ");

IV. System.out.print(((ArrayList[])o)[i] + " ");

If the fragments are added to line 12, independently, which are true? (Choose all that apply.)

A. Fragment I will compile.

B. Fragment II will compile.

C. Fragment III will compile.

D. Fragment IV will compile.

E. Compilation fails due to other errors.

F. Of those that compile, the output will be: [a0] [a1] [a2]

G. Of those that compile, the output will be: [a0, e] [a1, e] [a2, e]

7. Given:

          1. enum MyEnum {HI, ALOHA, HOWDY};
          2. public class PassEnum {
          3.   public static void main(String[] args) {
          4.     PassEnum p = new PassEnum();
          5.     MyEnum[] v = MyEnum.values();|
          6.     v = MyEnum.getValues();
          7.     for(MyEnum me: MyEnum.values())  p.getEnum(me);
          8.     for(int x = 0; x < MyEnum.values().length; x++)  p.getEnum(v[x]);
          9.     for(int x = 0; x < MyEnum.length; x++)  p.getEnum(v[x]);
         10.     for(MyEnum me: v)  p.getEnum(me);
         11.  }
         12.  public void getEnum(MyEnum e) {
         13.    System.out.print(e + " ");
         14. } }

Which line(s) of code will cause a compiler error? (Choose all that apply.)

A. line 1

B. line 5

C. line 6

D. line 7

E. line 8

F. line 9

G. line 10

H. line 12

8. Given:

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

Which are true? (Choose all that apply.)

A. Compilation fails.

B. An exception is thrown at runtime.

C. Loop #1 will run most of its iterations before Loop #2.

D. Loop #2 will run most of its iterations before Loop #1.

E. There is no way to predict which loop will mostly run first.

9. Given this code in a method:

          5.  int x = 3;
          6.  for(int i = 0; i < 3; i++) {
          7.    if(i == 1) x = x++;
          8.    if(i % 2 == 0 && x % 2 == 0) System.out.print(".");
          9.    if(i % 2 == 0 && x % 2 == 1) System.out.print("-");
         10.    if(i == 2 ^ x == 4) System.out.print(",");
         11.  }
         12.  System.out.println("<");

What is the result?

A. -.,<

B. --,<

C. -.,,<

D. --,,<

E. -.-,,<

F. Compilation fails.

10. Given:

          2. public class Incomplete {
          3.   public static void main(String[] args) {  // change code here ?
          4.     // insert code here ?
          5.       new Incomplete().doStuff();
          6.     // insert code here ?
        ...
         10.   }
         11.   static void doStuff() throws Exception {
         12.     throw new Exception();
         13. } }

Which are true? (Choose all that apply.)

A. Compilation succeeds with no code changes.

B. Compilation succeeds if main() is changed to throw an Exception.

C. Compilation succeeds if a try-catch is added, surrounding line 5.

D. Compilation succeeds if a try-finally is added, surrounding line 5.

E. Compilation succeeds only if BOTH main() declares an Exception AND a try-catch is added, surrounding line 5.

11. Given the proper imports, and given:

          24.  Date d1 = new Date();
          25.  Date d2 = d1;
          26.  System.out.println(d1);
          27.  d2.setTime(d1.getTime() + (7 * 24 * 60 * 60));
          28.  System.out.println(d2);

Which are true? (Choose all that apply.)

A. Compilation fails.

B. An exception is thrown at runtime.

C. Some of the output will be today’s date.

D. Some of the output will be next week’s date.

E. Some of the output will represent the Java “epoch” (i.e., January 1, 1970).

12. Given:

          2. interface Machine { }
          3. interface Engine { }
          4. abstract interface Tractor extends Machine, Engine {
          5.   void pullStuff();
          6. }
          7. class Deere implements Tractor {
          8.   public void pullStuff() { System.out.print("pulling "); }
          9. }
         10. class LT255 implements Tractor extends Deere  {
         11.   public void pullStuff() { System.out.print("pulling harder "); }
         12. }
         13. public class LT155 extends Deere implements Tractor, Engine { }

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

A. Compilation succeeds.

B. Compilation fails because of error(s) in Tractor.

C. Compilation fails because of error(s) in Deere.

D. Compilation fails because of error(s) in LT255.

E. Compilation fails because of error(s) in LT155.

13. Given this code in a method:

          5.  boolean[] ba = {true, false};
          6.  short[][] gr = {{1,2}, {3,4}};
          7.  int i = 0;
          8.  for( ; i < 10; ) i++;
          9.  for(short s: gr) ;
         10.  for(int j = 0, k = 10; k > j; ++j, k--) ;
         11.  for(int j = 0; j < 3; System.out.println(j++)) ;
         12.  for(Boolean b: ba) ;

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

A. Compilation succeeds.

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

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

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

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

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

14. Given:

          2. package w.x;
          3. import w.y.Zoom;
          4. public class Zip {
          5.   public static void main(String[] args) {
          6.     new Zoom().doStuff();
          7. } }

And the following command to compile Zip.java: javac -cp w.jar -d . Zip.java

And another class w.y.Zoom (with a doStuff() method), which has been compiled and deployed into a JAR file w.jar:

Fill in the blanks using the following fragments to show the directory structure and the contents of any directory(s) necessary to compile successfully from $ROOT. Note: NOT all the blanks must be filled, NOT all the fragments will be used, and fragments CANNOT be used more than once.

image

Fragments:

image

15. Given:

          2. class Pancake { }
          3. class BlueberryPancake extends Pancake { }
          4. public class SourdoughBlueberryPancake2 extends BlueberryPancake {
          5.   public static void main(String[] args) {
          6.   Pancake p4 = new SourdoughBlueberryPancake2();
          7.   // insert code here
          8. } }

And the following six declarations (which are to be inserted independently at line 7):

I. Pancake p5 = p4;

II. Pancake p6 = (BlueberryPancake)p4;

III. BlueberryPancake b2 = (BlueberryPancake)p4;

IV. BlueberryPancake b3 = (SourdoughBlueberryPancake2)p4;

V. SourdoughBlueberryPancake2 s1 = (BlueberryPancake)p4;

VI. SourdoughBlueberryPancake2 s2 = (SourdoughBlueberryPancake2)p4;

Which are true? (Choose all that apply.)

A. All six declarations will compile.

B. Exactly one declaration will not compile.

C. More than one of the declarations will not compile.

D. Of those declarations that compile, none will throw an exception.

E. Of those declarations that compile, exactly one will throw an exception.

F. Of those declarations that compile, more than one will throw an exception.

16. Given:

          3. class IcelandicHorse {
          4.   void tolt() { System.out.print("4-beat "); }
          5. }
          6. public class Vafi extends IcelandicHorse {
          7.   public static void main(String[] args) {
          8.     new Vafi().go();
          9.     new IcelandicHorse().tolt();
         10.   }
         11.   void go() {
         12.     IcelandicHorse h1 = new Vafi();
         13.     h1.tolt();
         14.     Vafi v = (Vafi) h1;
         15.     v.tolt();
         16.   }
         17.   void tolt() { System.out.print("pacey "); }
         18. }

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

A. 4-beat pacey pacey

B. pacey pacey 4-beat

C. 4-beat 4-beat 4-beat

D. 4-beat pacey 4-beat

E. pacey, followed by an exception

F. 4-beat, followed by an exception

17. Given:

          1. class MyException extends RuntimeException { }
          2. public class Houdini {
          3.   public static void main(String[] args) throws Exception {
          4.     throw new MyException();
          5.     System.out.println("success");
          6. } }

Which are true? (Choose all that apply.)

A. The code runs without output.

B. The output "success" is produced.

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

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

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

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

18. Given:

        3. public class Avast {
        4.   static public void main(String[] scurvy) {
        5.     System.out.print(scurvy[1] + " ");
        6.     main(scurvy[2]);
        7.   }
        8.   public static void main(String dogs) {
        9.     assert(dogs == null);
       10.     System.out.println(dogs);
       11. } }

And, if the code compiles, the command-line invocation:

java Avast -ea 1 2 3

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

A. 1 2

B. 2 3

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

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

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

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

G. Some output is produced and then an AssertionError is thrown.

19. Given:

          2. class Wheel {
          3.   Wheel(int s) { size = s; }
          4.   int size;
          5.   void spin() { System.out.print(size + " inch wheel spinning, "); }
          6. }
          7. public class Bicycle {
          8.   public static void main(String[] args) {
          9.     Wheel[] wa = {new Wheel(15), new Wheel(17)};
         10.     for(Wheel w: wa)
         11.       w.spin();
         12. } }

Which are true? (Choose all that apply.)

A. Compilation fails.

B. If size was private, the degree of coupling would change.

C. If size was private, the degree of cohesion would change.

D. The Bicycle class is tightly coupled with the Wheel class.

E. The Bicycle class is loosely coupled with the Wheel class.

F. If size was private, the degree of encapsulation would change.

20. Given:

          2. public class Mouthwash {
          3.   static int x = 1;
          4.   public static void main(String[] args) {
          5.     int x = 2;
          6.     for(int i=0; i< 3; i++) {
          7.       if(i==1) System.out.print(x + " ");
          8.     }
          9.     go();
         10.     System.out.print(x + " " + i);
         11.  }
         12.  static void go() { int x = 3; }
         13. }

What is the result?

A. 1 2 2

B. 2 2 2

C. 2 2 3

D. 2 3 2

E. Compilation fails.

F. An exception is thrown at runtime.

21. Given the proper imports, and given:

          23.  String s = "123 888888 x 345 -45";
          24.  Scanner sc = new Scanner(s);
          25.  while(sc.hasNext())
          26.    if(sc.hasNextShort())
          27.      System.out.print(sc.nextShort() + " ");

What is the result?

A. The output is 123 345

B. The output is 123 345 -45

C. The output is 123 888888 345 -45

D. The output is 123 followed by an exception.

E. The output is 123 followed by an infinite loop.

22. Given:

          1. interface Horse { public void nicker(); }

Which will compile? (Choose all that apply.)

A. public class Eyra implements Horse { public void nicker() { } }

B. public class Eyra implements Horse { public void nicker(int x) { } }

C. public class Eyra implements Horse {

public void nicker() { System.out.println("huhuhuhuh..."); }

}

D. public abstract class Eyra implements Horse {

public void nicker(int loud) { }

}

E. public abstract class Eyra implements Horse {

public void nicker(int loud) ;

}

23. Given:

          2. public class LaoTzu extends Philosopher {
          3.   public static void main(String[] args) {
          4.     new LaoTzu();
          5.     new LaoTzu("Tigger");
          6.   }
          7.   LaoTzu() { this("Pooh"); }
          8.   LaoTzu(String s) { super(s); }
          9. }
         10. class Philosopher {
         11.   Philosopher(String s) { System.out.print(s + " "); }
         12. }

What is the result?

A. Pooh Pooh

B. Pooh Tigger

C. Tigger Pooh

D. Tigger Tigger

E. Compilation fails due to a single error in the code.

F. Compilation fails due to multiple errors in the code.

24. Which are capabilities of Java’s assertion mechanism? (Choose all that apply.)

A. You can, at the command line, enable assertions for a specific class.

B. You can, at the command line, disable assertions for a specific package.

C. You can, at runtime, enable assertions for any version of Java.

D. It’s considered appropriate to catch and handle an AssertionError programmatically.

E. You can programmatically test whether assertions have been enabled without throwing an AssertionError.

25. Given:

          4. public class Stone implements Runnable {
          5.   static int id = 1;
          6.   public void run() {
          7.     try {
          8.       id = 1 - id;
          9.       if(id == 0) { pick(); } else { release(); }
         10.     } catch(Exception e) { }
         11.   }
         12.   private static synchronized void pick() throws Exception {
         13.     System.out.print("P ");  System.out.print("Q ");
         14.   }
         15.   private synchronized void release() throws Exception {
         16.     System.out.print("R ");  System.out.print("S ");
         17.   }
         18.   public static void main(String[] args) {
         19.     Stone st = new Stone();
         20.     new Thread(st).start();
         21.     new Thread(st).start();
         22.  } }

Which are true? (Choose all that apply.)

A. The output could be P Q R S

B. The output could be P R S Q

C. The output could be P R Q S

D. The output could be P Q P Q

E. The program could cause a deadlock.

F. Compilation fails.

26. Given:

          3. public class States {
          4.   static String s;
          5.   static Boolean b;
          6.   static Boolean t1() { return new Boolean("howdy"); }
          7.   static boolean t2() { return new Boolean(s); }
          8.   public static void main(String[] args) {
          9.     if(t1()) System.out.print("t1 ");
         10.     if(!t2()) System.out.print("t2 ");
         11.     if(t1() != t2()) System.out.print("!= ");
         12.  }
         13. }

Which are true? (Choose all that apply.)

A. Compilation fails.

B. No output is produced.

C. The output will contain "t1 "

D. The output will contain "t2 "

E. The output will contain "!= "

F. The output is "t1 ", followed by an exception.

27. Which are valid command-line switches when working with assertions? (Choose all that apply.)

A. -ea

B. -da

C. -dsa

D. -eva

E. -enableassertions

28. Given:

          4. class Account { Long acctNum, password; }
          5. public class Banker {
          6.   public static void main(String[] args) {
          7.     new Banker().go();
          8.     // do more stuff
          9.   }
         10.   void go() {
         11.     Account a1 = new Account();
         12.     a1.acctNum = new Long("1024");
         13.     Account a2 = a1;
         14.     Account a3 = a2;
         15.     a3.password = a1.acctNum.longValue();
         16.     a2.password = 4455L;
         17. } }

When line 8 is reached, which are true? (Choose all that apply.)

A. a1.acctNum == a3.password

B. a1.password == a2.password

C. Three objects are eligible for garbage collection.

D. Four objects are eligible for garbage collection.

E. Six objects are eligible for garbage collection.

F. Less than three objects are eligible for garbage collection.

G. More than six objects are eligible for garbage collection.

29. Given:

          2. public class Coyote {
          3.   public static void main(String[] args) {
          4.     int x = 4;
          5.     int y = 4;
          6.     while((x = jump(x)) < 8)
          7.       do {
          8.         System.out.print(x + " ");
          9.       } while ((y = jump(y)) < 6);
         10.   }
         11.   static int jump(int x) { return ++x; }
         12. }

What is the result?

A. 5 5 6 6

B. 5 5 6 7

C. 5 5 6 6 7

D. 5 6 5 6 7

E. Compilation fails due to a single error.

F. Compilation fails due to multiple errors.

30. Given:

          2. class Engine {
          3.   public class Piston {
          4.     static int count = 0;
          5.     void go() { System.out.print(" pump " + ++count); }
          6.   }
          7.   public Piston getPiston() { return new Piston(); }
          8. }
          9. public class Auto {
         10.   public static void main(String[] args) {
         11.     Engine e = new Engine();
         12.     // Engine.Piston p = e.getPiston();
         13.     e.Piston p = e.getPiston();
         14.     p.go();  p.go();
         15. } }

In order for the code to compile and produce the output " pump 1 pump 2", which are true? (Choose all that apply.)

A. The code is correct as it stands.

B. Line 4 must be changed. Count can’t be declared "static".

C. Line 12 must be un-commented, and line 13 must be removed.

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

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

31. Given:

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

Which are true? (Choose all that apply.)

A. Compilation fails.

B. No output is produced.

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

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

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

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

32. Given:

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

And the following four code fragments:

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

II. void getDeptName(Department d) { ; }

III. void getDeptName(long x) throws NullPointerException {

throw new NullPointerException();

}

IV. Department getDeptName() throws NullPointerException {

throw new NullPointerException();

return new Department();

}

Which are true? (Choose all that apply.)

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

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

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

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

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

33. Given:

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

Which are true? (Choose all that apply.)

A. Compilation succeeds.

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

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

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

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

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

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

34. Given:

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

What is the result?

A. 11

B. 21

C. 22

D. Compilation fails.

E. A StackOverflowError is thrown at runtime.

F. An ArrayIndexOutOfBoundsException is thrown at runtime.

35. Given:

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

What is the result?

A. 1

B. 1 3

C. 1 2 3

D. 1 3 4

E. 1 2 3 4

F. Compilation fails.

G. An exception is thrown at runtime.

36. Given:

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

What is the result?

A. do1 exc

B. do1 do2 exc

C. do1 do2 do3

D. Compilation fails.

E. An exception is thrown at runtime.

37. Given that FileNotFoundException extends IOException and given:

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

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

A. An IOException is thrown at runtime.

B. A NullPointerException is thrown at runtime.

C. An IllegalArgumentException is thrown at runtime.

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

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

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

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

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

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

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

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

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

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

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

39. Given:

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

What is the result?

A. Compilation fails.

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

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

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

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

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

40. Given:

          2. public class Boggy {
          3.   final static int mine = 7;
          4.   final static Integer i = 57;
          5.   public static void main(String[] args) {
          6.     int x = go(mine);
          7.     System.out.print(mine + " " + x + " ");
          8.     x += mine;
          9.     Integer i2 = i;
         10.     i2 = go(i);
         11.     System.out.println(x + " " + i2);
         12.     i2 = new Integer(60);
         13.   }
         14.   static int go(int x) { return ++x; }
         15. }

What is the result?

A. 7 7 14 57

B. 7 8 14 57

C. 7 8 15 57

D. 7 8 15 58

E. 7 8 16 58

F. Compilation fails.

G. An exception is thrown at runtime.

41. Given:

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

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

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

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

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

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

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

F. Compilation fails due to errors on multiple lines.

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

42. Given:

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

Which are true? (Choose all that apply.)

A. Compilation fails.

B. No output is produced.

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

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

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

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

43. Given:

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

What is the most likely result?

A. Compilation fails.

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

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

D. [Bob], followed by an exception.

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

44. Given the proper imports, and given:

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

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

A. Compilation fails.

B. An exception is thrown at runtime.

C. The output could contain "123 . 46"

D. The output could contain "123 . 456"

E. The output could contain "$123.46"

45. Given:

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

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

A. Compilation fails.

B. An exception is thrown at runtime.

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

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

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

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

46. Given:

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

What is the result?

A. Compilation fails.

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

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

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

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

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

47. Given:

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

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

A. [a, b, 3, d]

B. [d, a, b, 3]

C. [3, a, b, d]

D. [a, b, d, 3]

E. [1, 3, 4, b]

F. [b, 1, 3, 4]

G. 3-a-b-d-

H. a-b-d-3-

I. a-d-3-b-

48. Given this code in a method:

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

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

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

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

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

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

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

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

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

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

Which are true? (Choose all that apply.)

A. Compilation fails.

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

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

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

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

50. Given:

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

What is the most likely result?

A. [a, f]

B. [a, f, a]

C. [a, f, c]

D. [a, f, a, c]

E. Compilation fails.

F. An exception is thrown at runtime.

51. Given:

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

Which are true? (Choose all that apply.)

A. Compilation fails.

B. main() has-a List

C. MyStuff has-a List

D. MyStuff has-a Heroes

E. The output is "ENDER"

F. The output is "Heroes.ENDER"

G. An exception is thrown at runtime.

52. Given:

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

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

A. 12

B. 22

C. 122

D. 233

E. 244

F. 566

G. Compilation fails.

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

Code:

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

Fragments:

image

54. Given:

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

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

A. st

B. in

C. i3

D. The object created on line 5.

E. The object created on line 9.

F. The object created on line 10.

G. The object created on line 11.

55. Given:

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

What is the result?

A. fore fore

B. fore swish

C. bounce swish

D. bounce bounce

E. Compilation fails.

F. An exception is thrown at runtime.

56. Given the following three files:

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

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

image

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

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

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

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

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

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

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

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

57. Given:

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

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

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

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

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

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

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

58. Given:

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

What is the most likely result?

A. 2 0 1 0 1 true

B. 2 0 1 0 1 false

C. Compilation fails.

D. An exception is thrown at line 8.

E. An exception is thrown at line 13.

F. An exception is thrown at line 14.

G. An exception is thrown at line 19.

59. Given:

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

And, if the code compiles, the invocation:

java Begin

What is the result?

A. Compilation fails.

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

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

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

E. ExceptionInInitializerError is thrown before any output.

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

60. Given:

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

And, if the code compiles, the invocation:

         java -ea Alamo y

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

A. ae

B. ae2

C. kate

D. jane

E. AssertionError

F. IllegalArgumentException

G. There is no output because compilation fails.

QUICK ANSWER KEY

1. A

2. E

3. E

4. F

5. D

6. C, D, G

7. C, F

8. A

9. B

10. B, C

11. C

12. D

13. C

14. Drag and Drop

15. B, D

16. B

17. F

18. A

19. E, F

20. E

21. E

22. A, C, D

23. B

24. A, B, E

25. A, B, C

26. D

27. A, B, C, E

28. B, D

29. B

30. B, C

31. B

32. A, B, C, E

33. D

34. E

35. B

36. D

37. C

38. E, F

39. A

40. D

41. E

42. C

43. E

44. A

45. E

46. F

47. A, G

48. C, D, F

49. E

50. D

51. D, E

52. A, B, C, D, E, F

53. Drag and Drop

54. D, E, F

55. F

56. A, C, F

57. A, B

58. B

59. C

60. F

PRACTICE EXAM 3: ANSWERS

1. Given:

          3.   class Bonds {
          4.     Bonds force() { return new Bonds(); }
          5.   }
          6.   public class Covalent extends Bonds {
          7.     Covalent force() { return new Covalent(); }
          8.     public static void main(String[] args) {
          9.       new Covalent().go(new Covalent());
         10.     }
         11.   void go(Covalent c) {
         12.   go2(new Bonds().force(), c.force());
         13.   }
         14.   void go2(Bonds b, Covalent c) {
         15.   Covalent c2 = (Covalent)b;
         16.   Bonds b2 = (Bonds)c;
         17. }  }

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

A. A ClassCastException is thrown at line 15.

B. A ClassCastException is thrown at line 16.

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

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

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

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

2. Given:

          1.   import java.util.*;
          2.   public class Drawers {
          3.     public static void main(String[] args) {
          4.       List<String> desk = new ArrayList<String>();
          5.       desk.add("pen"); desk.add("scissors"); desk.add("redStapler");
          6.     System.out.print(desk.indexOf("redStapler"));
          7.     Collection.reverse(desk);
          8.     System.out.print(" " + desk.indexOf("redStapler"));
          9.     Collection.sort(desk);
         10.    System.out.println(" " + desk.indexOf("redStapler"));
         11. } }

What is the result?

A. 1 1 1

B. 2 0 1

C. 2 0 2

D. 2 2 2

E. Compilation fails.

F. An exception is thrown at runtime.

3. Given:

          1.  import java.util.*;
          2.  class Radio {
          3.    String getFreq() { return "97.3"; }
          4.    static String getF() { return "97.3"; }
          5.  }
          6.  class Ham extends Radio {
          7.    String getFreq() { return "50.1"; }
          8.    static String getF() { return "50.1"; }
          9.    public static void main(String[] args) {
         10.      List<Radio> radios = new ArrayList<Radio>();
         11.      radios.add(new Radio());
         12.      radios.add(new Ham());
         13.      for(Radio r: radios)
         14.        System.out.print(r.getFreq() + " " + r.getF() + "  ");
         15.  }  }

What is the result?

A. 50.1 50.1 50.1 50.1

B. 50.1 97.3 50.1 97.3

C. 97.3 50.1 50.1 50.1

D. 97.3 97.3 50.1 50.1

E. 97.3 97.3 50.1 97.3

F. 97.3 97.3 97.3 97.3

G. Compilation fails.

H. An exception is thrown at runtime.

4. 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 static com.MyClass.*;
         12.  public class TestImports {
         13.    public static void main(String[] args) {
         14.      System.out.print(myConstant + " ");
         15.      howdy();
         16.      System.out.print(mc.instVar + " ");
         17.      System.out.print(instVar + " ");
         18.  }  }

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

A. 343 howdy 42 42

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

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

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

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

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

5. Given this code in a method:

          4.   int x = 0;
          5.   int[] primes = {1,2,3,5};
          6.   for(int i: primes)
          7.     switch(i) {
          8.      case 1: x += i;
          9.      case 5: x += i;
         10.      default: x += i;
         11.      case 2: x += i;
         12.    }
         13.   System.out.println(x);

What is the result?

A. 11

B. 13

C. 24

D. 27

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

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

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

6. Given:

          1.   import java.util.*;
          2.   public class Elway {
          3.     public static void main(String[] args) {
          4.       ArrayList[] ls = new ArrayList[3];
          5.       for(int i = 0; i < 3; i++) {
          6.         ls[i] = new ArrayList();
          7.         ls[i].add("a" + i);
          8.     }
          9.     Object o = ls;
         10.    do3(ls);
         11.    for(int i = 0; i < 3; i++) {
         12.      // insert code here
         13.    }
         14.   }
         15.   static Object do3(ArrayList[] a) {
         16.     for(int i = 0; i < 3; i++)  a[i].add("e");
         17.     return a;
         18.  } }

And the following fragments:

I. System.out.print(o[i] + " ");

II. System.out.print((ArrayList[])[i] + " ");

III. System.out.print( ((Object[])o)[i] + " ");

IV. System.out.print(((ArrayList[])o)[i] + " ");

If the fragments are added to line 12, independently, which are true? (Choose all that apply.)

A. Fragment I will compile.

B. Fragment II will compile.

C. Fragment III will compile.

D. Fragment IV will compile.

E. Compilation fails due to other errors.

F. Of those that compile, the output will be [a0] [a1] [a2]

G. Of those that compile, the output will be [a0, e] [a1, e] [a2, e]

7. Given:

          1.  enum MyEnum {HI, ALOHA, HOWDY};
          2.  public class PassEnum {
          3.    public static void main(String[] args) {
          4.      PassEnum p = new PassEnum();
          5.      MyEnum[] v = MyEnum.values();
          6.      v = MyEnum.getValues();
          7.      for(MyEnum me: MyEnum.values())  p.getEnum(me);
          8.      for(int x = 0; x < MyEnum.values().length; x++)  p.getEnum(v[x]);
          9.      for(int x = 0; x < MyEnum.length; x++)  p.getEnum(v[x]);
         10.      for(MyEnum me: v)  p.getEnum(me);
         11.    }
         12.    public void getEnum(MyEnum e) {
         13.      System.out.print(e + " ");
         14.  }  }

Which line(s) of code will cause a compiler error? (Choose all that apply.)

A. line 1

B. line 5

C. line 6

D. line 7

E. line 8

F. line 9

G. line 10

H. line 12

8. Given:

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

Which are true? (Choose all that apply.)

A. Compilation fails.

B. An exception is thrown at runtime.

C. Loop #1 will run most of its iterations before Loop #2.

D. Loop #2 will run most of its iterations before Loop #1.

E. There is no way to predict which loop will mostly run first.

9. Given this code in a method:

          5.    int x = 3;
          6.    for(int i = 0; i < 3; i++) {
          7.      if(i == 1) x = x++;
          8.      if(i % 2 == 0 && x % 2 == 0) System.out.print(".");
          9.      if(i % 2 == 0 && x % 2 == 1) System.out.print("-");
         10.      if(i == 2 ^ x == 4) System.out.print(",");
         11.   }
         12.   System.out.println("<");

What is the result?

A. -.,<

B. --,<

C. -.,,<

D. --,,<

E. -.-,,<

F. Compilation fails.

10. Given:

          2.   public class Incomplete {
          3.     public static void main(String[] args) {  // change code here ?
          4.       // insert code here ?
          5.       new Incomplete().doStuff();
          6.       // insert code here
       ...
         10.    }
         11.    static void doStuff() throws Exception {
         12.      throw new Exception();
         13.  } }

Which are true? (Choose all that apply.)

A. Compilation succeeds with no code changes.

B. Compilation succeeds if main() is changed to throw an Exception.

C. Compilation succeeds if a try-catch is added, surrounding line 5.

D. Compilation succeeds if a try-finally is added, surrounding line 5.

E. Compilation succeeds only if BOTH main() declares an Exception AND a try-catch is added, surrounding line 5.

11. Given the proper imports, and given:

          24.  Date d1 = new Date();
          25.  Date d2 = d1;|

26. System.out.println(d1);
27. d2.setTime(d1.getTime() + (7 * 24 * 60 * 60));
28. System.out.println(d2);

Which are true? (Choose all that apply.)

A. Compilation fails.

B. An exception is thrown at runtime.

C. Some of the output will be today’s date.

D. Some of the output will be next week’s date.

E. Some of the output will represent the Java “epoch” (i.e., January 1, 1970).

12. Given:

          2. interface Machine { }
          3. interface Engine { }
          4. abstract interface Tractor extends Machine, Engine {
          5.   void pullStuff();
          6. }
          7. class Deere implements Tractor {
          8.   public void pullStuff() { System.out.print("pulling "); }
          9. }
         10. class LT255 implements Tractor extends Deere {
         11.   public void pullStuff() { System.out.print("pulling harder "); }
         12. }
         13. public class LT155 extends Deere implements Tractor, Engine { }

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

A. Compilation succeeds.

B. Compilation fails because of error(s) in Tractor.

C. Compilation fails because of error(s) in Deere.

D. Compilation fails because of error(s) in LT255.

E. Compilation fails because of error(s) in LT155.

13. Given this code in a method:

          5.     boolean[] ba = {true, false};
          6.     short[][] gr = {{1,2}, {3,4}};
          7.     int i = 0;
          8.     for( ; i < 10; ) i++;
          9.     for(short s: gr) ;
         10.     for(int j = 0, k = 10; k > j; ++j, k--) ;
         11.     for(int j = 0; j < 3; System.out.println(j++)) ;
         12.     for(Boolean b: ba) ;

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

A. Compilation succeeds.

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

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

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

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

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

14. Given:

          2. package w.x;
          3. import w.y.Zoom;
          4. public class Zip {
          5.   public static void main(String[] args) {
          6.     new Zoom().doStuff();
          7. } }

And the following command to compile Zip.java: javac -cp w.jar -d . Zip.java

And another class w.y.Zoom (with a doStuff() method), which has been compiled and deployed into a JAR file w.jar:

Fill in the blanks using the following fragments to show the directory structure and the contents of any directory(s) necessary to compile successfully from $ROOT.Note: NOT all the blanks must be filled, NOT all the fragments will be used, and fragments CANNOT be used more than once.

image

Fragments:

Zoom.java   Zip.java    Zoom.class
Zip.class   [Zoom.jar]  [w.jar]
w           x           y

15. Given:

          2. class Pancake { }
          3. class BlueberryPancake extends Pancake { }
          4. public class SourdoughBlueberryPancake2 extends BlueberryPancake {
          5.    public static void main(String[] args) {
          6.       Pancake p4 = new SourdoughBlueberryPancake2();
          7.       // insert code here
          8. } }

And the following six declarations (which are to be inserted independently at line 7):

I. Pancake p5 = p4;

II. Pancake p6 = (BlueberryPancake)p4;

III. BlueberryPancake b2 = (BlueberryPancake)p4;

IV. BlueberryPancake b3 = (SourdoughBlueberryPancake2)p4;

V. SourdoughBlueberryPancake2 s1 = (BlueberryPancake)p4;

VI. SourdoughBlueberryPancake2 s2 = (SourdoughBlueberryPancake2)p4;

Which are true? (Choose all that apply.)

A. All six declarations will compile.

B. Exactly one declaration will not compile.

C. More than one of the declarations will not compile.

D. Of those declarations that compile, none will throw an exception.

E. Of those declarations that compile, exactly one will throw an exception.

F. Of those declarations that compile, more than one will throw an exception.

16. Given:

          3. class IcelandicHorse {
          4.    void tolt() { System.out.print("4-beat "); }
          5. }
          6. public class Vafi extends IcelandicHorse {
          7.    public static void main(String[] args) {
          8.      new Vafi().go();
          9.      new IcelandicHorse().tolt();
         10.    }
         11.    void go() {
         12.      IcelandicHorse h1 = new Vafi();
         13.      h1.tolt();
         14.      Vafi v = (Vafi) h1;
         15.      v.tolt();
         16.    }
         17.    void tolt() { System.out.print("pacey "); }
         18.  }

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

A. 4-beat pacey pacey

B. pacey pacey 4-beat

C. 4-beat 4-beat 4-beat

D. 4-beat pacey 4-beat

E. pacey, followed by an exception

F. 4-beat, followed by an exception

17. Given:

          1. class MyException extends RuntimeException { }
          2. public class Houdini {
          3.   public static void main(String[] args) throws Exception {
          4.     throw new MyException();
          5.     System.out.println("success");
          6. } }

Which are true? (Choose all that apply.)

A. The code runs without output.

B. The output "success" is produced.

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

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

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

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

18. Given:

          3. public class Avast {
          4.   static public void main(String[] scurvy) {
          5.     System.out.print(scurvy[1] + " ");
          6.     main(scurvy[2]);
          7.   }
          8.   public static void main(String dogs) {
          9.     assert(dogs == null);
         10.     System.out.println(dogs);
         11. } }

And, if the code compiles, the command-line invocation:

        java Avast -ea 1 2 3

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

A. 1 2

B. 2 3

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

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

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

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

G. Some output is produced and then an AssertionError is thrown.

19. Given:

          2. class Wheel {
          3.   Wheel(int s) { size = s; }
          4.   int size;
          5.   void spin() { System.out.print(size + " inch wheel spinning, "); }
          6. }
          7. public class Bicycle {
          8.   public static void main(String[] args) {
          9.     Wheel[] wa = {new Wheel(15), new Wheel(17)};
         10.     for(Wheel w: wa)
         11.       w.spin();
         12. } }

Which are true? (Choose all that apply.)

A. Compilation fails.

B. If size was private, the degree of coupling would change.

C. If size was private, the degree of cohesion would change.

D. The Bicycle class is tightly coupled with the Wheel class.

E. The Bicycle class is loosely coupled with the Wheel class.

F. If size was private, the degree of encapsulation would change.

20. Given:

          2. public class Mouthwash {
          3.   static int x = 1;
          4.   public static void main(String[] args) {
          5.     int x = 2;
          6.     for(int i=0; i< 3; i++) {
          7.       if(i==1) System.out.print(x + " ");
          8.     }
          9.     go();
         10.     System.out.print(x + " " + i);
         11.   }
         12.   static void go() { int x = 3; }
         13. }

What is the result?

A. 1 2 2

B. 2 2 2

C. 2 2 3

D. 2 3 2

E. Compilation fails.

F. An exception is thrown at runtime.

21. Given the proper imports, and given:

          23. String s = "123 888888 x 345 -45";
          24. Scanner sc = new Scanner(s);
          25. while(sc.hasNext())
          26.   if(sc.hasNextShort())
          27.     System.out.print(sc.nextShort() + " ");

What is the result?

A. The output is 123 345

B. The output is 123 345 -45

C. The output is 123 888888 345 -45

D. The output is 123 followed by an exception.

E. The output is 123 followed by an infinite loop.

22. Given:

          1. interface Horse { public void nicker(); }

Which will compile? (Choose all that apply.)

A. public class Eyra implements Horse { public void nicker() { } }

B. public class Eyra implements Horse { public void nicker(int x) { } }

C. public class Eyra implements Horse {

public void nicker() { System.out.println("huhuhuhuh..." ); }

}

D. public abstract class Eyra implements Horse {

public void nicker(int loud) { }

}

E. public abstract class Eyra implements Horse {

public void nicker(int loud) ;

}

23. Given:

          2. public class LaoTzu extends Philosopher {
          3.    public static void main(String[] args) {
          4.      new LaoTzu();
          5.      new LaoTzu("Tigger");
          6.    }
          7.    LaoTzu() { this("Pooh"); }
          8.    LaoTzu(String s) { super(s); }
          9. }
         10. class Philosopher {
         11.   Philosopher(String s) { System.out.print(s + " "); }
         12. }

What is the result?

A. Pooh Pooh

B. Pooh Tigger

C. Tigger Pooh

D. Tigger Tigger

E. Compilation fails due to a single error in the code.

F. Compilation fails due to multiple errors in the code.

24. Which are capabilities of Java’s assertion mechanism? (Choose all that apply.)

A. You can, at the command line, enable assertions for a specific class.

B. You can, at the command line, disable assertions for a specific package.

C. You can, at runtime, enable assertions for any version of Java.

D. It’s considered appropriate to catch and handle an AssertionError programmatically.

E. You can programmatically test whether assertions have been enabled without throwing an AssertionError.

25. Given:

          4. public class Stone implements Runnable {
          5.   static int id = 1;
          6.   public void run() {
          7.     try {
          8.       id = 1 - id;
          9.       if(id == 0) { pick(); } else { release(); }
         10.   } catch(Exception e) { }
         11.  }
         12.  private static synchronized void pick() throws Exception {
         13.    System.out.print("P ");     System.out.print("Q ");
         14.  }
         15.  private synchronized void release() throws Exception {
         16.    System.out.print("R ");     System.out.print("S ");
         17.  }
         18.  public static void main(String[] args) {
         19.    Stone st = new Stone();
         20.    new Thread(st).start();
         21.    new Thread(st).start();
         22.  } }

Which are true? (Choose all that apply.)

A. The output could be P Q R S

B. The output could be P R S Q

C. The output could be P R Q S

D. The output could be P Q P Q

E. The program could cause a deadlock.

F. Compilation fails.

26. Given:

          3. public class States {
          4.   static String s;
          5.   static Boolean b;
          6.   static Boolean t1() { return new Boolean("howdy"); }
          7.   static boolean t2() { return new Boolean(s); }
          8.   public static void main(String[] args) {
          9.     if(t1()) System.out.print("t1 ");
         10.     if(!t2()) System.out.print("t2 ");
         11.     if(t1() != t2()) System.out.print("!= ");
         12.   }
         13. }

Which are true? (Choose all that apply.)

A. Compilation fails.

B. No output is produced.

C. The output will contain "t1 "

D. The output will contain "t2 "

E. The output will contain "!= "

F. The output is "t1 ", followed by an exception.

27. Which are valid command-line switches when working with assertions? (Choose all that apply.)

A. -ea

B. -da

C. -dsa

D. -eva

E. -enableassertions

28. Given:

          4. class Account { Long acctNum, password; }
          5. public class Banker {
          6.   public static void main(String[] args) {
          7.      new Banker().go();
          8.       // do more stuff
          9.    }
         10.    void go() {
         11.      Account a1 = new Account();
         12.      a1.acctNum = new Long("1024");
         13.      Account a2 = a1;
         14.      Account a3 = a2;
         15.      a3.password = a1.acctNum.longValue();
         16.      a2.password = 4455L;
         17. }  }

When line 8 is reached, which are true? (Choose all that apply.)

A. a1.acctNum == a3.password

B. a1.password == a2.password

C. Three objects are eligible for garbage collection.

D. Four objects are eligible for garbage collection.

E. Six objects are eligible for garbage collection.

F. Less than three objects are eligible for garbage collection.

G. More than six objects are eligible for garbage collection.

29. Given:

          2. public class Coyote {
          3.   public static void main(String[] args) {
          4.     int x = 4;
          5.     int y = 4;
          6.     while((x = jump(x)) < 8)
          7.       do {
          8.         System.out.print(x + " ");
          9.       } while ((y = jump(y)) < 6);
         10.   }
         11.   static int jump(int x) { return ++x; }
         12. }

What is the result?

A. 5 5 6 6

B. 5 5 6 7

C. 5 5 6 6 7

D. 5 6 5 6 7

E. Compilation fails due to a single error.

F. Compilation fails due to multiple errors.

30. Given:

          2. class Engine {
          3.   public class Piston {
          4.     static int count = 0;
          5.     void go() { System.out.print(" pump " + ++count); }
          6.   }
          7.   public Piston getPiston() { return new Piston(); }
          8. }
          9. public class Auto {
         10.   public static void main(String[] args) {
         11.     Engine e = new Engine();
         12.     // Engine.Piston p = e.getPiston();
         13.     e.Piston p = e.getPiston();
         14.     p.go(); p.go();
         15. } }

In order for the code to compile and produce the output " pump 1 pump 2", which are true? (Choose all that apply.)

A. The code is correct as it stands.

B. Line 4 must be changed. count can’t be declared "static"

C. Line 12 must be un-commented, and line 13 must be removed.

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

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