ASSESSMENT TEST 1

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

1. Given:

          2. public class Bunnies {
          3.   static int count = 0;
          4.   Bunnies() {
          5.     while(count < 10) new Bunnies(++count);
          6.   }
          7.   Bunnies(int x) { super(); }
          8.   public static void main(String[] args) {
          9.     new Bunnies();
         10.     new Bunnies(count);
         11.     System.out.println(count++);
         12.   }
         13. }

What is the result?

A. 9

B. 10

C. 11

D. 12

E. Compilation fails.

F. An exception is thrown at runtime.

2. Given:

          2. public class Jail {
          3.   private int x = 4;
          4.   public static void main(String[] args) {
          5.     protected int x = 6;
          6.     new Jail().new Cell().slam();
          7.   }
          8.   class Cell {
          9.     void slam() { System.out.println("throw away key " + x); }
         10.   }
         11. }

Which are true? (Choose all that apply.)

A. Compilation succeeds.

B. The output is "throw away key 4".

C. The output is "throw away key 6".

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

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

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

3. Given:

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

Which are true? (Choose all that apply.)

A. Compilation fails.

B. No output is produced.

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

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

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

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

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

4. Given:

         2. class Feline { }
         3. public class BarnCat2 extends Feline {
         4.   public static void main(String[] args) {
         5.     Feline ff = new Feline();
         6.     BarnCat2 b = new BarnCat2();
         7.     // insert code here
         8.   }
         9. }

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

A. if(b instanceof ff) System.out.print("1 ");

B. if(b.instanceof(ff)) System.out.print("2 ");

C. if(b instanceof Feline) System.out.print("3 ");

D. if(b instanceOf Feline) System.out.print("4 ");

E. if(b.instanceof(Feline)) System.out.print("5 ");

5. Given:

          2. public class Choosy {
          3.   public static void main(String[] args) {
          4.     String result = "";
          5.     int x = 7, y = 8;
          6.     if(x == 3) { result += "1"; }
          7.     else if (x > 9) { result += "2"; }
          8.     else if (y < 9) { result += "3"; }
          9.     else if (x == 7) { result += "4"; }
         10.     else { result += "5"; }
         11.     System.out.println(result);
         12.   }
         13. }

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

A. 3

B. 34

C. 35

D. 345

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

F. Compilation fails due to errors on lines 8 and 9.

G. Compilation fails due to errors on lines 7, 8, and 9.

6. Given:

          1. public class Twine {
          2.   public static void main(String[] args) {
          3.     String s = "";
          4.     StringBuffer sb1 = new StringBuffer("hi");
          5.     StringBuffer sb2 = new StringBuffer("hi");
          6.     StringBuffer sb3 = new StringBuffer(sb2);
          7.     StringBuffer sb4 = sb3;
          8.     if(sb1.equals(sb2)) s += "1 ";
          9.     if(sb2.equals(sb3)) s += "2 ";
         10.     if(sb3.equals(sb4)) s += "3 ";
         11.     String s2 = "hi";
         12.     String s3 = "hi";
         13.     String s4 = s3;
         14.     if(s2.equals(s3)) s += "4 ";
         15.     if(s3.equals(s4)) s += "5 ";
         16.     System.out.println(s);
         17.   }
         18. }

What is the result?

A. 1 3

B. 1 5

C. 1 2 3

D. 1 4 5

E. 3 4 5

F. 1 3 4 5

G. 1 2 3 4 5

H. Compilation fails.

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

A. All classes of Exception extend Error.

B. All classes of Error extend Exception.

C. All Errors must be handled or declared.

D. All classes of Exception extend Throwable.

E. All Throwables must be handled or declared.

F. All Exceptions must be handled or declared.

G. RuntimeExceptions need never be handled or declared.

8. Given:

          2. import java.util.*;
          3. public class Birthdays {
          4.   public static void main(String[] args) {
          5.     Map<Friends, String> hm = new HashMap<Friends, String>();
          6.     hm.put(new Friends("Charis"), "Summer 2009");
          7.     hm.put(new Friends("Draumur"), "Spring 2002");
          8.     Friends f = new Friends(args[0]);
          9.     System.out.println(hm.get(f));
         10.   }
         11. }
         12. class Friends {
         13.   String name;
         14.   Friends(String n) { name = n; }
         15. }

And the command line invocation:

         java Birthdays Draumur

What is the result?

A. null

B. Draumur

C. Spring 2002

D. Compilation fails.

E. The output is unpredictable.

F. An exception is thrown at runtime.

G. Friends@XXXX (where XXXX is a representation of a hashcode)

9. Given:

          2. import java.util.*;
          3. class Cereal { }
          4. public class Flakes extends Cereal {
          5.   public static void main(String[] args) {
          6.     List<Flakes> c0 = new List<Flakes>();
          7.     List<Cereal> c1 = new ArrayList<Cereal>();
          8.     List<Cereal> c2 = new ArrayList<Flakes>();
          9.     List<Flakes> c3 = new ArrayList<Cereal>();
         10.     List<Object> c4 = new ArrayList<Flakes>();
         11.     ArrayList<Cereal> c5 = new ArrayList<Flakes>();
         12.   }
         13. }

Which are true? (Choose all that apply.)

A. Compilation succeeds.

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

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

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

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

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

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

10. Given:

          3. public class RediMix extends Concrete {
          4.   RediMix() { System.out.println("r "); }
          5.   public static void main(String[] args) {
          6.     new RediMix();
          7.   }
          8. }
          9. class Concrete extends Sand {
         10.   Concrete() { System.out.print("c "); }
         11.   private Concrete(String s) { }
         12. }
         13. abstract class Sand {
         14.   Sand() { System.out.print("s "); }
         15. }

What is the result?

A. r

B. c r

C. r c

D. s c r

E. r c s

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

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

11. Which statement(s) are true? (Choose all that apply.)

A. Coupling is the OO principle most closely associated with hiding a class’s implementation details.

B. Coupling is the OO principle most closely associated with making sure classes know about other classes only through their APIs.

C. Coupling is the OO principle most closely associated with making sure a class is designed with a single, well-focused purpose.

D. Coupling is the OO principle most closely associated with allowing a single object to be seen as having many types.

12. Given:

          2. class Mosey implements Runnable {
          3.   public void run() {
          4.     for(int i = 0; i < 1000; i++) {
          5.       System.out.print(Thread.currentThread().getId() + "-" + i + " ");
          6. } } }
          7. public class Stroll {
          8.   public static void main(String[] args) throws Exception {
          9.     Thread t1 = new Thread(new Mosey());
         10.     // insert code here
         11.   }
         12. }

Which of the following code fragments, inserted independently at line 10, will probably run most (or all) of the main thread’s run() method invocation before running most of the t1 thread’s run() method invocation? (Choose all that apply.)

A. t1.setPriority(1);
new Mosey().run();
t1.start();

B. t1.setPriority(9);
new Mosey().run();
t1.start();

C. t1.setPriority(1);
t1.start();
new Mosey().run();

D. t1.setPriority(8);
t1.start();
new Mosey().run();

13. Given:

         37. boolean b = false;
         38. int i = 7;
         39. double d = 1.23;
         40. float f = 4.56f;
         41.
         42. // insert code here

Which line(s) of code, inserted independently at line 42, will compile and run without exception? (Choose all that apply.)

A. System.out.printf(" %b", b);

B. System.out.printf(" %i", i);

C. System.out.format(" %d", d);

D. System.out.format(" %d", i);

E. System.out.format(" %f", f);

14. Given:

          1. import java.util.*;
          2. public class MyPancake implements Pancake {
          3.   public static void main(String[] args) {
          4.     List<String> x = new ArrayList<String>();
          5.     x.add("3"); x.add("7"); x.add("5");
          6.     List<String> y = new MyPancake().doStuff(x);
          7.     y.add("1");
          8.     System.out.println(x);
          9.   }
         10.   List<String> doStuff(List<String> z) {
         11.     z.add("9");
         12.     return z;
         13.   }
         14. }
         15. interface Pancake {
         16.   List<String> doStuff(List<String> s);
         17. }

What is the most likely result?

A. [3, 7, 5]

B. [3, 7, 5, 9]

C. [3, 7, 5, 9, 1]

D. Compilation fails.

E. An exception is thrown at runtime.

QUICK ANSWER KEY

1. B

2. D

3. A

4. C

5. A

6. E

7. D, G

8. A

9. B, D, E, F, G

10. D

11. B

12. A, B, C

13. A, D, E

14. D

ASSESSMENT TEST 1: ANSWERS

1. Given:

          2. public class Bunnies {
          3.   static int count = 0;
          4.   Bunnies() {
          5.     while(count < 10) new Bunnies(++count);
          6.   }
          7.   Bunnies(int x) { super(); }
          8.   public static void main(String[] args) {
          9.     new Bunnies();
         10.     new Bunnies(count);
         11.     System.out.println(count++);
         12.   }
         13. }

What is the result?

A. 9

B. 10

C. 11

D. 12

E. Compilation fails.

F. An exception is thrown at runtime.

2. Given:

          2. public class Jail {
          3.   private int x = 4;
          4.   public static void main(String[] args) {
          5.     protected int x = 6;
          6.     new Jail().new Cell().slam();
          7.   }
          8.   class Cell {
          9.     void slam() { System.out.println("throw away key " + x); }
         10.   }
         11. }

Which are true? (Choose all that apply.)

A. Compilation succeeds.

B. The output is "throw away key 4".

C. The output is "throw away key 6".

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

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

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

3. Given:

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

Which are true? (Choose all that apply.)

A. Compilation fails.

B. No output is produced.

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

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

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

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

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

4. Given:

         2. class Feline { }
         3. public class BarnCat2 extends Feline {
         4.   public static void main(String[] args) {
         5.     Feline ff = new Feline();
         6.     BarnCat2 b = new BarnCat2();
         7.     // insert code here
         8.   }
         9. }

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

A. if(b instanceof ff) System.out.print("1 ");

B. if(b.instanceof(ff)) System.out.print("2 ");

C. if(b instanceof Feline) System.out.print("3 ");

D. if(b instanceOf Feline) System.out.print("4 ");

E. if(b.instanceof(Feline)) System.out.print("5 ");

5. Given:

          2. public class Choosy {
          3.   public static void main(String[] args) {
          4.     String result = "";
          5.     int x = 7, y = 8;
          6.     if(x == 3) { result += "1"; }
          7.     else if (x > 9) { result += "2"; }
          8.     else if (y < 9) { result += "3"; }
          9.     else if (x == 7) { result += "4"; }
         10.     else { result += "5"; }
         11.     System.out.println(result);
         12.   }
         13. }

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

A. 3

B. 34

C. 35

D. 345

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

F. Compilation fails due to errors on lines 8 and 9.

G. Compilation fails due to errors on lines 7, 8, and 9.

6. Given:

          1. public class Twine {
          2.   public static void main(String[] args) {
          3.     String s = "";
          4.     StringBuffer sb1 = new StringBuffer("hi");
          5.     StringBuffer sb2 = new StringBuffer("hi");
          6.     StringBuffer sb3 = new StringBuffer(sb2);
          7.     StringBuffer sb4 = sb3;
          8.     if(sb1.equals(sb2)) s += "1 ";
          9.     if(sb2.equals(sb3)) s += "2 ";
         10.     if(sb3.equals(sb4)) s += "3 ";
         11.     String s2 = "hi";
         12.     String s3 = "hi";
         13.     String s4 = s3;
         14.     if(s2.equals(s3)) s += "4 ";
         15.     if(s3.equals(s4)) s += "5 ";
         16.     System.out.println(s);
         17.   }
         18. }

What is the result?

A. 1 3

B. 1 5

C. 1 2 3

D. 1 4 5

E. 3 4 5

F. 1 3 4 5

G. 1 2 3 4 5

H. Compilation fails.

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

A. All classes of Exception extend Error.

B. All classes of Error extend Exception.

C. All Errors must be handled or declared.

D. All classes of Exception extend Throwable.

E. All Throwables must be handled or declared.

F. All Exceptions must be handled or declared.

G. RuntimeExceptions need never be handled or declared.

8. Given:

          2. import java.util.*;
          3. public class Birthdays {
          4.   public static void main(String[] args) {
          5.     Map<Friends, String> hm = new HashMap<Friends, String>();
          6.     hm.put(new Friends("Charis"), "Summer 2009");
          7.     hm.put(new Friends("Draumur"), "Spring 2002");
          8.     Friends f = new Friends(args[0]);
          9.     System.out.println(hm.get(f));
         10.   }
         11. }
         12. class Friends {
         13.   String name;
         14.   Friends(String n) { name = n; }
         15. }

And the command line invocation:

         java Birthdays Draumur

What is the result?

A. null

B. Draumur

C. Spring 2002

D. Compilation fails.

E. The output is unpredictable.

F. An exception is thrown at runtime.

G. Friends@XXXX (where XXXX is a representation of a hashcode)

9. Given:

          2. import java.util.*;
          3. class Cereal { }
          4. public class Flakes extends Cereal {
          5.   public static void main(String[] args) {
          6.     List<Flakes> c0 = new List<Flakes>();
          7.     List<Cereal> c1 = new ArrayList<Cereal>();
          8.     List<Cereal> c2 = new ArrayList<Flakes>();
          9.     List<Flakes> c3 = new ArrayList<Cereal>();
         10.     List<Object> c4 = new ArrayList<Flakes>();
         11.     ArrayList<Cereal> c5 = new ArrayList<Flakes>();
         12.   }
         13. }

Which are true? (Choose all that apply.)

A. Compilation succeeds.

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

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

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

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

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

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

10. Given:

          3. public class RediMix extends Concrete {
          4.   RediMix() { System.out.println("r "); }
          5.   public static void main(String[] args) {
          6.     new RediMix();
          7.   }
          8. }
          9. class Concrete extends Sand {
         10.   Concrete() { System.out.print("c "); }
         11.   private Concrete(String s) { }
         12. }
         13. abstract class Sand {
         14.   Sand() { System.out.print("s "); }
         15. }

What is the result?

A. r

B. c r

C. r c

D. s c r

E. r c s

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

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

11. Which statement(s) are true? (Choose all that apply.)

A. Coupling is the OO principle most closely associated with hiding a class’s implementation details.

B. Coupling is the OO principle most closely associated with making sure classes know about other classes only through their APIs.

C. Coupling is the OO principle most closely associated with making sure a class is designed with a single, well-focused purpose.

D. Coupling is the OO principle most closely associated with allowing a single object to be seen as having many types.

12. Given:

          2. class Mosey implements Runnable {
          3.   public void run() {
          4.     for(int i = 0; i < 1000; i++) {
          5.       System.out.print(Thread.currentThread().getId() + "-" + i + " ");
          6. } } }
          7. public class Stroll {
          8.   public static void main(String[] args) throws Exception {
          9.     Thread t1 = new Thread(new Mosey());
         10.     // insert code here
         11.   }
         12. }

Which of the following code fragments, inserted independently at line 10, will probably run most (or all) of the main thread’s run() method invocation before running most of the t1 thread’s run() method invocation? (Choose all that apply.)

A. t1.setPriority(1);
new Mosey().run();
t1.start();

B. t1.setPriority(9);
new Mosey().run();
t1.start();

C. t1.setPriority(1);
t1.start();
new Mosey().run();

D. t1.setPriority(8);
t1.start();
new Mosey().run();

13. Given:

         37. boolean b = false;
         38. int i = 7;
         39. double d = 1.23;
         40. float f = 4.56f;
         41.
         42. // insert code here

Which line(s) of code, inserted independently at line 42, will compile and run without exception? (Choose all that apply.)

A. System.out.printf(" %b", b);

B. System.out.printf(" %i", i);

C. System.out.format(" %d", d);

D. System.out.format(" %d", i);

E. System.out.format(" %f", f);

14. Given:

          1. import java.util.*;
          2. public class MyPancake implements Pancake {
          3.   public static void main(String[] args) {
          4.     List<String> x = new ArrayList<String>();
          5.     x.add("3"); x.add("7"); x.add("5");
          6.     List<String> y = new MyPancake().doStuff(x);
          7.     y.add("1");
          8.     System.out.println(x);
          9.   }
         10.   List<String> doStuff(List<String> z) {
         11.     z.add("9");
         12.     return z;
         13.   }
         14. }
         15. interface Pancake {
         16.   List<String> doStuff(List<String> s);
         17. }

What is the most likely result?

A. [3, 7, 5]

B. [3, 7, 5, 9]

C. [3, 7, 5, 9, 1]

D. Compilation fails.

E. An exception is thrown at runtime.

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

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