多选题 Refer to this class for the following two questions.
public class Account {
private int money;
public int getMoney() {
return this.money;
}
public void setMoney(int money) {
this.money = money;
}
}
多选题 In the code segment, what is the method getMoney() considered?
  • A. Get method
  • B. Access method
  • C. Getter method
  • D. Instance Variable method
【正确答案】 C
【答案解析】这是一个getter方法。getter方法用于获取一个private的实例变量。getter方法的名字总是get后紧跟以大写字母开头的变量名。如果变量是一个boolean类型,则get被替代为is。A、B和D都不正确,并且它们不是典型的Java术语。
多选题 In the code segment, what is the method setMoney (int money) considered?
  • A. Set method
  • B. Access method
  • C. Setter method
  • D. Instance variable method
【正确答案】 C
【答案解析】这是一个setter方法。setter方法用于设置一个private的实例变量。setter方法的名字总是set后紧跟以大写字母开头的变量名。它们需要一个参数并且使用它来设置变量。A、B和D都不正确,并且不是典型的Java术语。
多选题 Which of the following defines information hiding?
  • A. Information hiding is hiding as much detail about your class as possible so others can't steal it.
  • B. Information hiding is about hiding implementation details and protecting variables from being used the wrong way.
  • C. Information hiding is used to obscure the interworking of your class so external classes must use the public interface.
【正确答案】 B
【答案解析】好的类设计尽可能多地隐藏方法和实例变量。这是通过使用private访问修饰符来完成的。这就是为什么外部对象不要试图以开发者不期望的方式与对象进行交互。隐藏信息使得代码更易于维护和更模块化。A和C不正确。A不正确,因为信息隐藏与代码保护没有关系。C不正确,因为访问修饰符应该用于强制外部类使用合适的公共接口。
多选题 What access modifier is used to make the instance variable or method available only to the class in which it is defined?
  • A. public
  • B. private
  • C. protected
  • D. package-private (default)
【正确答案】 B
【答案解析】private访问修饰符用于只允许类中的方法访问方法或者实例变量。A、C和D不正确。A不正确,因为public将使实例变量对所有类都可见。C不正确,因为protected将使实例变量对所有子类或者同一包中的任何类都可见。D不正确,因为包私有的或者默认的访问级别,将使实例变量对同一包中的任何其他类可见。
多选题 What access modifier is used for methods that were defined in an interface?
  • A. public
  • B. private
  • C. protected
  • D. package-private (default)
【正确答案】 A
【答案解析】当实现接口的方法时,使用public访问修饰符。B、C和D不正确。因为它们限制了方法的可访问性,因此不适合于一个接口。
多选题 What is the proper signature for class X if it inherits class Z?
  • A. public class X inherits Z{...}
  • B. public class X extends Z{...}
  • C. public class X implements Z{...}
【正确答案】 B
【答案解析】extends关键字用于继承一个类。A和C不正确。A不正确,因为inherits不是有效的Java关键字。C不正确,因为implements关键字用于接口,而不是类。
多选题 How many classes can a class extend directly?
  • A. Zero
  • B. One
  • C. Two
  • D. As many as it needs
【正确答案】 B
【答案解析】一个类只能扩展一个其他类。但是,它可以使一个类扩展一个又扩展了另一个类的类,以此类推。A、C和D不正确。
多选题 How many interfaces can a class implement directly?
  • A. Zero
  • B. One
  • C. Two
  • D. As many as it needs
【正确答案】 D
【答案解析】类可以按需实现多个接口。A、B和C不正确。
多选题 Consider the following UML illustration for assistance with this question: What is the proper signature for class A if it implements interfaces B and C?
【正确答案】 B
【答案解析】类使用关键字implements来实现一个接口。要实现多个接口,则接口应该出现在关键字implements后以逗号分隔的列表中。A、C、D和E不正确。A不正确,因为implements关键字不应该被包括多次。C不正确,因为应该使用implements关键字而不是interface,并且它只应该使用一次。D不正确,因为应该使用implements而不是interface。E不正确,因为extends用于类,而不是接口;应该使用implements替代。
多选题 Consider the following UML illustration for assistance with this question: What is the proper signature for interface I to inherit interfaces J and K?
【正确答案】 A
【答案解析】接口也可以继承其他接口。不同于类,接口可以按照需要继承或者扩展多个接口。接口使用关键字extends,紧跟逗号分隔的一个列表,列出该接口希望扩展的所有其他接口。B、C和D不正确。B不正确,因为只有类实现接口。一个接口扩展其他接口。C不正确,因为应该使用extends,并且只能用一次。D不正确,因为没有正确使用interface关键字。
多选题 Which statement is true about the term polymorphism?
  • A. It is a Latin word that roughly means "changeable."
  • B. It is a Greek word that roughly means "many forms."
  • C. It is an old English word that roughly means "insectlike."
  • D. It is a new technical term that means "Java object."
【正确答案】 B
【答案解析】多态这个单词来源于希腊语,意思为“多种形式”。A、C和D不正确。
多选题 What type of object can polymorphically behave as another?
  • A. An object can act as any subclass of the class it was created from.
  • B. An object can act as any superclass of the class it was created from.
  • C. An object can act as any other abstract class.
【正确答案】 B
【答案解析】对象继承了其超类的所有功能,因此可以多态地具有它们的行为。A和C不正确。A不正确,因为对象不能用作其子类,因为子类更具体并且包含父类中不存在的功能。C不正确,因为这些类之间需要有“is-a”关系。这个答案没有提及是什么关系。
多选题 Polymorphism helps to facilitate which of the following? (Choose all that apply.)
  • A. Highly optimized code
  • B. Code reuse
  • C. Code obfuscation
  • D. Code that is genetic and flexible
【正确答案】 B、D
【答案解析】多态有助于创建可重用的代码,因为它允许代码可以编写得更抽象,因此B正确。类似于B,通过使用任何更具体对象都可以满足的通用数据类型,多态使得代码更通用。因此,D也正确。A和C不正确。A不正确,因为多态对代码优化方面没有影响。C不正确,因为混淆代码(目的是让代码难以阅读)与多态无关。
多选题 What is a correct "is-a" relationship?
  • A. A specific object "is-a" more generic one.
  • B. A genetic object "is-a" more specific one.
  • C. A null object "is-an" object.
【正确答案】 A
【答案解析】一个更具体的对象可以被看成是一个更通用的对象。这是多态的基本原则。B和C不正确。B不正确,因为通用对象不具有更具体对象的所有功能,因此与具体的对象不具备“is-a”关系。C不正确,因为一个null对象对它与其他对象间的关系没有影响。
多选题 Which of the following statements explain why an object can polymorphically behave as an interface?
  • A. By implementing the interface, the object is required to have all of the functionality that the interface represents.
  • B. By implementing the interface, the object inherits all the required methods it defines.
  • C. An object can behave as an interface because interfaces do not have a strict expected behavior and therefore any object can act as an interface.
【正确答案】 A
【答案解析】当类实现一个接口时,它需要实现接口包含的所有方法。这给予类在接口中定义的功能。因此允许该类用作接口。B和C不正确。B不正确,因为当接口被实现时,没有继承任何东西。C不正确,因为每一个接口都有严格的行为预期。这由必须实现的方法来表示。
多选题 What does it mean if a developer is programming to an interface?
  • A. The developer is implementing an interface for the class he or she is working on.
  • B. The developer was given a set of interfaces he or she must implement.
  • C. The developer is defining the functionality instead of strict object types as much as possible.
【正确答案】 C
【答案解析】面向接口编程意味着开发者定义功能来代替对象数据类型。然后任何对象都可以实现所需要的接口,并使用它。如果不使用接口,则只能使用定义的特定的数据类型对象。A和B不正确。A不正确,因为面向接口编程与只在类中实现接口相比是一个更大的概念。B不正确,因为在这种情形下,开发者只是实现了预先确定的一组接口。