0:00

Given the following class declarations, what is the output from Student s1 = new GradStudent(); followed by s1.getInfo();?

public class Student {
   public String getFood() {
      return "Pizza";
   }
   public String getInfo()  {
      return this.getFood();
   }
}

public class GradStudent extends Student {
   public String getFood() {
      return "Taco";
   }
}
注意getInfo()中调用的getFood()GradStudent中的。
2

Given the following class declarations, and EnhancedItem enItemObj = new EnhancedItem();in a client class, which of the following statements would compile?

public class Item
{
   private int x;

   public void setX(int theX)
   {
      x = theX;
   }
   // ... other methods not shown
}

public class EnhancedItem extends Item
{
   private int y;

   public void setY(int theY)
   {
      y = theY;
   }

  // ... other methods not shown
}

I. enItemObj.y = 32;
II. enItemObj.setY(32);
III. enItemObj.setX(52);
public method可以直接调用,private field无法直接访问。
4

Given the following class declarations and initializations in a client program, which of the following is a correct call to method1?

public class Test1
{
   public void method1(Test2 v1, Test3 v2)
   {
      // rest of method not shown
   }
}

public class Test2 extends Test1
{
}

public class Test3 extends Test2
{
}

The following initializations appear in a different class.
Test1 t1 = new Test1();
Test2 t2 = new Test2();
Test3 t3 = new Test3();
因为Test3Test2的child class,故t3可以被当成Test2来使用。
5

If you have a parent class Animal that has a method speak() which returns: Awk. Cat has a speak method that returns: Meow. Bird does not have a speak method. Dog has a speak method that returns: Woof. Pig does not have a speak method. Cow has a speak method that returns: Moo. What is the output from looping through the array a created below and asking each element to speak()?

Animal[] a = { new Cat(), new Cow(), new Dog(), new Pig(), new Bird() }
因为BirdPig都没有重写speak(),会调用parent class中的method并返回Awk
1

Given the following class declarations and code, what is the result when the code is run?

public class Car
{
   private int fuel;

   public Car() { fuel = 0; }
   public Car(int g) { fuel = g; }

   public void addFuel() { fuel++; }
   public void display() { System.out.print(fuel + " "); }
}

public class RaceCar extends Car
{
   public RaceCar(int g) { super(2*g); }
}

What is the result when the following code is compiled and run?

Car car = new Car(5);
Car fastCar = new RaceCar(5);
car.display()
car.addFuel();
car.display();
fastCar.display();
fastCar.addFuel();
fastCar.display();
RaceCar重写了构造函数,让初始燃料变成了原来的两倍。
1

Given the following class definitions and a declaration of Book b = new Dictionary(); which of the following will cause a compile-time error?

public class Book
{
   public String getISBN()
   {
      // implementation not shown
   }

   // constructors, fields, and other methods not shown
}

public class Dictionary extends Book
{
   public String getDefinition(String word)
   {
      // implementation not shown
   }
}
Book中没有getDefintion() method,因而b不能直接调用它。
2


陈 欣

AADPS创始人

发表评论