Friday, January 28, 2011

Self Test: Chapters 6 to 14

Question 1:
Given:
public abstract interface Cultivate { public void water(String s); }

Which is a correct class? (Choose all that apply.)
A. public abstract class Grow implements Cultivate {
public abstract void water(String s) { }
}
B. public abstract class Grow implements Cultivate { }
C. public class Grow extends Cultivate {
public void water(Integer i) { }
}
D. public class Grow implements Cultivate {
public void water(Integer i) { }
}
E. public class Grow implements Cultivate {
public void water(String i) { }
public void water(Integer s) { }
}

Question 2:
Given:
class Parent {
public Parent(String s) { System.out.print("B"); }
}
public class Child extends Parent {
public Child(String s) { System.out.print("D"); }
public static void main(String [] args) {
new Child("C");
System.out.println(" ");
}
}

What is the result?
A. BD
B. DB
C. BDC
D. DBC
E. Compilation fails

Question 3:
Given:
class Parent {
private final void flipper() { System.out.println("Parent"); }
}

public class Child extends Parent {
public final void flipper() { System.out.println("Child"); }
public static void main(String [] args) {
new Child().flipper();
}
}

What is the result?
A. Child
B. Parent
C. Parent
D. Child
E. Child
F. Parent
G. Compilation fails

Question :

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 runle, well-focused purpose
D. Cohesion is the OO principle most closely associated with allowing a runle object to be seen as having many types

Question 5:

Given the following,
1. class X { void doX() { } }
2. class Y extends X { void doY() { } }
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.doY();
B. (Y)x2.doY();
C. ((Y)x2).doY();
D. None of the above statements will compile

Question 6:
Given:
1. Class1 has a Class4
2. Methods in Class1 use public methods in Class2
3. Methods in Class3 use public methods in Class1
4. Methods in Class1 use public variables in Class2
Which is most likely true? (Choose the most likely.)
A. Class4 has low cohesion
B. Class1 has weak encapsulation
C. Class2 has weak encapsulation
D. Class2 has strong encapsulation
E. Class3 is tightly coupled to Class1

Question 7:

Given:
3. class WildCat {
4. public void roar() { System.out.print("Grrrrrrrrr "); }
5. }
6. class Lion extends WildCat {
7. public void sniff() { System.out.print("sniff "); }
8. public void roar() { System.out.print("roarrrrrr "); }
9. }
10. public class WildCatShow {
11. public static void main(String[] args) { new WildCatShow().go(); }
12. void go() {
13. new Lion().roar();
14. ((WildCat) new Lion()).roar();
15. ((WildCat) new Lion()).sniff();
16. }
17. }

What is the result? (Choose all that apply.)
A. roarrrrrr roarrrrrr sniff
B. roarrrrrr Grrrrrrrrr sniff
C. roarrrrrr roarrrrrr followed by an exception
D. roarrrrrr Grrrrrrrrr followed by an exception
E. Compilation fails with an error at line 14
F. Compilation fails with an error at line 15

Question 8:

Given:
3. public class Ferrari extends Car {
4. public static void main(String[] args) {
5. new Ferrari().go();
6. }
7. void go() {
8. go2(new Car(), new Ferrari());
9. go2((Ferrari) new Car(), new Ferrari());
10. }
11. void go2(Car t1, Ferrari r1) {
12. Ferrari r2 = (Ferrari)t1;
13. Car t2 = (Car)r1;
14. }
15. }
16. class Car { }

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 8
D. Compilation fails with an error at line 9
E. Compilation fails with an error at line 12
F. Compilation fails with an error at line 13

Question 9:

Given:
3. public class Ferrari extends Car {
4. public static String run() { return "fa"; }
5. public static void main(String[] args) {
6. Ferrari t = new Ferrari();
7. Car s = new Ferrari();
8. System.out.println(t.run() + " " + s.run());
9. }
10. }
11. class Car { public static String run() { 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

Question 10:
Given:
3. class Manager {
4. static String s = " ";
5. protected Manager() { s += "Manager "; }
6. }
7. class SubManager extends Manager {
8. private SubManager() { s += "sub "; }
9. }
10. public class SubSubManager extends Manager {
11. private SubSubManager() { s += "subsub "; }
12. public static void main(String[] args) {
13. new SubSubManager();
14. System.out.println(s);
15. }
16. }

What is the result?
A. subsub
B. sub subsub
C. Manager subsub
D. Manager sub subsub
E. Compilation fails
F. An exception is thrown at runtime

Question 11:
Given:
3. class Apartments {
4. Apartments() { System.out.print("b "); }
5. Apartments(String name) {
6. this(); System.out.print("bn " + name);
7. }
8. }
9. public class Home extends Apartments {
10. Home() { System.out.print("h "); }
11. Home(String name) {
12. this(); System.out.print("hn " + name);
13. }
14. public static void main(String[] args) { new Home("x "); }
15. }

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

Question 12:
Given:
3. class Animal {
4. String name = "Rocky ";
5. String makeNoise() { return "generic noise"; }
6. }
7. class Tiger extends Animal {
8. String name = "stripes ";
9. String makeNoise() { return "roarrrrrrrrrrr"; }
10. }
11. public class Zoo {
12. public static void main(String[] args) { new Zoo().go(); }
13. void go() {
14. Animal m = new Tiger();
15. System.out.println(m.name + m.makeNoise());
16. }
17. }

What is the result?
A. Rocky roarrrrrrrrrrr
B. stripes roarrrrrrrrrrr
C. Rocky generic noise
D. stripes generic noise
E. Compilation fails
F. An exception is thrown at runtime

Answers:

Q1. 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 water() 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.

Q2. E is correct. The implied super() call in Child’s constructor cannot be satisfied because there isn’t a no-arg constructor in Parent. 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.

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

Q4. Answer C is correct.
A refers to encapsulation, B refers to coupling, and D refers to polymorphism

Q5. C is correct. Before you can invoke Y’s doY 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

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

Q7. F is correct. Class WildCat doesn’t have a sniff method.
A, B, C, D, and E are incorrect based on the above information

Q8. A is correct, a Class3astException will be thrown when the code attempts to downcast a Car to a Ferrari.
B, C, D, E, and F are incorrect based on the above information.

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

Q10. C is correct. Watch out, SubSubManager extends Manager! Since the code doesn’t attempt to make a SubManager, the private constructor in SubManager is okay.
A, B, D, E, and F are incorrect based on the above information

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

Q12. A is correct. Polymorphism is only for instance methods.
B, C, D, E, and F are incorrect based on the above information

Previous Chapter: Quick Recap Chapters 6 to 14

Next Chapter: Chapter 15: Stack and Heap

2 comments:

  1. Question 1: Choice D is correct!!!!!!!!!!!!

    ReplyDelete
    Replies
    1. Hoang,
      Choice D is incorrect. You will get compilation error:

      The type Grow must implement the inherited abstract method Cultivate.water(String)

      Only choices B and E will compile.

      Anand

      Delete

© 2013 by www.inheritingjava.blogspot.com. All rights reserved. No part of this blog or its contents may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the Author.

ShareThis

Google+ Followers

Followers