Quantcast

P^i

Your Online Tech Magazine

Sun05192013

Last update03:26:05 AM

Back You are here: Home More Programming and Web Programming Java Guide: Self Test In Object Orientation

Java Guide: Self Test In Object Orientation



Java Self Test

Self Test

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

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

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?

A. Flip a Clidlet
B. Flip a Clidder
C. Flip a Clidder
Flip a Clidlet
D. Flip a Clidlet
Flip a Clidder
E. Compilation fails.

3. 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) { }
}

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?

A. BD
B. DB
C. BDC
D. DBC
E. Compilation fails.

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

 


 

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?

A. Clidlet
B. Clidder
C. Clidder
Clidlet
D. Clidlet
Clidder
E. Compilation fails.

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

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

A. Flower getType() { return this; }
B. String getType() { return "this"; }
C. Plant getType() { return this; }
D. Tulip getType() { return new Tulip(); }

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.)
A. Woop is-a Hmpf and has-a Zing.
B. Zing is-a Woop and has-a Hmpf.
C. Hmpf has-a Woop and Woop is-a Zing.
D. Woop has-a Hmpf and Woop is-a Zing.
E. Zing has-a Hmpf and Zing is-a Woop.

 


 

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

A. Programmer debug() { return this; }
B. SCJP debug() { return this; }
C. Object debug() { return this; }
D. int debug() { return 1; }
E. int debug(int x) { return 1; }
F. Object debug(int x) { return this; }

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?

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

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

13. Given:
1. class Dog { }
2. class Beagle extends Dog { }
3.
4. class Kennel {
5. public static void main(String [] arfs) {
6. Beagle b1 = new Beagle();
7. Dog dog1 = new Dog();
8. Dog dog2 = b1;
9. // insert code here
10. } }
Which, inserted at line 9, will compile? (Choose all that apply.)

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

14. Given the following,
1. class X { void do1() { } }
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.)

A. x2.do2();
B. (Y)x2.do2();
C. ((Y)x2).do2();
D. None of the above statements will compile.

Answers








blog comments powered by Disqus