Monday 18 March 2013

SCJP - 37 -Given Example



What is the result?
A. pre b1 b2 r3 r2 hawk
B. pre b2 b1 r2 r3 hawk
C. pre b2 b1 r2 r3 hawk r1 r4
D. r1 r4 pre b1 b2 r3 r2 hawk
E. r1 r4 pre b2 b1 r2 r3 hawk
F. pre r1 r4 b1 b2 r3 r2 hawk
G. pre r1 r4 b2 b1 r2 r3 hawk
H. The order of output cannot be predicted.
I. Compilation fails.

Answer:

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)
===
class Bird {
{ System.out.print("b1 "); }
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 ");
}
}

SCJP - 35 -Given Example


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

A. After line 8 runs.
B. After line 9 runs.
C. After line 10 runs.
D. After line 11 runs.
E. Compilation fails.
F. Never in this program.
G. An exception is thrown at runtime.

Answer:

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)
==
class Eco {
public static void main(String[] args) {
Eco e1 = new Eco();
Eco e2 = new Eco();
Eco e3 = new Eco();
e3.e = e2;
e1.e = e3;
e2 = null;
e3 = null;
e2.e = e1;
e1 = null;
1}
Eco e;
}


SCJP - 36 - Given Example



Which, inserted independently at line 3, will compile? (Choose all that apply.)
A. Faster f = Faster.Higher;
B. Faster f = Better.Faster.Higher;
C. Better.Faster f = Better.Faster.Higher;
D. Bigger.Faster f = Bigger.Faster.Higher;
E. Better.Faster f2; f2 = Better.Faster.Longer;
F. Better b; b.Faster = f3; f3 = Better.Faster.Longer;

Answer:

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

A, B, D, and F are incorrect syntax.
(Objective 1.3)
==
class Bigger {
 public static void main(String[] args) {
 // insert code here
 }
 }
 class Better {
 enum Faster {Higher, Longer};
 }

SCJP - 33 -Given Example


33. Which is true? (Choose all that apply.)

A. The invocation of an object’s finalize() method is always the last thing that happens
before an object is garbage collected (GCed).
B. When a stack variable goes out of scope it is eligible for GC.
C. Some reference variables live on the stack, and some live on the heap.
D. Only objects that have no reference variables referring to them can be eligible for GC.
E. It’s possible to request the GC via methods in either java.lang.Runtime or
java.lang.System classes.

Answer:

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)

SCJP - 34 -Given Example



Which will compile using Java 5, but will NOT compile using Java 1.4? (Choose all that apply.)
A. Line 4.
B. Line 5.
C. Line 6.
D. Line 7.
E. Line 8.
F. Line 9.

Answer:

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)
==
class Convert {
public static void main(String[] args) {
Long xL = new Long(456L);
long x1 = Long.valueOf("123");
Long x2 = Long.valueOf("123");
long x3 = xL.longValue();
Long x4 = xL.longValue();
Long x5 = Long.parseLong("456");
long x6 = Long.parseLong("123");
 }
 }

SCJP - 32 -Given Example



What is the result?
A. 343 340 340
B. 343 340 342
C. 343 341 342
D. 343 341 340
E. 343 341 343
F. Compilation fails.
G. An exception is thrown at runtime.

Answer:

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)
==
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);
} }


SCJP - 31 -Given Example



What is the result?
A. true true
B. false true
C. true false
D. false false
E. Compilation fails.
F. An exception is thrown at runtime.

Answer:

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)
==
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;
} }