Self Test


1. 

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

  1. Has-a relationships always rely on inheritance.

  2. Has-a relationships always rely on instance variables.

  3. Has-a relationships always require at least two class types.

  4. Has-a relationships always rely on polymorphism.

  5. Has-a relationships are always tightly coupled.

image from book

2. 

Given:

 class Clidders {   public final void flipper() { System.out.println("Clidder"); } } public class Clidlets extends Clidders {   public void flipper() {     System.out.println("Flip a Clidlet");     super.flipper();   }   public static void main(String [] args) {     new Clidlets().flipper();   } } 

What is the result?

  1. Flip a Clidlet

  2. Flip a Clidder

  3. Flip a Clidder

    Flip a Clidlet

  4. Flip a Clidlet

    Flip a Clidder

  5. Compilation fails.

image from book

3. 

Given:

 public abstract interface Frobnicate { public void twiddle(String s) ; } 

Which is a correct class? (Choose all that apply.)

  1. public abstract class Frob implements Frobnicate {

    public abstract void twiddle(String s){}

    }

  2. public abstract class Frob implements Frobnicate { }

  3. public class Frob extends Frobnicate {

    public void twiddle(Integer i) { }

    }

  4. public class Frob implements Frobnicate {

    public void twiddle(Integer i) { }

    }

  5. public class Frob implements Frobnicate {

    public void twiddle(String i) { }

    public void twiddle(Integer s) { }

    }

image from book

4. 

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?

  1. BD

  2. DB

  3. BDC

  4. DBC

  5. Compilation fails.

image from book

5. 

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

  1. The attributes of the class are all private.

  2. The class refers to a small number of other objects.

  3. The object contains only a small number of variables.

  4. The object is referred to using an anonymous variable, not directly.

  5. The reference variable is declared for an interface type, not a class. The interface provides a small number of methods.

  6. It is unlikely that changes made to one class will require any changes in another.

image from book

6. 

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?

  1. Clidlet

  2. Clidder

  3. Clidder

    Clidlet

  4. Clidlet

    Clidder

  5. Compilation fails.

image from book

7. 

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

 

(

)

{

}

;

   

image from book

8. 

Given:

 1. class Plant { 2.  String getName() { return "plant"; } 3.  Plant getType() { return this; } 4. } 5. class Flower extends Plant { 6.  // insert code here 7. } 8. class Tulip extends Flower {} 

Which statement(s), inserted at line 6, will compile? (Choose all that apply.)

  1. Flower getType() { return this; }

  2. String getType() { return "this"; }

  3. Plant getType() { return this; }

  4. Tulip getType() { return new Tulip() ;}

image from book

9. 

Given:

 1. class Zing { 2.   protected Hmpf h; 3. } 4. class Woop extends Zing { } 5. class Hmpf { } 

Which is true? (Choose all that apply.)

  1. Woop is-a Hmpf and has-a zing.

  2. zing is-a Woop and has-a Hmpf.

  3. Hmpf has-a Woop and Woop is-a Zing.

  4. Woop has-a Hmpf and Woop is-a zing.

  5. Zing has-a Hmpf and Zing is-a Woop.

image from book

10. 

Given:

 1. class Programmer { 2.   Programmer debug() { return this; } 3. } 4. class SCJP extends Programmer { 5.   // insert code here 6. } 

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

  1. Programmer debug() { return this; }

  2. SCJP debug() { return this; }

  3. Object debug() { return this; }

  4. int debug() { return 1; }

  5. int debug(int x) { return 1; }

  6. Object debug (int x) { return this; }

image from book

11. 

Given:

 class Uber {   static int y = 2;   Uber(int x) { this(); y = y * 2; }   Uber() { y++; } } class Minor extends Uber {   Minor() { super(y); y = y + 3; }   public static void main(String [] args) {     new Minor();     System.out.println(y); } } 

What is the result?

  1. 6

  2. 7

  3. 8

  4. 9

  5. Compilation fails.

  6. An exception is thrown.

image from book

12. 

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

  1. Cohesion is the OO principle most closely associated with hiding implementation details.

  2. Cohesion is the OO principle most closely associated with making sure that classes know about other classes only through their APIs.

  3. Cohesion is the OO principle most closely associated with making sure that a class is designed with a single, well-focused purpose.

  4. Cohesion is the OO principle most closely associated with allowing a single object to be seen as having many types.

image from book

13. 

Given:

  1. class Dog { }  2. class Beagle extends Dog { }  3.   4. class Kennel {  5.   public static void main(String [] arfs) {  6.     Beagle bl = new Beagle();  7.     Dog dogl = new Dog();  8.     Dog dog2 = bl;  9.     // insert code here 10. } } 

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

  1. Beagle b2 = (Beagle) dog1;

  2. Beagle b3 = (Beagle) dog2;

  3. Beagle b4 = dog2;

  4. None of the above statements will compile.

image from book

14. 

Given the following,

  1. class X { void dol() { } }  2. class Y extends X { void do2() { } }  3.   4. class Chrome {  5.   public static void main(String [] args) {  6.     X x1 = new X();  7.     X x2 = new Y();  8.     Y y1 = new Y();  9.     // insert code here 10. } } 

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

  1. x2.do2( );

  2. (Y) x2. do2( );

  3. ((Y)x2).do2();

  4. None of the above statements will compile.

image from book

Answers

1. 

þ  

B is correct.

ý  

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

2. 

þ  

E is correct. final methods cannot be overridden.

ý  

A, B, C, and D are incorrect based on the above. (Objective 5.3)

3. 

þ  

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)

4. 

þ  

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)

5. 

þ  

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

ý  

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

6. 

þ  

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)

7. 

 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 no-argument superclass constructor. This is done as: super();. The line cannot be left blank, as the parentheses are already in place. Further, since the superclass constructor called is the no-argument version, this constructor must be created. It will not be created by the compiler because there is another constructor already present.
(Objective 5.4) .

8. 

þ  

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

ý  

B is incorrect, String is not a subtype of Plant.
(Objective 1.5)

9. 

þ  

D is correct, Woop inherits a Hmpf from Zing.

ý  

A, B, C, and E are incorrect based on the preceding.
(Objective 5.5)

10. 

þ  

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

ý  

C and D are incorrect. They are illegal overrides because their return types are incompatible. They are illegal overloads because their arguments did not change.
(Objective 5.4)

11. 

þ  

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

ý  

A, B, C, E, and F are incorrect based on the preceding.
(Objective 1.6)

12. 

þ  

Answer C is correct.

ý  

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

13. 

þ  

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

ý  

C and D are incorrect based on the preceding.
(Objective 5.2).

14. 

þ  

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)




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