Aug 2, 2012
Operators
Relational Operators (Objective 7.6)
- Relational operators always result in a boolean value (true or false).
- There are six relational operators: >, >=, <, <=, ==, and !=.
- The last two (== and !=) are sometimes referred to as equality operators.
- When comparing characters, Java uses the Unicode value of the character as the numerical value.
- There are two equality operators: == and != .
- Four types of things can be tested: numbers, characters, booleans, and reference variables.
- When comparing reference variables, == returns true only if both references refer to the same object.
- instanceof is for reference variables only, and checks for whether the object is of a particular type.
- The instanceof operator can be used only to test objects (or null) against class types that are in the same class hierarchy.
- For interfaces, an object passes the instanceof test if any of its superclasses implement the interface on the right side of the instanceof operator.
- There are four primary math operators: add, subtract, multiply, and divide.
- The remainder operator (%), returns the remainder of a division.
- Expressions are evaluated from left to right, unless you add parentheses, or unless some operators in the expression have higher precedence than others.
- The *, /, and % operators have higher precedence than + and -.
- If either operand is a String, the + operator concatenates the operands.
- If both operands are numeric, the + operator adds the operands.
- Prefix operators (++ and --) run before the value is used in the expression.
- Postfix operators (++ and --) run after the value is used in the expression.
- In any expression, both operands are fully evaluated before the operator is applied.
- Variables marked final cannot be incremented or decremented.
- Returns one of two values based on whether a boolean expression is true or false.
- Returns the value after the ? if the expression is true.
- Returns the value after the : if the expression is false.
- The exam covers six "logical" operators: &, |, ^, !, &&, and ||.
- Logical operators work with two expressions (except for !) that must resolve to boolean values.
- The && and & operators return true only if both operands are true.
- The || and | operators return true if either or both operands are true.
- The && and || operators are known as short-circuit operators.
- The && operator does not evaluate the right operand if the left operand is false.
- The || does not evaluate the right operand if the left operand is true.
- The & and | operators always evaluate both operands.
- The ^ operator (called the "logical XOR"), returns true if exactly one operand is true.
- The ! operator (called the "inversion" operator), returns the opposite value of the boolean operand it precedes.
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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 { SetB.q; } class Quizel implements Klakker { } public class Floozel extends Jammer { List f; } interface Floozet { }
import java.util.*; class Klakker { SetC.q; } class Quizel extends Klakker { } class Jammer { List f; } class Floozet extends Floozel { } public class Floozel { Set k; }
import java.util.*; class Floozet { } class Quizel implements Klakker { } class Jammer { ListD.q; } interface Klakker { } class Floozel extends Jammer { List f; }
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)
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)
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 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)
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)
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:
- class Voop {
- public static void main(String[] args) {
- doStuff(1);
- doStuff(1,2);
- }
- // insert code here
- }
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)
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:
- 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);
- }
- }
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)
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)
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)
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)
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)
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)
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.
- Declarations and Access Control
- Object Orientation
- Assignments
- Operators
- Flow Control
- Exceptions
- Assertions
- Strings
- Input / Output
- Formatting, and Parsing
- Generics and Collections
- Inner Classes
- Threads
- 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
- 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).
- 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.
- 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.
- 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.
- 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
- Develop code that implements an if or switch statement; and identify legal argument types for these statements.
- 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.
- Develop code that makes use of assertions, and distinguish appropriate from inappropriate uses of assertions.
- Develop code that makes use of exceptions and exception handling clauses (try, catch, finally), and declares methods and overriding methods that throw exceptions.
- 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.
- 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
- 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.
- 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.
- 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.
- 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
- Write code to define, instantiate, and start new threads using both java.lang.Thread and java.lang.Runnable.
- Recognize the states in which a thread can exist, and identify ways in which a thread can transition from one state to another.
- 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
- Develop code that implements tight encapsulation, loose coupling, and high cohesion in classes, and describe the benefits.
- 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.
- Explain the effect of modifiers on inheritance with respect to constructors, instance or static variables, and instance or static methods.
- 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.
- Develop code that implements "is-a" and/or "has-a" relationships.
Objective 6: Collections / Generics
- 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.
- Distinguish between correct and incorrect overrides of corresponding hashCode and equals methods, and explain the difference between == and the equals method.
- 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.
- 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.
- 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
- 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.
- Given an example of a class and a command-line, determine the expected runtime behavior.
- 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.
- 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.
- 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.
- 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
- SCJP Sun Certified Programmer for Java 6 Exam 310-065 by Katherine Sierra, Bert Bates
- http://www.javachamp.com/
- http://scjptest.com/
- 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
Subscribe to:
Posts (Atom)