Self Test


The following questions will help you measure your understanding of the material presented in this chapter. Read all of the choices carefully, as there may be more than one correct answer. Choose all correct answers for each question. Stay focused.

If you have a rough time with these at first, don't beat yourself up. Be positive. Repeat nice affirmations to yourself like, "I am smart enough to understand enums" and "OK, so that other guy knows enums better than I do, but I bet he can't <insert something you are good at> like me."

1. 

Given the following,

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

Which code fragments will compile? (Choose all that apply.)

  1. interface Base2 implements Base { }

  2. abstract class Class2 extends Base {

    public boolean ml() { return true; } }

  3. abstract class Class2 implements Base { }

  4. abstract class Class2. implements Base {

    public boolean m1() { return (true); } }

  5. class Class2 implements Base {

    boolean m1( ) { return false; }

    byte m2(short s) { return 42; } }

image from book

2. 

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

  1. public abstract class Canine { public Bark speak(); }

  2. public abstract class Canine { public Bark speak() { } }

  3. public class Canine { public abstract Bark speak(); }

  4. public class Canine abstract { public abstract Bark speak(); }

image from book

3. 

Which is true? (Choose all that apply. )

  1. "X extends Y" is correct if and only if X is a class and Y is an interface.

  2. "X extends Y" is correct if and only if X is an interface and Y is a class.

  3. "X extends Y" is correct if X and Y are either both classes or both interfaces.

  4. "X extends Y" is correct for all combinations of X and Y being classes and/or interfaces.

image from book

4. 

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

  1. int $x;

  2. int 123;

  3. int _123;

  4. int #dim;

  5. int %percent;

  6. int *divide;

  7. int central_sales_region_Summer_2005_gross_sales;

image from book

5. 

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

  1. addSize

  2. getCust

  3. deleteRep

  4. isColorado

  5. putDimensions

image from book

6. 

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

  1. static void doStuff(int... doArgs) { }

  2. static void doStuff (int [] doArgs) { }

  3. static void doStuff(int doArgs...) { }

  4. static void doStuff(int... doArgs, int y) { }

  5. static void doStuff(int x, int... doArgs) { }

image from book

7. 

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

  1. short x [];

  2. short [] y;

  3. short [5] x2;

  4. short z2 [5];

  5. short [] z [] [];

  6. short [] y2 = [5];

image from book

8. 

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?

  1. woof burble

  2. Multiple compilation errors

  3. Compilation fails due to an error on line 2

  4. Compilation fails due to an error on line 3

  5. Compilation fails due to an error on line 4

  6. Compilation fails due to an error on line 9

image from book

9. 

Given:

 1. enum A { A } 2. class E2 { 3.   enum B { B } 4.   void C() { 5.     enum D { D } 6.   } 7. } 

Which statements are true? (Choose all that apply.)

  1. The code compiles.

  2. If only line 1 is removed the code compiles.

  3. If only line 3 is removed the code compiles.

  4. If only line 5 is removed the code compiles.

  5. If lines 1 and 3 are removed the code compiles

  6. If lines 1, 3 and 5 are removed the code compiles.

image from book

Answers

1. 

þ  

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

ý  

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

2. 

þ  

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

ý  

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

3. 

þ  

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)

4. 

þ  

A, C, and G are legal identifiers.

ý  

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

5. 

þ  

B and D use the valid prefixes 'get' and 'is'.

ý  

A, C, and E are incorrect because 'add', 'delete' and 'put' are not standard JavaBeans name prefixes. (Objective 1.4)

6. 

þ  

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)

7. 

þ  

A, B, and E are correct array declarations; E is a three dimensional array.

ý  

C, D, and F are incorrect, you can't include the size of your array in a declaration unless you also instantiate the array object. F uses invalid instantiation syntax. (Objective 1.3)

8. 

þ  

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)

9. 

þ  

D and F are correct. Line 5 is the only line that will not compile, because enums cannot be local to a method.

ý  

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




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

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