Pages

Jul 27, 2012

OCPJ Exercise-3 (Basics)






1. Given:
class CardBoard {
 Short story = 200;
 
 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?
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)
2. 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?
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)
3. Given:
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]);
 } 
}
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)
4. Given:
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 "); 
 }
}
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)
5. 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?
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)
6. Given:
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 ");
 }
}
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)
7. Given:
public class Bridge {
 public enum Suits {
  CLUBS(20), DIAMONDS(20), HEARTS(30), SPADES(30),
  NOTRUMP(40) { public int getValue(int bid) {
  return ((bid-1)*30)+40; } };
  Suits(int points) { this.points = points; }
  private int points;
  public int getValue(int bid) { return points * bid; }
 }
 public static void main(String[] args) {
  System.out.println(Suits.NOTRUMP.getBidValue(3));
  System.out.println(Suits.SPADES + " " + Suits.SPADES.points);
  System.out.println(Suits.values());
 }
}
Which are true? (Choose all that apply.)
A. The output could contain 30
B. The output could contain @bf73fa
C. The output could contain DIAMONDS
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
H. Compilation fails due to an error within lines 10 to 12
Answer:
A and B are correct. The code compiles and runs without exception. The values() method returns an array reference, not the contents of the enum, so DIAMONDS is never printed.
C, D, E, F, G, and H are incorrect based on the above. (Objective 1.3)
8. Given:
public class Ouch {
 static int ouch = 7;
 public static void main(String[] args) {
  new Ouch().go(ouch);
  System.out.print(" " + ouch);
 }
 void go(int ouch) {
  ouch++;
 for(int ouch = 3; ouch < 6; ouch++)
  ;
 System.out.print(" " + ouch);
 }
}
What is the result?
A. 5 7
B. 5 8
C. 8 7
D. 8 8
E. Compilation fails
F. An exception is thrown at runtime
Answer:
E is correct. The parameter declared on line 7 is valid (although ugly), but the variable name ouch cannot be declared again on line 9 in the same scope as the declaration on line 7.
A, B, C, D, and F are incorrect based on the above. (Objective 1.3)
9. Given:
public class Bertha {
 static String s = "";
 public static void main(String[] args) {
  int x = 4; Boolean y = true; short[] sa = {1,2,3};
  doStuff(x, y);
  doStuff(x);
  doStuff(sa, sa);
  System.out.println(s);
 }
 static void doStuff(Object o) { s += "1"; }
 static void doStuff(Object... o) { s += "2"; }
 static void doStuff(Integer... i) { s += "3"; }
 static void doStuff(Long L) { s += "4"; }
}
What is the result?
A. 212
B. 232
C. 234
D. 312
E. 332
F. 334
G. Compilation fails
Answer:
A is correct. It's legal to autobox and then widen. The first call to doStuff() boxes the int to an Integer then passes two objects. The second call cannot widen and then box (making the Long method unusable), so it boxes the int to an Integer. As always, a var-args method will be chosen only if no non-var-arg method is possible. The third call is passing two objects—they are of type 'short array.'
B, C, D, E, F, and G are incorrect based on the above. (Objective 3.1)
10. Given:
class Dozens {
 int[] dz = {1,2,3,4,5,6,7,8,9,10,11,12};
}
public class Eggs {
 public static void main(String[] args) {
  Dozens [] da = new Dozens[3];
  da[0] = new Dozens();
  Dozens d = new Dozens();
  da[1] = d;
  d = null;
  da[1] = null;
  // do stuff
 }
}
Which two are true about the objects created within main(), and eligible for garbage collection when line 12 is reached?
A. Three objects were created
B. Four objects were created
C. Five objects were created
D. Zero objects are eligible for GC
E. One object is eligible for GC
F. Two objects are eligible for GC
G. Three objects are eligible for GC
Answer:
C and F are correct. da refers to an object of type "Dozens array," and each Dozens object that is created comes with its own "int array" object. When line 14 is reached, only the second Dozens object (and its "int array" object) are not reachable.
A, B, D, E, and G are incorrect based on the above. (Objective 7.4)
11. Given:
class Beta { }
class Alpha {
 static Beta b1;
 Beta b2;
}
public class Tester {
 public static void main(String[] args) {
  Beta b1 = new Beta(); Beta b2 = new Beta();
  Alpha a1 = new Alpha(); Alpha a2 = new Alpha();
  a1.b1 = b1;
  a1.b2 = b1;
  a2.b2 = b2;
  a1 = null; b1 = null; b2 = null;
  // do stuff
 }
}
When line 16 is reached, how many objects will be eligible for garbage collection?
A. 0
B. 1
C. 2
D. 3
E. 4
F. 5
Answer:
B is correct. It should be clear that there is still a reference to the object referred to by a2, and that there is still a reference to the object referred to by a2.b2. What might be less clear is that you can still access the other Beta object through the static variable a2.b1—because it's static.
A, C, D, E, and F are incorrect based on the above. (Objective 7.4)
12. Given:
class Box {
 int size;
 Box(int s) { size = s; }
}
public class Laser {
 public static void main(String[] args) {
  Box b1 = new Box(5);
  Box[] ba = go(b1, new Box(6));
  ba[0] = b1;
  for(Box b : ba) System.out.print(b.size + " ");
 }
 static Box[] go(Box b1, Box b2) {
  b1.size = 4;
  Box[] ma = {b2, b1};
  return ma;
 }
}
What is the result?
A. 4 4
B. 5 4
C. 6 4
D. 4 5
E. 5 5
F. Compilation fails
Answer:
A is correct. Although main()'s b1 is a different reference variable than go()'s b1, they refer to the same Box object.
B, C, D, E, and F are incorrect based on the above. (Objective 7.3)
13. Given:
public class Dark {
 int x = 3;
 public static void main(String[] args) {
  new Dark().go1();
 }
 void go1() {
  int x;
  go2(++x);
 }
 void go2(int y) {
  int x = ++y;
  System.out.println(x);
 }
}
what is the result?
A. 2
B. 3
C. 4
D. 5
E. Compilation fails
F. An exception is thrown at runtime
Answer:
E is correct. In go1() the local variable x is not initialized.
A, B, C, D, and F are incorrect based on the above. (Objective 1.3)


regards,
Tech Dexters Support Team,
Hyderabad.


for Feedback and Support simply drop a mail in
techdexters{at}gmail.com
Remember to replace the {at} with @ in the email addresses


Jul 24, 2012

Basics


Bullet Points on some Basic Concepts:

Stack and Heap
  • Local variables (method variables) live on the stack.
  • Objects and their instance variables live on the heap.
Literals and Primitive Casting (Objective 1.3)
  • Integer literals can be decimal, octal (e.g. 013), or hexadecimal (e.g. 0x3d).
  • Literals for longs end in L or l.
  • Float literals end in F or f, double literals end in a digit or D or d.
  • The boolean literals are true and false.
  • Literals for chars are a single character inside single quotes: 'd'.
Scope (Objectives 1.3 and 7.6)
  • Scope refers to the lifetime of a variable.
  • There are four basic scopes:
    • Static variables live basically as long as their class lives.
    • Instance variables live as long as their object lives.
    • Local variables live as long as their method is on the stack; however, if their method invokes another method, they are temporarily unavailable.
    • Block variables (e.g., in a for or an if) live until the block completes.
Basic Assignments (Objectives 1.3 and 7.6)
  • Literal integers are implicitly ints.
  • Integer expressions always result in an int-sized result, never smaller.
  • Floating-point numbers are implicitly doubles (64 bits).
  • Narrowing a primitive truncates the high order bits.
  • Compound assignments (e.g. +=), perform an automatic cast.
  • A reference variable holds the bits that are used to refer to an object.
  • Reference variables can refer to subclasses of the declared type but not to superclasses.
  • When creating a new object, e.g., Button b = new Button(); three things happen:
    • Make a reference variable named b, of type Button
    • Create a new Button object
    • Assign the Button object to the reference variable b 
Using a Variable or Array Element That Is Uninitialized and Unassigned (Objectives 1.3 and 7.6)
  • When an array of objects is instantiated, objects within the array are not instantiated automatically, but all the references get the default value of null.
  • When an array of primitives is instantiated, elements get default values.
  • Instance variables are always initialized with a default value.
  • Local/automatic/method variables are never given a default value. If you attempt to use one before initializing it, you'll get a compiler error.
Passing Variables into Methods (Objective 7.3)
  • Methods can take primitives and/or object references as arguments.
  • Method arguments are always copies.
  • Method arguments are never actual objects (they can be references to objects).
  • A primitive argument is an unattached copy of the original primitive.
  • A reference argument is another copy of a reference to the original object.
  • Shadowing occurs when two variables with different scopes share the same name. This leads to hard-to-find bugs, and hard-to-answer exam questions.
Array Declaration, Construction, and Initialization (Obj. 1.3)
  • Arrays can hold primitives or objects, but the array itself is always an object.
  • When you declare an array, the brackets can be left or right of the name.
  • It is never legal to include the size of an array in the declaration.
  • You must include the size of an array when you construct it (using new) unless you are creating an anonymous array.
  • Elements in an array of objects are not automatically created, although primitive array elements are given default values.
  • You'll get a NullPointerException if you try to use an array element in an object array, if that element does not refer to a real object.
  • Arrays are indexed beginning with zero.
  • An ArrayIndexOutOfBoundsException occurs if you use a bad index value.
  • Arrays have a length variable whose value is the number of array elements.
  • The last index you can access is always one less than the length of the array.
  • Multidimensional arrays are just arrays of arrays.
  • The dimensions in a multidimensional array can have different lengths.
  • An array of primitives can accept any value that can be promoted implicitly to the array's declared type;. e.g., a byte variable can go in an int array.
  • An array of objects can hold any object that passes the IS-A (or instanceof) test for the declared type of the array. For example, if Horse extends Animal, then a Horse object can go into an Animal array.
  • If you assign an array to a previously declared array reference, the array you're assigning must be the same dimension as the reference you're assigning it to.
  • You can assign an array of one type to a previously declared array reference of one of its supertypes. For example, a Honda array can be assigned to an array declared as type Car (assuming Honda extends Car).
Initialization Blocks (Objectives 1.3 and 7.6)
  • Static initialization blocks run once, when the class is first loaded.
  • Instance initialization blocks run every time a new instance is created. They run after all super-constructors and before the constructor's code has run.
  • If multiple init blocks exist in a class, they follow the rules stated above, AND they run in the order in which they appear in the source file.
Using Wrappers (Objective 3.1)
  • The wrapper classes correlate to the primitive types.
  • Wrappers have two main functions:
    • To wrap primitives so that they can be handled like objects
    • To provide utility methods for primitives (usually conversions)
  • The three most important method families are
    • xxxValue() Takes no arguments, returns a primitive
    • parseXxx() Takes a String, returns a primitive, throws NFE
    • valueOf() Takes a String, returns a wrapped object, throws NFE
  • Wrapper constructors can take a String or a primitive, except for Character, which can only take a char.
  • Radix refers to bases (typically) other than 10; octal is radix = 8, hex = 16. 
Boxing (Objective 3.1)
  • As of Java 5, boxing allows you to convert primitives to wrappers or to convert wrappers to primitives automatically.
  • Using == with wrappers created through boxing is tricky; those with the same small values (typically lower than 127), will be ==, larger values will not be ==.
Advanced Overloading (Objectives 1.5 and 5.4)
  • Primitive widening uses the "smallest" method argument possible.
  • Used individually, boxing and var-args are compatible with overloading.
  • You CANNOT widen from one wrapper type to another. (IS-A fails.)
  • You CANNOT widen and then box. (An int can't become a Long.)
  • You can box and then widen. (An int can become an Object, via an Integer.)
  • You can combine var-args with either widening or boxing.
Garbage Collection (Objective 7.4)
  • In Java, garbage collection (GC) provides automated memory management.
  • The purpose of GC is to delete objects that can't be reached.
  • Only the JVM decides when to run the GC, you can only suggest it.
  • You can't know the GC algorithm for sure.
  • Objects must be considered eligible before they can be garbage collected.
  • An object is eligible when no live thread can reach it.
  • To reach an object, you must have a live, reachable reference to that object.
  • Java applications can run out of memory.
  • Islands of objects can be GCed, even though they refer to each other.
  • Request garbage collection with System.gc();
  • Class Object has a finalize() method.
  • The finalize() method is guaranteed to run once and only once before the garbage collector deletes an object.
  • The garbage collector makes no guarantees, finalize() may never run.
  • You can uneligibilize an object for GC from within finalize().

regards,
Tech Dexters Support Team,
Hyderabad.


for Feedback and Support simply drop a mail in
techdexters{at}gmail.com
Remember to replace the {at} with @ in the email addresses

Jul 18, 2012

OCPJ Exercise-2 (Object Orientation)




1. Given:
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) { }
}
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)


2. Given:
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(" ");
   } 
}
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)


3. Given:
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();
  }
}
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)


4. Using the fragments below, complete the following code so it compiles. Note, you may not have to fill all of the slots.
Code:

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 ( ) { } ;

Answer:
class AgedP {
AgedP() {}
public AgedP(int x) {
}
}
public class Kinder extends AgedP {
public Kinder(int x) {
super();
}
}
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 noargument 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 noargument version, this constructor must be created. It will not be created by the compiler because there is another constructor already present.(Objective 5.4)


5 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)


6. Given the following,
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
  }
}
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)


7. Given:
1. ClassA has a ClassD
2. Methods in ClassA use public methods in ClassB
3. Methods in ClassC use public methods in ClassA
4. Methods in ClassA use public variables in ClassB
Which is most likely true? (Choose the most likely.) A. ClassD has low cohesion
B. ClassA has weak encapsulation
C. ClassB has weak encapsulation
D. ClassB has strong encapsulation
E. ClassC is tightly coupled to ClassA
Answer:
C is correct. Generally speaking, public variables are a sign of weak encapsulation.
A, B, D, and E are incorrect, because based on the information given, none of these statements can be supported. (Objective 5.1)


8. Given:
class Dog {
  public void bark() { System.out.print("woof "); }
}
class Hound extends Dog {
  public void sniff() { System.out.print("sniff "); }
  public void bark() { System.out.print("howl "); }
}
public class DogShow {
  public static void main(String[] args) { new DogShow().go(); }
  void go() {
    new Hound().bark();
    ((Dog) new Hound()).bark();
    ((Dog) new Hound()).sniff();
  }
}
What is the result? (Choose all that apply.)
A. howl howl sniff
B. howl woof sniff
C. howl howl followed by an exception
D. howl woof followed by an exception
E. Compilation fails with an error at line 12
F. Compilation fails with an error at line 13
Answer:
F is correct. Class Dog doesn't have a sniff method.
A, B, C, D, and E are incorrect based on the above information. (Objective 5.2)


9. Given:
public class Redwood extends Tree {
   public static void main(String[] args) {
     new Redwood().go();
   }
   void go() {
     go2(new Tree(), new Redwood());
     go2((Redwood) new Tree(), new Redwood());
   }
   void go2(Tree t1, Redwood r1) {
     Redwood r2 = (Redwood)t1;
     Tree t2 = (Tree)r1;
   }
}
class Tree { }
What is the result? (Choose all that apply.)
A. An exception is thrown at runtime
B. The code compiles and runs with no output
C. Compilation fails with an error at line 6
D. Compilation fails with an error at line 7
E. Compilation fails with an error at line 10
F. Compilation fails with an error at line 11
Answer:
A is correct, a ClassCastException will be thrown when the code attempts to downcast a Tree to a Redwood.
B, C, D, E, and F are incorrect based on the above information.(Objective 5.2)


10. Given:
public class Tenor extends Singer {
   public static String sing() { return "fa"; }
   public static void main(String[] args) {
     Tenor t = new Tenor();
     Singer s = new Tenor();
     System.out.println(t.sing() + " " + s.sing());
   }
}
class Singer { public static String sing() { return "la"; } }
What is the result?
A. fa fa
B. fa la
C. la la
D. Compilation fails
E. An exception is thrown at runtime
Answer:
B is correct. The code is correct, but polymorphism doesn't apply to static methods.
A, C, D, and E are incorrect based on the above information.(Objective 5.2)


11. Given:
class Alpha {
  static String s = " ";
  protected Alpha() { s += "alpha "; }
}
class SubAlpha extends Alpha {
  private SubAlpha() { s += "sub "; }
}
public class SubSubAlpha extends Alpha {
  private SubSubAlpha() { s += "subsub "; }
  public static void main(String[] args) {
    new SubSubAlpha();
    System.out.println(s);
  }
}
What is the result? A. subsub
B. sub subsub
C. alpha subsub
D. alpha sub subsub
E. Compilation fails
F. An exception is thrown at runtime
Answer:
C is correct. Watch out, SubSubAlpha extends Alpha! Since the code doesn't attempt to make a SubAlpha, the private constructor in SubAlpha is okay.
A, B, D, E, and F are incorrect based on the above information. (Objective 5.3)


12. Given:
class Building {
   Building() { System.out.print("b "); }
   Building(String name) {
      this(); 
      System.out.print("bn " + name);
   }
}
public class House extends Building {
   House() { System.out.print("h "); }
   House(String name) {
      this(); 
      System.out.print("hn " + name);
   }
   public static void main(String[] args) { new House("x "); }
}
what is the result?
A. h hn x
B. hn x h
C. b h hn x
D. b hn x h
E. bn x h hn x
F. b bn x h hn x
G. bn x b h hn x
H. Compilation fails
Answer:
C is correct. Remember that constructors call their superclass constructors, which execute first, and that constructors can be overloaded.
A, B, D, E, F, G, and H are incorrect based on the above information.(Objectives 1.6, 5.4)


13. Given:
class Mammal {
String name = "furry ";
String makeNoise() { return "generic noise"; }
}
class Zebra extends Mammal {
String name = "stripes ";
String makeNoise() { return "bray"; }
}
public class ZooKeeper {
public static void main(String[] args) { new ZooKeeper().go(); }
void go() {
Mammal m = new Zebra();
System.out.println(m.name + m.makeNoise());
}
}
What is the result?
A. furry bray
B. stripes bray
C. furry generic noise
D. stripes generic noise
E. Compilation fails
F. An exception is thrown at runtime
Answer:
A is correct. Polymorphism is only for instance methods.
B, C, D, E, and F are incorrect based on the above information.(Objectives 1.5, 5.4)


14. You're designing a new online board game in which Floozels are a type of Jammers, Jammers can have Quizels, Quizels are a type of Klakker, and Floozels can have several Floozets. Which of the following fragments represent this design? (Choose all that apply.)
A.
import java.util.*;
interface Klakker { }
class Jammer { Set q; }
class Quizel implements Klakker { }
public class Floozel extends Jammer { List f; }
interface Floozet { }
B.
import java.util.*;
class Klakker { Set q; }
class Quizel extends Klakker { }
class Jammer { List f; }
class Floozet extends Floozel { }
public class Floozel { Set k; }
C.
import java.util.*;
class Floozet { }
class Quizel implements Klakker { }
class Jammer { List q; }
interface Klakker { }
class Floozel extends Jammer { List f; }
D.
import java.util.*;
interface Jammer extends Quizel { }
interface Klakker { }
interface Quizel extends Klakker { }
interface Floozel extends Jammer, Floozet { }
interface Floozet { }
Answer:
A and C are correct. The phrase "type of" indicates an "is-a" relationship (extends or implements), and the phrase “have” is of course a "has-a" relationship (usually instance variables).
B and D are incorrect based on the above information. (Objective 5.5)


15. Given:
class A { }

class B extends A { }

public class ComingThru {
  static String s = "-";
  
  public static void main(String[] args) {
  A[] aa = new A[2];
  B[] ba = new B[2];
  sifter(aa);
  sifter(ba);
  sifter(7);
  
  System.out.println(s);
  }
  
  static void sifter(A[]... a2) { s += "1"; }
  static void sifter(B[]... b1) { s += "2"; }
  static void sifter(B[] b1) { s += "3"; }
  static void sifter(Object o) { s += "4"; }
}
What is the result?
A. -124
B. -134
C. -424
D. -434
E. -444
F. Compilation fails
Answer:
D is correct. In general, overloaded var-args methods are chosen last. Remember that arrays are objects. Finally, an int can be boxed to an Integer and then "widened" to an Object.
A, B, C, E, and F are incorrect based on the above information.(Objective 1.5)


regards,
Tech Dexters Support Team,
Hyderabad.


for Feedback and Support simply drop a mail in
techdexters{at}gmail.com
Remember to replace the {at} with @ in the email addresses

Jul 11, 2012

Object Orientation


Encapsulation, IS-A, HAS-A (Objective 5.1)

  1. Encapsulation helps hide implementation behind an interface (or API).
  2. Encapsulated code has two features:
    • Instance variables are kept protected (usually with the private modifier).
    • Getter and setter methods provide access to instance variables.
  3. IS-A refers to inheritance or implementation.
  4. IS-A is expressed with the keyword extends.
  5. IS-A, "inherits from," and "is a subtype of " are all equivalent expressions.
  6. HAS-A means an instance of one class "has a" reference to an instance of another class or another instance of the same class.

Inheritance (Objective 5.5)

  1. Inheritance allows a class to be a subclass of a superclass, and thereby inherit public and protected variables and methods of the superclass.
  2. Inheritance is a key concept that underlies IS-A, polymorphism, overriding, overloading, and casting.
  3. All classes (except class Object), are subclasses of type Object, and therefore they inherit Object's methods.

Polymorphism (Objective 5.2)

  1. Polymorphism means "many forms."
  2. A reference variable is always of a single, unchangeable type, but it can refer to a subtype object.
  3. A single object can be referred to by reference variables of many different types —as long as they are the same type or a supertype of the object.
  4. The reference variable's type (not the object's type), determines which methods can be called!
  5. Polymorphic method invocations apply only to overridden instance methods.

Overriding and Overloading (Objectives 1.5 and 5.4)

  1. Methods can be overridden or overloaded; constructors can be overloaded but not overridden.
  2. Abstract methods must be overridden by the first concrete (non-abstract) subclass.
  3. With respect to the method it overrides, the overriding method
    • Must have the same argument list.
    • Must have the same return type, except that as of Java 5, the return type can be a subclass—this is known as a covariant return.
    • Must not have a more restrictive access modifier.
    • May have a less restrictive access modifier.
    • Must not throw new or broader checked exceptions.
    • May throw fewer or narrower checked exceptions, or any unchecked exception.
  4. final methods cannot be overridden.
  5. Only inherited methods may be overridden, and remember that private methods are not inherited.
  6. A subclass uses super.overriddenMethodName() to call the superclass version of an overridden method.
  7. Overloading means reusing a method name, but with different arguments.
  8. Overloaded methods
    • Must have different argument lists
    • May have different return types, if argument lists are also different
    • May have different access modifiers
    • May throw different exceptions
  9. Methods from a superclass can be overloaded in a subclass.
  10. Polymorphism applies to overriding, not to overloading.
  11. Object type (not the reference variable's type), determines which overridden method is used at runtime.
  12. Reference type determines which overloaded method will be used at compile time.

Reference Variable Casting (Objective 5.2)

  1. There are two types of reference variable casting: downcasting and upcasting.
  2. Downcasting: If you have a reference variable that refers to a subtype object, you can assign it to a reference variable of the subtype. You must make anexplicit cast to do this, and the result is that you can access the subtype's members with this new reference variable.
  3. Upcasting: You can assign a reference variable to a supertype reference variable explicitly or implicitly. This is an inherently safe operation because the assignment restricts the access capabilities of the new variable. 

Implementing an Interface (Objective 1.2)

  1. When you implement an interface, you are fulfilling its contract.
  2. You implement an interface by properly and concretely overriding all of the methods defined by the interface.
  3. A single class can implement many interfaces.

Return Types (Objective 1.5)

  1. Overloaded methods can change return types; overridden methods cannot, except in the case of covariant returns.
  2. Object reference return types can accept null as a return value.
  3. An array is a legal return type, both to declare and return as a value.
  4. For methods with primitive return types, any value that can be implicitly converted to the return type can be returned.
  5. Nothing can be returned from a void, but you can return nothing. You're allowed to simply say return, in any method with a void return type, to bust out of a method early. But you can't return nothing from a method with a non-void return type.
  6. Methods with an object reference return type, can return a subtype.
  7. Methods with an interface return type, can return any implementer.

Constructors and Instantiation (Objectives 1.6 and 5.4)

  1. A constructor is always invoked when a new object is created.
  2. Each superclass in an object's inheritance tree will have a constructor called.
  3. Every class, even an abstract class, has at least one constructor.
  4. Constructors must have the same name as the class.
  5. Constructors don't have a return type. If you see code with a return type, it's a method with the same name as the class, it's not a constructor.
  6. Typical constructor execution occurs as follows:
    • The constructor calls its superclass constructor, which calls its superclass constructor, and so on all the way up to the Object constructor.
    • The Object constructor executes and then returns to the calling constructor, which runs to completion and then returns to its calling constructor, and so on back down to the completion of the constructor of the actual instance being created.
  7. Constructors can use any access modifier (even private!).
  8. The compiler will create a default constructor if you don't create any constructors in your class.
  9. The default constructor is a no-arg constructor with a no-arg call to super().
  10. The first statement of every constructor must be a call to either this() (an overloaded constructor) or super().
  11. The compiler will add a call to super() unless you have already put in a call to this() or super().
  12. Instance members are accessible only after the super constructor runs.
  13. Abstract classes have constructors that are called when a concrete subclass is instantiated.
  14. Interfaces do not have constructors.
  15. If your superclass does not have a no-arg constructor, you must create a constructor and insert a call to super() with arguments matching those of the superclass constructor.
  16. Constructors are never inherited, thus they cannot be overridden. 
  17. A constructor can be directly invoked only by another constructor (using a call to super() or this()).
  18. Issues with calls to this()
    • May appear only as the first statement in a constructor.
    • The argument list determines which overloaded constructor is called. 
    • Constructors can call constructors can call constructors, and so on, but sooner or later one of them better call super() or the stack will explode.
    • Calls to this() and super() cannot be in the same constructor. You can have one or the other, but never both.

Statics (Objective 1.3)

  1. Use static methods to implement behaviors that are not affected by the state of any instances.
  2. Use static variables to hold data that is class specific as opposed to instance specific—there will be only one copy of a static variable.
  3. All static members belong to the class, not to any instance.
  4. A static method can't access an instance variable directly.
  5. static methods can't be overridden, but they can be redefined.
  6. Use the dot operator to access static members, but remember that using a reference variable with the dot operator is really a syntax trick, and the compiler will substitute the class name for the reference variable, for instance:

d.doStuff();
becomes:
Dog.doStuff();


Coupling and Cohesion (Objective 5.1)

  1. Coupling refers to the degree to which one class knows about or uses members of another class.
  2. Loose coupling is the desirable state of having classes that are well encapsulated, minimize references to each other, and limit the breadth of API usage.
  3. Tight coupling is the undesirable state of having classes that break the rules of loose coupling.
  4. Cohesion refers to the degree in which a class has a single, well-defined role or responsibility.
  5. High cohesion is the desirable state of a class whose members support a single, well-focused role or responsibility.
  6. Low cohesion is the undesirable state of a class whose members support multiple, unfocused roles or responsibilities.


regards,
Tech Dexters Support Team,
Hyderabad.


for Feedback and Support simply drop a mail in
techdexters{at}gmail.com
Remember to replace the {at} with @ in the email addresses

Jul 6, 2012

OCPJ Exercise - I


1. 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)

2. 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 is incorrect because 'add' can be used only with Listener methods. C and E are incorrect because 'delete' and 'put' are not standard JavaBeans name prefixes. (Objective 1.4)

3. Given:
  1. class Voop {
  2. public static void main(String[] args) {
  3. doStuff(1);
  4. doStuff(1,2);
  5. }
  6.  // insert code here
  7. }
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)

4. Given:
  1. enum Animals {
  2. DOG("woof"), CAT("meow"), FISH("burble");
  3. String sound;
  4. Animals(String s) { sound = s; }
  5. }
  6. class TestEnum {
  7. static Animals a;
  8. public static void main(String [] args) {
  9. System.out.println(a.DOG.sound + " " + a.FISH.sound);
  10. }
  11. }

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. (Objective 1.3)


5. Given two files:

1. package pkgA;
2. public class Foo {
3. int a = 5;
4. protected int b = 6;
5. public int c = 7;
6. }

3. package pkgB;
4. import pkgA.*;
5. public class Baz {
6. public static void main(String[] args) {
7. Foo f = new Foo();
8. System.out.print(" " + f.a);
9. System.out.print(" " + f.b);
10. System.out.print(" " + f.c);
11. }
12. }

What is the result? (Choose all that apply.)
A. 5 6 7
B. 5 followed by an exception
C. Compilation fails with an error on line 7
D. Compilation fails with an error on line 8
E. Compilation fails with an error on line 9
F. Compilation fails with an error on line 10
Answer:
D and E are correct. Variable a has default access, so it cannot be accessed from outside the package. Variable b has protected access in pkgA.
A, B, C, and F are incorrect based on the above information. (Objective 1.1)


6. Given:
1. public class Electronic implements Device
   { public void doIt() { } }
2.
3. abstract class Phone1 extends Electronic { }
4.
5. abstract class Phone2 extends Electronic
   { public void doIt(int x) { } }
6.
7. class Phone3 extends Electronic implements Device
   { public void doStuff() { } }
8.
9. interface Device { public void doIt(); }
What is the result? (Choose all that apply.)
A. Compilation succeeds
B. Compilation fails with an error on line 1
C. Compilation fails with an error on line 3
D. Compilation fails with an error on line 5
E. Compilation fails with an error on line 7
F. Compilation fails with an error on line 9
Answer:
A is correct; all of these are legal declarations.
B, C, D, E, and F are incorrect based on the above information. (Objective 1.2)


7. Given:
4. class Announce {
5. public static void main(String[] args) {
6. for(int __x = 0; __x < 3; __x++) ;
7. int #lb = 7;
8. long [] x [5];
9. Boolean []ba[];
10. enum Traffic { RED, YELLOW, GREEN };
11. }
12. }
What is the result? (Choose all that apply.)
A. Compilation succeeds
B. Compilation fails with an error on line 6
C. Compilation fails with an error on line 7
D. Compilation fails with an error on line 8
E. Compilation fails with an error on line 9
F. Compilation fails with an error on line 10
Answer:
C, D, and F are correct. Variable names cannot begin with a #, an array declaration can’t include a size without an instantiation, and enums can’t be declared within a method.
A, B, and E are incorrect based on the above information. (Objective 1.3)


8. Given:
  3. public class TestDays {
  4. public enum Days { MON, TUE, WED };
  5. public static void main(String[] args) {
  6. for(Days d : Days.values() )
  7. ;
  8. Days [] d2 = Days.values();
  9. System.out.println(d2[2]);
  10. }
  11. }
What is the result? (Choose all that apply.)
A. TUE
B. WED
C. The output is unpredictable
D. Compilation fails due to an error on line 4
E. Compilation fails due to an error on line 6
F. Compilation fails due to an error on line 8
G. Compilation fails due to an error on line 9
Answer:
B is correct. Every enum comes with a static values() method that returns an array of the enum's values, in the order in which they are declared in the enum.
A, C, D, E, F, and G are incorrect based on the above information. (Objective 1.3)


9. Given:
 4. public class Frodo extends Hobbit {
 5. public static void main(String[] args) {
 6. Short myGold = 7;
 7. System.out.println(countGold(myGold, 6));
 8. }
 9. }
 10. class Hobbit {
 11. int countGold(int x, int y) { return x + y; }
 12. }
What is the result?
A. 13
B. Compilation fails due to multiple errors
C. Compilation fails due to an error on line 6
D. Compilation fails due to an error on line 7
E. Compilation fails due to an error on line 11
Answer:
D is correct. The Short myGold is autoboxed correctly, but the countGold() method cannot be invoked from a static context.
A, B, C, and E are incorrect based on the above information. (Objective 1.4)



regards,
Tech Dexters Support Team,
Hyderabad.

for Feedback and Support simply drop a mail in
techdexters{at}gmail.com
Remember to replace the {at} with @ in the email addresses




Jul 5, 2012

Declaration and Access Control


Identifiers (Objective 1.3)

  •  Identifiers can begin with a letter, an underscore, or a currency character.
  •  After the first character, identifiers can also include digits.
  •  Identifiers can be of any length.
  •  JavaBeans methods must be named using camelCase, and depending on the method's purpose, must start with set, get, is, add, or remove.

Declaration Rules (Objective 1.1)

  • A source code file can have only one public class.
  • If the source file contains a public class, the filename must match the public class name.
  • A file can have only one package statement, but multiple imports.
  • The package statement (if any) must be the first (non-comment) line in a source file.
  • The import statements (if any) must come after the package and before the class declaration.
  • If there is no package statement, import statements must be the first (noncomment) statements in the source file.
  • package and import statements apply to all classes in the file.
  • A file can have more than one nonpublic class.
  • Files with no public classes have no naming restrictions.

Class Access Modifiers (Objective 1.1)

  • There are three access modifiers: public, protected, and private.
  • There are four access levels: public, protected, default, and private.
  • Classes can have only public or default access.
  • A class with default access can be seen only by classes within the same package.
  • A class with public access can be seen by all classes from all packages.
  • Class visibility revolves around whether code in one class can
    • Create an instance of another class
    • Extend (or subclass), another class
    • Access methods and variables of another class

Class Modifiers (Nonaccess) (Objective 1.2)

  • Classes can also be modified with final, abstract, or strictfp.
  • A class cannot be both final and abstract.
  • A final class cannot be subclassed.
  • An abstract class cannot be instantiated.
  • A single abstract method in a class means the whole class must be abstract.
  • An abstract class can have both abstract and nonabstract methods.
  • The first concrete class to extend an abstract class must implement all of its abstract methods.

 Member Access Modifiers (Objectives 1.3 and 1.4)

  • Methods and instance (nonlocal) variables are known as "members."
  • Members can use all four access levels: public, protected, default, private.
  • Member access comes in two forms:
    • Code in one class can access a member of another class.
    • A subclass can inherit a member of its superclass.
  • If a class cannot be accessed, its members cannot be accessed.
  • Determine class visibility before determining member visibility.
  • public members can be accessed by all other classes, even in other packages.
  • If a superclass member is public, the subclass inherits it—regardless of package.
  • Members accessed without the dot operator (.) must belong to the same class.
  • this. always refers to the currently executing object.
  • this.aMethod() is the same as just invoking aMethod().
  • private members can be accessed only by code in the same class.
  • private members are not visible to subclasses, so private members cannot be inherited.



Static Variables and Methods (Objective 1.4)

  • They are not tied to any particular instance of a class.
  • No classes instances are needed in order to use static members of the class.
  • There is only one copy of a static variable / class and all instances share it.
  • static methods do not have direct access to non-static members.

Enums (Objective 1.3)

  • An enum specifies a list of constant values assigned to a type.
  • An enum is NOT a String or an int; an enum constant's type is the enum type. For example, SUMMER and FALL are of the enum type Season.
  • An enum can be declared outside or inside a class, but NOT in a method.
  • An enum declared outside a class must NOT be marked static, final, abstract, protected, or private.
  • Enums can contain constructors, methods, variables, and constant class bodies.
  • enum constants can send arguments to the enum constructor, using the syntax BIG(8), where the int literal 8 is passed to the enum constructor.
  • enum constructors can have arguments, and can be overloaded.
  • enum constructors can NEVER be invoked directly in code. They are always called automatically when an enum is initialized.
  • The semicolon at the end of an enum declaration is optional. These are legal:
  • enum Foo { ONE, TWO, THREE}
  • enum Foo { ONE, TWO, THREE};
  • MyEnum.values() returns an array of MyEnum's values.

Methods with var-args (Objective 1.4)

  • As of Java 5, methods can declare a parameter that accepts from zero to many arguments, a so-called var-arg method.
  • A var-arg parameter is declared with the syntax type... name; for instance:                                                  doStuff(int... x) { }
  • A var-arg method can have only one var-arg parameter.
  • In methods with normal parameters and a var-arg, the var-arg must come last.




Jul 3, 2012

OCPJ Exam Objectives and Training Topics



Hi All,
Let us start a training process of Oracle Certificate in Java..
In series of further posts, We are discussing about following topics. 
  1. Declarations and Access Control
  2. Object Orientation
  3. Assignments
  4. Operators 
  5. Flow Control
  6. Exceptions
  7. Assertions
  8. Strings
  9. Input / Output
  10. Formatting, and Parsing 
  11. Generics and Collections
  12. Inner Classes
  13. Threads
  14. Development

For every topic I will give you some Drill Points later followed by some practice questions with solution and Explanation. At each topic we will specify the Exam Objective of the given problem.

Exam Objectives:
Prior to the traning session. I want to share Exam Objectives of OCPJ with you..

Objective 1: Declarations, Initialization and Scoping

  1. Develop code that declares classes (including abstract and all forms of nested classes), interfaces, and enums, and includes the appropriate use of package and import statements (including static imports).
  2. Develop code that declares an interface. Develop code that implements or extends one or more interfaces. Develop code that declares an abstract class. Develop code that extends an abstract class.
  3. Develop code that declares, initializes, and uses primitives, arrays, enums, and objects as static, instance, and local variables. Also, use legal identifiers for variable names.
  4. Given a code example, determine if a method is correctly overriding or overloading another method, and identify legal return values (including covariant returns), for the method.
  5. Given a set of classes and superclasses, develop constructors for one or more of the classes. Given a class declaration, determine if a default constructor will be created, and if so, determine the behavior of that constructor. Given a nested or non-nested class listing, write code to instantiate the class.

Objective 2: Flow Control

  1. Develop code that implements an if or switch statement; and identify legal argument types for these statements.
  2. Develop code that implements all forms of loops and iterators, including the use of for, the enhanced for loop (for-each), do, while, labels, break, and continue; and explain the values taken by loop counter variables during and after loop execution.
  3. Develop code that makes use of assertions, and distinguish appropriate from inappropriate uses of assertions.
  4. Develop code that makes use of exceptions and exception handling clauses (try, catch, finally), and declares methods and overriding methods that throw exceptions.
  5. Recognize the effect of an exception arising at a specified point in a code fragment. Note that the exception may be a runtime exception, a checked exception, or an error.
  6. Recognize situations that will result in any of the following being thrown: ArrayIndexOutOfBoundsException,ClassCastException, IllegalArgumentException, IllegalStateException, NullPointerException, NumberFormatException, AssertionError, ExceptionInInitializerError, StackOverflowError or NoClassDefFoundError. Understand which of these are thrown by the virtual machine and recognize situations in which others should be thrown programatically.

Objective 3: API Contents

  1. Develop code that uses the primitive wrapper classes (such as Boolean, Character, Double, Integer, etc.), and/or autoboxing & unboxing. Discuss the differences between the String, StringBuilder, and StringBuffer classes.
  2. Given a scenario involving navigating file systems, reading from files, writing to files, or interacting with the user, develop the correct solution using the following classes (sometimes in combination), from java.io: BufferedReader, BufferedWriter, File, FileReader, FileWriter, PrintWriter, and Console.
  3. Use standard J2SE APIs in the java.text package to correctly format or parse dates, numbers, and currency values for a specific locale; and, given a scenario, determine the appropriate methods to use if you want to use the default locale or a specific locale. Describe the purpose and use of the java.util.Locale class.
  4. Write code that uses standard J2SE APIs in the java.util and java.util.regex packages to format or parse strings or streams. For strings, write code that uses the Pattern and Matcher classes and the String.split method. Recognize and use regular expression patterns for matching (limited to: . (dot), * (star), + (plus), ?, \d, \s, \w, [], ()). The use of *, +, and ? will be limited to greedy quantifiers, and the parenthesis operator will only be used as a grouping mechanism, not for capturing content during matching. For streams, write code using the Formatter and Scanner classes and the PrintWriter.format/printf methods. Recognize and use formatting parameters (limited to: %b, %c, %d, %f, %s) in format strings.

Objective 4: Concurrency

  1. Write code to define, instantiate, and start new threads using both java.lang.Thread and java.lang.Runnable.
  2. Recognize the states in which a thread can exist, and identify ways in which a thread can transition from one state to another.
  3. Given a scenario, write code that makes appropriate use of object locking to protect static or instance variables from concurrent access problems.

Objective 5: OO Concepts

  1. Develop code that implements tight encapsulation, loose coupling, and high cohesion in classes, and describe the benefits.
  2. Given a scenario, develop code that demonstrates the use of polymorphism. Further, determine when casting will be necessary and recognize compiler vs. runtime errors related to object reference casting.
  3. Explain the effect of modifiers on inheritance with respect to constructors, instance or static variables, and instance or static methods.
  4. Given a scenario, develop code that declares and/or invokes overridden or overloaded methods and code that declares and/or invokes superclass, or overloaded constructors.
  5. Develop code that implements "is-a" and/or "has-a" relationships.

Objective 6: Collections / Generics

  1. Given a design scenario, determine which collection classes and/or interfaces should be used to properly implement that design, including the use of the Comparable interface.
  2. Distinguish between correct and incorrect overrides of corresponding hashCode and equals methods, and explain the difference between == and the equals method.
  3. Write code that uses the generic versions of the Collections API, in particular, the Set, List, and Map interfaces and implementation classes. Recognize the limitations of the non-generic Collections API and how to refactor code to use the generic versions. Write code that uses the NavigableSet and NavigableMap interfaces.
  4. Develop code that makes proper use of type parameters in class/interface declarations, instance variables, method arguments, and return types; and write generic methods or methods that make use of wildcard types and understand the similarities and differences between these two approaches.
  5. Use capabilities in the java.util package to write code to manipulate a list by sorting, performing a binary search, or converting the list to an array. Use capabilities in the java.util package to write code to manipulate an array by sorting, performing a binary search, or converting the array to a list. Use the java.util.Comparator and java.lang.Comparable interfaces to affect the sorting of lists and arrays. Furthermore, recognize the effect of the "natural ordering" of primitive wrapper classes and java.lang.String on sorting.

Objective 7: Fundamentals

  1. Given a code example and a scenario, write code that uses the appropriate access modifiers, package declarations, and import statements to interact with (through access or inheritance) the code in the example.
  2. Given an example of a class and a command-line, determine the expected runtime behavior.
  3. Determine the effect upon object references and primitive values when they are passed into methods that perform assignments or other modifying operations on the parameters.
  4. Given a code example, recognize the point at which an object becomes eligible for garbage collection, determine what is and is not guaranteed by the garbage collection system, and recognize the behaviors of the Object.finalize() method.
  5. Given the fully-qualified name of a class that is deployed inside and/or outside a JAR file, construct the appropriate directory structure for that class. Given a code example and a classpath, determine whether the classpath will allow the code to compile successfully.
  6. Write code that correctly applies the appropriate operators including assignment operators (limited to: =, +=, -=), arithmetic operators (limited to: +, -, *, /, %, ++, --), relational operators (limited to: <, <=, >, >=, ==, !=), the instanceof operator, logical operators (limited to: &, |, ^, !, &&, ||), and the conditional operator ( ? : ), to produce a desired result. Write code that determines the equality of two objects or two primitives.
References


  1. SCJP Sun Certified Programmer for Java 6 Exam 310-065 by Katherine Sierra, Bert Bates
  2. http://www.javachamp.com/
  3. http://scjptest.com/
  4. http://education.oracle.com

regards,
Tech Dexters Support Team,
Hyderabad.

for Feedback and Support simply drop a mail in
techdexters{at}gmail.com
Remember to replace the {at} with @ in the email addresses