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

SCJP - 30 - Given Example


What is the result?

A. Compilation succeeds.
B. Compilation fails due only to an error on line 3.
C. Compilation fails due only to an error on line 4.

D. Compilation fails due only to an error on line 5.
E. Compilation fails due to errors on lines 3 and 5.
F. Compilation fails due to errors on lines 3, 4, and 5.

Answer:

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)
==
class Zippy {
String[] x;
int[] a [] = {{1,2}, {1}};
Object c = new long[4];
Object[] d = x;
}


SCJP - 29 -Given Example



What is the result?
A. hi
B. hi hi
C. hi hi hi
D. Compilation fails
E. hi, followed by an exception
F. hi hi, followed by an exception

Answer:

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)
==
class Mixer {
Mixer() { }
Mixer(Mixer m) { m1 = 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 "); }
}

SCJP - 28 -Given Example


What is the result?
A. 1 1
B. 2 1
C. 3 1
D. 4 1
E. 2 3
F. 3 3
G. 4 3

Answer:

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


SCJP - 27 -Given Example



What is the result?
A. 2
B. 4
C. An exception is thrown at runtime
D. Compilation fails due to an error on line 4.
E. Compilation fails due to an error on line 5.
F. Compilation fails due to an error on line 6.
G. Compilation fails due to an error on line 7.

Answer:

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)
==
class Dims {
public static void main(String[] args) {
int[][] a = {{1,2,}, {3,4}};
int[] b = (int[]) a[1];
Object o1 = a;
int[][] a2 = (int[][]) o1;
int[] b2 = (int[]) o1;
System.out.println(b[1]);
} }


SCJP - 26 -Given Example

What is the result?
A. many
B. a few
C. Compilation fails.
D. The output is not predictable.
E. An exception is thrown at runtime.

Answer:

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

SCJP - 25 -Given Example



When // doStuff is reached, how many objects are eligible for GC?
A. 0
B. 1

C. 2
D. Compilation fails.
E. It is not possible to know.
F. An exception is thrown at runtime.

Answer:

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


SCJP - 24 - Given Example



What is the result?
A. x = 42
B. x = 43
C. x = 44
D. Compilation fails.
E. The code runs with no output.

Answer:

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 is are incorrect based on the above. (Objective 1.3)
==
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.println("x = " + ++x);
} } }

Sunday 17 March 2013

SCJP - 23 - Given the following,


Which, inserted at line 9, will compile? (Choose all that apply.)
A. x2.do2();
B. (Y)x2.do2();
C. ((Y)x2).do2();
D. None of the above statements will compile.

Answer:

C is correct. Before you can invoke Y’s do2 method you have to cast x2 to be of type
Y. Statement B looks like a proper cast but without the second set of parentheses, the
compiler thinks it’s an incomplete statement.

A, B and D are incorrect based on the preceding.
(Objective 5.2)
==
class X { void do1() { } }
class Y extends X { void do2() { } }

class Chrome {
public static void main(String [] args) {
X x1 = new X();
X x2 = new Y();
Y y1 = new Y();
// insert code here
}
}


SCJP - 22 - Given Example



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

A. Beagle b2 = (Beagle) dog1;
B. Beagle b3 = (Beagle) dog2;
C. Beagle b4 = dog2;
D. None of the above statements will compile

Answer:

A and B are correct. However, at runtime, A will throw a ClassCastException because
dog1 refers to a Dog object, which can’t necessarily do Beagle stuff.

C and D are incorrect based on the preceding.
(Objective 5.2).
==
class Dog { }
class Beagle extends Dog { }

class Kennel {
public static void main(String [] arfs) {
Beagle b1 = new Beagle();
Dog dog1 = new Dog();
Dog dog2 = b1;
// insert code here
}
}


SCJP - 21 - Which statement(s) are true? (Choose all that apply.)


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

A. Cohesion is the OO principle most closely associated with hiding implementation details.
B. Cohesion is the OO principle most closely associated with making sure that classes know
about other classes only through their APIs.
C. Cohesion is the OO principle most closely associated with making sure that a class is
designed with a single, well-focused purpose.
D. Cohesion is the OO principle most closely associated with allowing a single object to be
seen as having many types.

Answer:

Answer C is correct.

A refers to encapsulation, B refers to coupling, and D refers to polymorphism.
(Objective 5.1)

SCJP - 20 - Given Example


What is the result?
A. 6
B. 7
C. 8
D. 9
E. Compilation fails.
F. An exception is thrown.

Answer:

D is correct. Minor’s constructor makes an explicit call to Uber’s 1-arg constructor,
which makes an explicit (this) call to Uber’s no-arg constructor, which increments y,
then returns to the 1-arg constructor, which multiples y * 2, and then returns to Minor’s
constructor, which adds 3 to y.

A, B, C, E, and F are incorrect based on the preceding.
(Objective 1.6)
==
class Uber {
static int y = 2;
Uber(int x) { this(); y = y * 2; }
Uber() { y++; }
}
class Minor extends Uber {
Minor() { super(y); y = y + 3; }
public static void main(String [] args) {
new Minor();
System.out.println(y);
} }


SCJP - 19 -Given Example



Which, inserted at line 5, will compile? (Choose all that apply.)
A. Programmer debug() { return this; }
B. SCJP debug() { return this; }
C. Object debug() { return this; }
D. int debug() { return 1; }
E. int debug(int x) { return 1; }
F. Object debug(int x) { return this; }

Answer:

A, B, E, and F are correct. A and B are examples of overriding, specifically, B is an
example of overriding using a covariant return. E and F are examples of overloading.

C and D are incorrect. They are illegal overrides because their return types are
incompatible. They are illegal overloads because their arguments did not change.
(Objective 5.4)
==
class Programmer {
Programmer debug() { return this; }
}
class SCJP extends Programmer {
// insert code here
}



SCJP - 18 -Given Example




Which is true? (Choose all that apply.)
A. Woop is-a Hmpf and has-a Zing.
B. Zing is-a Woop and has-a Hmpf.
C. Hmpf has-a Woop and Woop is-a Zing.
D. Woop has-a Hmpf and Woop is-a Zing.
E. Zing has-a Hmpf and Zing is-a Woop.

Answer:

D is correct, Woop inherits a Hmpf from Zing.

A, B, C, and E are incorrect based on the preceding.
(Objective 5.5)
==
class Zing {
protected Hmpf h;
}
class Woop extends Zing { }
class Hmpf { }

SCJP - 17 - Given Example




Which statement(s), inserted at line 6, will compile? (Choose all that apply.)
A. Flower getType() { return this; }
B. String getType() { return "this"; }
C. Plant getType() { return this; }
D. Tulip getType() { return new Tulip(); }

Answer:

A, C, and D are correct. A and D are examples of co-variant returns, i.e., Flower and
Tulip are both subtypes of Plant.

B is incorrect, String is not a subtype of Plant.
(Objective 1.5)
==
class Plant {
String getName() { return "plant"; }
Plant getType() { return this; }
}
class Flower extends Plant {
// insert code here
}
class Tulip extends Flower { }


SCJP - 16 - Using the fragments below, complete the following code so it compiles. Note, you may not have to fill all of the slots.


Answer:


As there is no droppable tile for the variable x and the parentheses (in the Kinder constructor),
are already in place and empty, there is no way to construct a call to the superclass constructor
that takes an argument. Therefore, the only remaining possibility is to create a call to the no-argument
superclass constructor. This is done as: super();. The line cannot be left blank, as the
parentheses are already in place. Further, since the superclass constructor called is the no-argument
version, this constructor must be created. It will not be created by the compiler because
there is another constructor already present.
(Objective 5.4)
==
class AgedP {
__________ __________ __________ __________ __________
public AgedP(int x) {
__________ __________ __________ __________ __________
}
}
public class Kinder extends AgedP {
__________ __________ __________ _________ ________ __________

public Kinder(int x) {
__________ __________ __________ __________ __________ ();
}
}
Fragments: Use the following fragments zero or more times:
AgedP super this
( ) { }
;
===
class AgedP {
AgedP() {}
public AgedP(int x) {
}
}
public class Kinder extends AgedP {
public Kinder(int x) {
super();
}
} 


SCJP - 15 -Given Example


What is the result?
A. Clidlet
B. Clidder
C. Clidder
Clidlet
D. Clidlet
Clidder
E. Compilation fails.

Answer:

A is correct. Although a final method cannot be overridden, in this case, the method is
private, and therefore hidden. The effect is that a new, accessible, method flipper is created.
Therefore, no polymorphism occurs in this example, the method invoked is simply that of
the child class, and no error occurs.

B, C, D, and E are incorrect based on the preceding.
(Objective 5.3)
==
class Clidder {
private final void flipper() { System.out.println("Clidder"); }
}
public class Clidlet extends Clidder {
public final void flipper() { System.out.println("Clidlet"); }
public static void main(String [] args) {
new Clidlet().flipper();
} }

SCJP - 14 -Select the two statements that best indicate a situation with low coupling. (Choose two.)


14. Select the two statements that best indicate a situation with low coupling. (Choose two.)

A. The attributes of the class are all private.
B. The class refers to a small number of other objects.
C. The object contains only a small number of variables.
D. The object is referred to using an anonymous variable, not directly.
E. The reference variable is declared for an interface type, not a class. The interface provides a
small number of methods.
F. It is unlikely that changes made to one class will require any changes in another.

Answer:

E and F are correct. Only having access to a small number of methods implies limited
coupling. If the access is via a reference of interface type, it may be argued that there is
even less opportunity for coupling as the class type itself is not visible. Stating that changes
in one part of a program are unlikely to cause consequences in another part is really the
essence of low coupling. There is no such thing as an anonymous variable. Referring to
only a small number of other objects might imply low coupling, but if each object has many
methods, and all are used, then coupling is high. Variables (attributes) in a class should
usually be private, but this describes encapsulation, rather than low coupling. Of course,
good encapsulation tends to reduce coupling as a consequence.

A, B, C and D are incorrect based on the preceding treatise.
(Objective 5.1)

SCJP - 13 -Given Example


What is the result?
A. BD
B. DB
C. BDC
D. DBC
E. Compilation fails.

Answer:

E is correct. The implied super() call in Bottom2’s constructor cannot be satisfied because
there isn’t a no-arg constructor in Top. A default, no-arg constructor is generated by the
compiler only if the class has no constructor defined explicitly.
A, B, C, and D are incorrect based on the above.
(Objective 1.6)
==
class Top {
public Top(String s) { System.out.print("B"); }
}
public class Bottom2 extends Top {
public Bottom2(String s) { System.out.print("D"); }
public static void main(String [] args) {

new Bottom2("C");
System.out.println(" ");
} }


SCJP - 12 - Given Example



Answer:

B is correct, an abstract class need not implement any or all of an interface’s methods.
E is correct, the class implements the interface method and additionally overloads the
twiddle() method.
A is incorrect because abstract methods have no body.
C is incorrect because classes implement interfaces they don’t extend them.
D is incorrect because overloading a method is not implementing it.
(Objective 5.4)
===
public abstract interface Frobnicate { public void twiddle(String s); }
// Which is a correct class? (Choose all that apply.)
A. public abstract class Frob implements Frobnicate {
public abstract void twiddle(String s) { }
}
B. public abstract class Frob implements Frobnicate { }
C. public class Frob extends Frobnicate {
public void twiddle(Integer i) { }
}
D. public class Frob implements Frobnicate {
public void twiddle(Integer i) { }
}
E. public class Frob implements Frobnicate {
public void twiddle(String i) { }
public void twiddle(Integer s) { }
}

SCJP - 11 - Given Example


What is the result?
A. Flip a Clidlet
B. Flip a Clidder
C. Flip a Clidder
Flip a Clidlet
D. Flip a Clidlet
Flip a Clidder
E. Compilation fails.

Answer:

E is correct. final methods cannot be overridden.
A, B, C, and D are incorrect based on the above.
(Objective 5.3)
==
class Clidders {
public final void flipper() { System.out.println("Clidder"); }
}
public class Clidlets extends Clidders {
public void flipper() {
System.out.println("Flip a Clidlet");
super.flipper();
}
public static void main(String [] args) {
new Clidlets().flipper();
}
}


SCJP - 10 -Which statement(s) are true? (Choose all that apply.)


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

A. Has-a relationships always rely on inheritance.
B. Has-a relationships always rely on instance variables.
C. Has-a relationships always require at least two class types.
D. Has-a relationships always rely on polymorphism.
E. Has-a relationships are always tightly coupled.

Answer:

B is correct.
A and D describe other OO topics. C is incorrect because a class can have an instance of
itself. E is incorrect because while has-a relationships can lead to tight coupling, it is by no
means always the case. (Objective 5.5)

SCJP - 9 - Given Example


Which statements are true? (Choose all that apply.)
A. The code compiles.
B. If only line 1 is removed the code compiles.
C. If only line 3 is removed the code compiles.
D. If only line 5 is removed the code compiles.
E. If lines 1 and 3 are removed the code compiles.
F. If lines 1, 3 and 5 are removed the code compiles.

Answer:
D and F are correct. Line 5 is the only line that will not compile, because enums cannot be
local to a method.
A, B, C and E are incorrect based on the above.
==
enum A { A }
class E2 {
enum B { B }
void C() {
enum D { D }
}
}

SCJP - 8 - Given Example

What is the result?
A. woof burble
B. Multiple compilation errors
C. Compilation fails due to an error on line 2
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 9

Answer:

A is correct; enums can have constructors and variables.
B, C, D, E, and F are incorrect; these lines all use correct syntax.
==
enum Animals {
DOG("woof"), CAT("meow"), FISH("burble");
String sound;
Animals(String s) { sound = s; }
}
class TestEnum {
static Animals a;
public static void main(String [] args) {
System.out.println(a.DOG.sound + " " + a.FISH.sound);
}
}


SCJP - 7 -Which are legal declarations? (Choose all that apply.)


7. Which are legal declarations? (Choose all that apply.)

A. short x [];
B. short [] y;
C. short[5] x2;
D. short z2 [5];
E. short [] z [] [];
F. short [] y2 = [5];

Answer:

A, B, and E are correct array declarations; E is a three dimensional array.
C, D, and F are incorrect, you can't include the size of your array in a declaration unless
you also instantiate the array object. F uses invalid instantiation syntax. (Objective 1.3)

SCJP - 6 - Given Example



Which, inserted independently at line 6, will compile? (Choose all that apply.)
A. static void doStuff(int... doArgs) { }
B. static void doStuff(int[] doArgs) { }
C. static void doStuff(int doArgs...) { }
D. static void doStuff(int... doArgs, int y) { }
E. static void doStuff(int x, int... doArgs) { }

Answer:

A and E use valid var-args syntax.
B and C are invalid var-arg syntax, and D is invalid because the var-arg must be the last
of a method's arguments. (Objective 1.4)
==
class Voop {
public static void main(String[] args) {
doStuff(1);
doStuff(1,2);
}
// insert code here
}

SCJP - 5 -Which method names follow the JavaBeans standard? (Choose all that apply.)


5. Which method names follow the JavaBeans standard? (Choose all that apply.)

A. addSize
B. getCust
C. deleteRep
D. isColorado
E. putDimensions

Answer:

B and D use the valid prefixes 'get' and 'is'.
A, C, and E are incorrect because 'add', 'delete' and 'put' are not standard JavaBeans
name prefixes. (Objective 1.4)

SCJP - 4 -Which are valid declarations? (Choose all that apply.)


4. Which are valid declarations? (Choose all that apply.)

A. int $x;
B. int 123;
C. int _123;
D. int #dim;
E. int %percent;
F. int *divide;
G. int central_sales_region_Summer_2005_gross_sales;

Answer:

A, C, and G are legal identifiers.
B is incorrect because an identifier can't start with a digit.
D, E, and F are incorrect because identifiers must start with $, _, or a letter. (Objective 1.3)

SCJP - 3 -Which is true? (Choose all that apply.)


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

A. "X extends Y" is correct if and only if X is a class and Y is an interface.
B. "X extends Y" is correct if and only if X is an interface and Y is a class.
C. "X extends Y" is correct if X and Y are either both classes or both interfaces.
D. "X extends Y" is correct for all combinations of X and Y being classes and/or interfaces.

Answer:

C is correct.

A is incorrect because classes implement interfaces, they don't extend them.
B is incorrect because interfaces only "inherit from" other interfaces.
D is incorrect based on the preceding rules. (Objective 1.2)

Saturday 16 March 2013

SCJP - 2 - Which declare a compilable abstract class? (Choose all that apply.)


2. Which declare a compilable abstract class? (Choose all that apply.)

A. public abstract class Canine { public Bark speak(); }
B. public abstract class Canine { public Bark speak() { } }
C. public class Canine { public abstract Bark speak(); }
D. public class Canine abstract { public abstract Bark speak(); }

Answer
:
B is correct. abstract classes don't have to have any abstract methods.

A is incorrect because abstract methods must be marked as such. C is incorrect because
you can't have an abstract method unless the class is abstract. D is incorrect because the
keyword abstract must come before the classname. (Objective 1.1)

SCJP - 1 - Which code fragments will compile?


1. Given the following,
1. interface Base {
2. boolean m1 ();
3. byte m2(short s);
4. }

Which code fragments will compile? (Choose all that apply.)
A. interface Base2 implements Base { }
B. abstract class Class2 extends Base {
public boolean m1() { return true; } }
C. abstract class Class2 implements Base { }
D. abstract class Class2 implements Base {
public boolean m1() { return (true); } }
E. class Class2 implements Base {
boolean m1() { return false; }
byte m2(short s) { return 42; } }

Answer:

C and D are correct. C is correct because an abstract class doesn't have to implement any
or all of its interface's methods. D is correct because the method is correctly implemented.

A is incorrect because interfaces don't implement anything. B is incorrect because classes
don't extend interfaces. E is incorrect because interface methods are implicitly public, so
the methods being implemented must be public. (Objective 1.1)