Sunday, August 15, 2010

Constructor Initialization: HorseRadish.java

Courtesy of Sun Web Learning Center

1. class HorseRadish {
2.   // insert code here
3.   protected HorseRadish(int x) {
4.     System.out.println("bok choy");
5.   }
6. }
7. class Wasabi extends HorseRadish {
8.   public static void main(String [] args) {
9.     Wasabi w = new Wasabi();
10.   }
11. }

Which two, inserted independently at line 2, will allow the code to compile and produce the output "bok choy"? (Choose two.)

A   // just a comment
B  protected HorseRadish() { }
C  protected HorseRadish() { this(42);}
D  protected HorseRadish() { new HorseRadish(42);}

Options C and D are correct. B is incorrect because no output would be produced.

No comments:

Post a Comment