Self Test


1. 

Given:

 class Scoop {   static int thrower() throws Exception { return 42; }   public static void main(String [] args) {     try {       int x = thrower();     } catch (Exception e) {       X++;     } finally {       System.out.printIn("x = " + ++x); } } } 

What is the result?

  1. x = 42

  2. x = 43

  3. x = 44

  4. Compilation fails.

  5. The code runs with no output.

image from book

2. 

Given:

 class CardBoard {   Short story = 5;   CardBoard go(CardBoard cb) {     cb = null;     return cb;   }   public static void main(String[] args) {     CardBoard c1 = new CardBoard();     CardBoard c2 = new CardBoard();     CardBoard c3 = c1.go(c2);     c1 = null;     // do Stuff } } 

When // doStuff is reached, how many objects are eligible for GC?

  1. 0

  2. 1

  3. 2

  4. Compilation fails.

  5. It is not possible to know.

  6. An exception is thrown at runtime.

image from book

3. 

Given:

 class Alien {   String invade(short ships) { return "a few"; }   String invade(short... ships) { return "many"; } } class Defender {   public static void main(String [] args) {     System.out.println(new Alien().invade(7));   } } 

What is the result?

  1. many

  2. a few

  3. Compilation fails.

  4. The output is not predictable.

  5. An exception is thrown at runtime.

image from book

4. 

Given:

 1. class Dims { 2.   public static void main(String[] args) { 3.     int[] [] a = {{1,2,}, {3,4}}; 4.     int [] b = (int [] ) a [1] ; 5.     Object o1 = a; 6.     int [] [] a2 = (int[] [] )   o1; 7.     int [] b2 = (int []) o1; 8.     System.out.println(b[1]); 9. } } 

What is the result?

  1. 2

  2. 4

  3. An exception is thrown at runtime.

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

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

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

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

image from book

5. 

Given:

 class Eggs {   int doX(Long x, Long y) { return 1; }   int doX(long... x) { return 2; }   int doX(Integer x, Integer y) { return 3; }   int doX(Number n, Number m) { return 4; }   public static void main(String[] args) {     new Eggs().go();   }   void go () {     short s = 7;     System.out.print(doX(s,s) + " ");     System.out.println(doX(7,7)); } } 

What is the result?

  1. 1 1

  2. 2 1

  3. 3 1

  4. 4 1

  5. 2 3

  6. 3 3

  7. 4 3

image from book

6. 

Given:

 class Mixer {   Mixer() { }   Mixer(Mixer m) { ml = m;}   Mixer m1;   public static void main(String[] args) {     Mixer m2 = new Mixer();     Mixer m3 = new Mixer(m2);  m3.go();     Mixer m4 = m3.m1;          m4.go();     Mixer m5 = m2.m1;          m5.go();   }   void go() { System.out.print("hi "); } } 

What is the result?

  1. hi

  2. hi hi

  3. hi hi hi

  4. Compilation fails

  5. hi, followed by an exception

  6. hi hi, followed by an exception

image from book

7. 

Given:

 1. class Zippy { 2.   String[] x; 3.   int[] a [] = {{1,2}, {l}}; 4.   Object c = new long [4] ; 5.   Object[] d = x; 6. } 

What is the result?

  1. Compilation succeeds.

  2. Compilation fails due only to an error on line 3.

  3. Compilation fails due only to an error on line 4.

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

  5. Compilation fails due to errors on lines 3 and 5.

  6. Compilation fails due to errors on lines 3, 4, and 5.

image from book

8. 

Given:

 class Fizz {   int x = 5;   public static void main(String[] args) {     final Fizz f1 = new Fizz();     Fizz f2 = new Fizz();     Fizz f3 = FizzSwitch(f1,f2);     System.out.println((f1 == f3) + " " + (f1.x == f3.x));   }   static Fizz FizzSwitch(Fizz x, Fizz y) {     final Fizz z = x;     z.x = 6;     return z; } } 

What is the result?

  1. true true

  2. false true

  3. true false

  4. false false

  5. Compilation fails.

  6. An exception is thrown at runtime.

image from book

9. 

Given:

 class Knowing {   static final long tooth = 343L;   static long doIt(long tooth) {     System.out.print(++tooth + " ");     return ++tooth;   }   public static void main(String[] args) {     System.out.print(tooth + " ");     final long tooth = 340L;     new Knowing().doIt(tooth);     System.out.println(tooth);   } } 

What is the result?

  1. 343 340 340

  2. 343 340 342

  3. 343 341 342

  4. 343 341 340

  5. 343 341 343

  6. Compilation fails.

  7. An exception is thrown at runtime.

image from book

10. 

Which is true? (Choose all that apply.)

  1. The invocation of an object's finalize() method is always the last thing that happens before an object is garbage collected (GCed).

  2. When a stack variable goes out of scope it is eligible for GC.

  3. Some reference variables live on the stack, and some live on the heap.

  4. Only objects that have no reference variables referring to them can be eligible for GC.

  5. It's possible to request the GC via methods in either java. lang. Runtime or java.lang.System classes.

image from book

11. 

Given:

  1. class Convert {  2.   public static void main(String[] args) {  3.     Long xL = new Long(456L);  4.     long x1 = Long.valueOf("123");  5.     Long x2 = Long.valueOf("123");  6.     long x3 = xL.longValue();  7.     Long x4 = xL.longValue();  8.     Long x5 = Long.parseLong("456");  9.     long x6 = Long.parseLong("123"); 10.  } 11. } 

Which will compile using Java 5, but will NOT compile using Java 1.4? (Choose all that apply.)

  1. Line 4

  2. Line 5

  3. Line 6

  4. Line 7

  5. Line 8

  6. Line 9

image from book

12. 

Given:

 1. class Eco { 2.   public static void main(String[] args)    { 3.     Eco e1 = new Eco(); 4.     Eco e2 = new Eco(); 5.     Eco e3 = new Eco(); 6.     e3.e = e2; 7.     e1.e = e3; 8.     e2 = null; 9.     e3 = null; 10.    e2.e = el; 11.    e1   =  null; 12.  } 13.  Eco e; 14. } 

At what point is only a single object eligible for GC?

  1. After line 8 runs.

  2. After line 9 runs.

  3. After line 10 runs.

  4. After line 11 runs.

  5. Compilation fails.

  6. Never in this program.

  7. An exception is thrown at runtime.

image from book

13. 

Given:

 1. class Bigger { 2.   public static void main(String[] args) { 3.   // insert code here 4.   } 5. } 6. class Better { 7.   enum Faster {Higher, Longer}; 8. } 

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

  1. Faster f = Faster.Higher;

  2. Faster f = Better.Faster.Higher;

  3. Better.Faster f = Better.Faster.Higher;

  4. Bigger.Faster f = Bigger.Faster.Higher;

  5. Better. Faster f2; f2 = Better.Faster.Longer;

  6. Better b; b.Faster = f3; f3 = Better.Faster.Longer;

image from book

14. 

Given:

 class Bird {   { System.out.print("bl "); }   public Bird() { System.out.print("b2 "); } } class Raptor extends Bird {   static { System.out.print("r1 "); }   public Raptor() { System.out.print("r2 "); }   { System.out.print("r3 "); }   static { System.out.print("r4 "); } } class Hawk extends Raptor {   public static void main(String[] args) {     System.out.print("pre ");     new Hawk();     System.out.println("hawk ");   } } 

What is the result?

  1. pre b1 b2 r3 r2 hawk

  2. pre b2 b1 r2 r3 hawk

  3. pre b2 b1 r2 r3 hawk r1 r4

  4. r1 r4 pre b1 b2 r3 r2 hawk

  5. r1 r4 pre b2 b1 r2 r3 hawk

  6. pre r1 r4 b1 b2 r3 r2 hawk

  7. pre r1 r4 b2 b1 r2 r3 hawk

  8. The order of output cannot be predicted.

  9. Compilation fails.

image from book

Answers

1. 

þ  

D is correct, the variable x is only in scope within the try code block, it's not in scope in the catch or finally blocks. (For the exam, get used to those horrible closing } } } .)

ý  

A, B, C, and E are incorrect based on the above. (Objective 1.3)

2. 

þ  

C is correct. Only one CardBoard object (c1) is eligible, but it has an associated Short wrapper object that is also eligible.

ý  

A, B, D, E, and F are incorrect based on the above. (Objective 7-4)

3. 

þ  

C is correct, compilation fails. The var-args declaration is fine, but invade takes a short, so the argument 7 needs to be cast to a short. With the cast, the answer is B, 'a few'.

ý  

A, B, D, and E are incorrect based on the above. (Objective 1.3)

4. 

þ  

C is correct. A ClassCastException is thrown at line 7 because o1 refers to an int [] [] not an int []. If line 7 was removed, the output would be 4.

ý  

A, B, D, E, F, and G are incorrect based on the above. (Objective 1.3)

5. 

þ  

G is correct. Two rules apply to the first invocation of dox(). You can't widen and then box in one step, and var-args are always chosen last. Therefore you can't widen shorts to either ints or longs, and then box them to Integers.or Longs. But you can box shorts to Shorts and then widen them to Numbers, and this takes priority over using a var-args method. The second invocation uses a simple box from int to Integer.

ý  

A, B, C, D, E, and F are incorrect based on the above, (Objective 3.1)

6. 

þ  

F is correct. The m2 object's m1 instance variable is never initialized, so when m5 tries to use it a NullPointerException is thrown.

ý  

A, B, C, D, and E are incorrect based on the above. (Objective 7.3)

7. 

þ  

A is correct, all of these array declarations are legal. Lines 4 and 5 demonstrate that arrays can be cast.

ý  

B, C, D, E, and F are incorrect because this code compiles. (Objective 1.3)

8. 

þ  

A is correct. The references f1, z, and f3 all refer to the same instance of Fizz. The final modifier assures that a reference variable cannot be referred to a different object, but final doesn't keep the object's state from changing.

ý  

B,C, D, E, and F are incorrect based on the above. (Objective 7.3)

9. 

þ  

D is correct. There are three different long variables named tooth. Remember that you can apply the final modifier to local variables, but in this case the 2 versions of tooth marked final are not changed. The only tooth whose value changes is the one not marked final. This program demonstrates a bad practice known as shadowing.

ý  

A, B, C, E, F, and G are incorrect based on the above. (Objective 7.3)

10. 

þ  

C and E are correct. When an object has a reference variable, the reference variable lives inside the object, on the heap.

ý  

A is incorrect, because if, the first time an object's finalize() method runs, the object is saved from the GC, then the second time that object is about to be GCed, finalize() will not run. B is incorrect—stack variables are not dealt with by the GC. D is incorrect because objects can live in "islands of isolation" and be GC eligible. (Objective 7.4)

11. 

þ  

A, D, and E are correct. Because of the methods' return types, these method calls required autoboxing to compile.

ý  

B, C, and F are incorrect based on the above. (Objective 3,1)

12. 

þ  

G is correct. An error at line 10 causes a NullPointerException to be thrown because e2 was set to null in line 8, If line 10 was moved between lines 7 and 8, then F would be correct, because until the last reference is nulled none of the objects is eligible, and once the last reference is nulled, all three are eligible.

ý  

A, B, C, D, E, and F are incorrect based on the above. (Objective 7.4)

13. 

þ  

C and E are correct syntax for accessing an enum from another class.

ý  

A, B, D, and F are incorrect syntax. (Objective 1.3)

14. 

þ  

D is correct. Static init blocks are executed at class loading time, instance init blocks run right after the call to super() in a constructor. When multiple init blocks of a single type occur in a class, they run in order, from the top down.

ý  

A, B, C, E, F, G, H, and I are incorrect based on the above. Note: you'll probably never see this many choices on the real exam! (Objective 1.3)




SCJP Sun Certified Programmer for Java 5 Study Guide Exam 310-055
SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310-055) (Certification Press)
ISBN: 0072253606
EAN: 2147483647
Year: 2006
Pages: 131

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net