多选题 The Drivable interface:
public interface Drivable {
/*
* Drivable definitions
*/
}
The Tractor class:
public class Tractor implements Drivable {
/*
* Tractor functionality
*/
}
The Vehicle class:
public class Vehicle {
/*
* Vehicle functionality
*/
}
The Car class:
public class Car extends Vehicle implements Drivable {
/*
* Car functionality
*/
}
The Truck class:
public class Truck extends Vehicle implements Drivable {
/*
* Truck functionality
*/
}
多选题 Given the preceding classes and interface, would the following code segment produce errors when compiled? Car car = new Car(); Vehicle vehicle = car; A. No errors would be produced. B. This code would result in compile errors.
【正确答案】 A
【答案解析】不会产生错误,因为Car类扩展了Vehicle类,因此可以用作一个Vehicle对象。B不正确。
多选题 Given the preceding classes and interface, would the following code segment produce errors when compiled? Truck truck = new Truck(); Drivable drivable = truck; A. No errors would be produced. B. This code would result in compile errors.
【正确答案】 A
【答案解析】不会产生错误,因为Truck类实现了Drivable接口,因此可以用作一个Drivable对象。B不正确。
多选题 Given the preceding classes and interface, would the following code segment produce errors when compiled? Tractor tractor = new Tractor(); Vehicle vehicle = tractor; A. No errors would be produced. B. This code would result in compile errors.
【正确答案】 B
【答案解析】这段代码将会导致编译错误,因为Vehicle类不是Tractor类的超类。A不正确。
多选题 Given the preceding classes and interface, would the following code segment produce errors when compiled? Drivable drivable = new Drivable(); Truck truck = drivable; A. No errors would be produced. B. This code would result in compile errors.
【正确答案】 B
【答案解析】这段代码会导致编译错误,因为Drivable是一个接口,它不能被实例化。A不正确。
多选题 Given the preceding classes and interface, would the following code segment produce errors when compiled? Vehicle vehicle = new Vehicle(); Object o = vehicle; A. No errors would be produced. B. This code would result in compile errors.
【正确答案】 A
【答案解析】不会产生错误,因为Vehicle类不是具体类,并且Object类是每个Java对象的超类。B不正确。
多选题 Given the preceding classes and interface, would the following code segment produce errors when compiled? Truck truck = new Truck(); Object o = truck; A. No errors would be produced. B. This code would result in compile errors.
【正确答案】 A
【答案解析】不会产生错误,因为Object类是所有Java对象的超类。B不正确。
多选题 In what cases is casting needed? (Choose all that apply.)
  • A. Going from a superclass to subclass
  • B. Going from a subclass to superclass
  • C. Using an int as a double
  • D. Using a float as a long
【正确答案】 A、D
【答案解析】A正确,因为类型转换总是朝继承链向下转换。D正确,因为long不是浮点数;精度将会丢失,因此需要类型转换。B和C不正确。B不正确,因为多态发生在从子类向父类转换时。C不正确,因为当使用int作为double时,不会丢失精度,所以不需要类型转换。
多选题 Why must the a variable be casted?
  • A. So future developers understand the intended use of the variable
  • B. To tell the compiler that the data conversion is safe to make
  • C. Because it was used polymorphically before
【正确答案】 B
【答案解析】有可能会不兼容数据,必须使用类型转换。A和C不正确。
多选题 What would cause an exception to be thrown from a cast?
  • A. If precision is lost due to the cast
  • B. If the object is cast to a superclass of its current type
  • C. If the object was never instantiated as the object that it is being cast to or one of its subclasses
  • D. If the object is being cast is null
【正确答案】 C
【答案解析】如果对象没有实例化为它将被类型转换为的对象级别的细节,将会造成运行时异常。A、B和D不正确。A不正确,因为在基本数据类型间会丢失精度,没有异常。B是多态的一个示例。可以做类型转换,但不是必要的。D不正确,因为null对象可以类型转换。这不会造成异常。但是,如果类型转换为一个无效的类型,编译器将产生一个错误。