Deck 10: Object-Oriented Programming: Polymorphism and Interfaces
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/34
Play
Full screen (f)
Deck 10: Object-Oriented Programming: Polymorphism and Interfaces
1
A(n)_______class cannot be instantiated.
A) final.
B) concrete.
C) abstract.
D) polymorphic.
A) final.
B) concrete.
C) abstract.
D) polymorphic.
abstract.
2
Polymorphism enables you to:
A) program in the general.
B) program in the specific.
C) absorb attributes and behavior from previous classes.
D) hide information from the user.
A) program in the general.
B) program in the specific.
C) absorb attributes and behavior from previous classes.
D) hide information from the user.
A
3
Polymorphism allows for specifics to be dealt with during:
A) execution.
B) compilation.
C) programming.
D) debugging.
A) execution.
B) compilation.
C) programming.
D) debugging.
A
4
Every object in Java knows its own class and can access this information through method .
A) getClass.
B) getInformation.
C) objectClass.
D) objectInformation.
A) getClass.
B) getInformation.
C) objectClass.
D) objectInformation.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
5
Declaring a method final means:
A) it will prepare the object for garbage collection.
B) it cannot be accessed from outside its class.
C) it cannot be overloaded.
D) it cannot be overridden.
A) it will prepare the object for garbage collection.
B) it cannot be accessed from outside its class.
C) it cannot be overloaded.
D) it cannot be overridden.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
6
In Java SE 7 and earlier, an interface may contain:
A) private static data and public abstract methods.
B) only public abstract methods.
C) public static final data and public abstract methods.
D) private static data and public final methods.
A) private static data and public abstract methods.
B) only public abstract methods.
C) public static final data and public abstract methods.
D) private static data and public final methods.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
7
If the superclass contains only abstract method declarations, the superclass is used for ________.
A) implementation inheritance.
B) interface inheritance.
C) Both.
D) Neither.
A) implementation inheritance.
B) interface inheritance.
C) Both.
D) Neither.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
8
It is a UML convention to denote the name of an abstract class in ________.
A) bold.
B) italics.
C) a diamond.
D) there is no convention of the UML to denote abstract classes-they are listed just as any other class.
A) bold.
B) italics.
C) a diamond.
D) there is no convention of the UML to denote abstract classes-they are listed just as any other class.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
9
Which of the following statements about abstract superclasses is true?
A) abstract superclasses may contain data.
B) abstract superclasses may not contain implementations of methods.
C) abstract superclasses must declare all methods as abstract.
D) abstract superclasses must declare all data members not given values as abstract.
A) abstract superclasses may contain data.
B) abstract superclasses may not contain implementations of methods.
C) abstract superclasses must declare all methods as abstract.
D) abstract superclasses must declare all data members not given values as abstract.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
10
Non-abstract classes are called ________.
A) real classes.
B) instance classes.
C) implementable classes.
D) concrete classes.
A) real classes.
B) instance classes.
C) implementable classes.
D) concrete classes.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
11
When a superclass variable refers to a subclass object and a method is called on that object, the proper implementation is determined at execution time. What is the process of determining the correct method to call?
A) early binding.
B) non-binding.
C) on-time binding.
D) late binding.
A) early binding.
B) non-binding.
C) on-time binding.
D) late binding.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
12
For which of the following would polymorphism not provide a clean solution?
A) A billing program where there is a variety of client types that are billed with different fee structures.
B) A maintenance log program where data for a variety of types of machines is collected and maintenance schedules are produced for each machine based on the data collected.
C) A program to compute a 5% savings account interest for a variety of clients.
D) An IRS program that maintains information on a variety of taxpayers and determines who to audit based on criteria for classes of taxpayers.
A) A billing program where there is a variety of client types that are billed with different fee structures.
B) A maintenance log program where data for a variety of types of machines is collected and maintenance schedules are produced for each machine based on the data collected.
C) A program to compute a 5% savings account interest for a variety of clients.
D) An IRS program that maintains information on a variety of taxpayers and determines who to audit based on criteria for classes of taxpayers.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
13
Which of the following is false?
A) You should not call overridable methods from constructors-when creating a subclass object, this could lead to an overridden method being called before the subclass object is fully initialized.
B) It's OK to any of a class's methods from its constructors.
C) When you construct a subclass object, its constructor first calls one of the direct superclass's constructors. If the superclass constructor calls an overridable method, the subclass's version of that method will be called by the superclass constructor.
D) It's acceptable to call a static method from a constructor.
A) You should not call overridable methods from constructors-when creating a subclass object, this could lead to an overridden method being called before the subclass object is fully initialized.
B) It's OK to any of a class's methods from its constructors.
C) When you construct a subclass object, its constructor first calls one of the direct superclass's constructors. If the superclass constructor calls an overridable method, the subclass's version of that method will be called by the superclass constructor.
D) It's acceptable to call a static method from a constructor.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
14
Which of the following could be used to declare abstract method method1 in abstract class Class1 (method1 returns an int and takes no arguments)?
A) public int method1();
B) public int abstract method1();
C) public abstract int method1();
D) public int nonfinal method1();
A) public int method1();
B) public int abstract method1();
C) public abstract int method1();
D) public int nonfinal method1();
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
15
Consider classes A, B and C, where A is an abstract superclass, B is a concrete class that inherits from A and C is a concrete class that inherits from B. Class A declares abstract method originalMethod, implemented in class B. Which of the following statements is true of class C?
A) Method originalMethod cannot be overridden in class C-once it has been implemented in concrete class B, it is implicitly final.
B) Method originalMethod must be overridden in class C, or a compilation error will occur.
C) If method originalMethod is not overridden in class C but is called by an object of class C, an error occurs.
D) None of the above.
A) Method originalMethod cannot be overridden in class C-once it has been implemented in concrete class B, it is implicitly final.
B) Method originalMethod must be overridden in class C, or a compilation error will occur.
C) If method originalMethod is not overridden in class C but is called by an object of class C, an error occurs.
D) None of the above.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
16
Which statement best describes the relationship between superclass and subclass types?
A) A subclass reference cannot be assigned to a superclass variable and a superclass reference cannot be assigned to a subclass variable.
B) A subclass reference can be assigned to a superclass variable and a superclass reference can be assigned to a subclass variable.
C) A superclass reference can be assigned to a subclass variable, but a subclass reference cannot be assigned to a superclass variable.
D) A subclass reference can be assigned to a superclass variable, but a superclass reference cannot be assigned to a subclass variable.
A) A subclass reference cannot be assigned to a superclass variable and a superclass reference cannot be assigned to a subclass variable.
B) A subclass reference can be assigned to a superclass variable and a superclass reference can be assigned to a subclass variable.
C) A superclass reference can be assigned to a subclass variable, but a subclass reference cannot be assigned to a superclass variable.
D) A subclass reference can be assigned to a superclass variable, but a superclass reference cannot be assigned to a subclass variable.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
17
Assigning a subclass reference to a superclass variable is safe ________.
A) because the subclass object has an object of its superclass.
B) because the subclass object is an object of its superclass.
C) only when the superclass is abstract.
D) only when the superclass is concrete.
A) because the subclass object has an object of its superclass.
B) because the subclass object is an object of its superclass.
C) only when the superclass is abstract.
D) only when the superclass is concrete.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
18
Classes and methods are declared final for all but the following reasons:
A) final methods allow inlining the code.
B) final methods and classes prevent further inheritance.
C) final methods are static.
D) final methods can improve performance.
A) final methods allow inlining the code.
B) final methods and classes prevent further inheritance.
C) final methods are static.
D) final methods can improve performance.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
19
All of the following methods are implicitly final except:
A) a method in an abstract class.
B) a private method.
C) a method declared in a final class.
D) static method.
A) a method in an abstract class.
B) a private method.
C) a method declared in a final class.
D) static method.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
20
Consider the abstract superclass below:
public abstract class Foo {
Private int a;
Public int b;
Public Foo(int aVal, int bVal) {
A = aVal;
B = bVal;
}
Public abstract int calculate();
}
Any concrete subclass that extends class Foo:
A) Must implement a method called calculate.
B) Will not be able to access the instance variable
C) Neither (a) nor (b).
D) Both (a) and (b).
public abstract class Foo {
Private int a;
Public int b;
Public Foo(int aVal, int bVal) {
A = aVal;
B = bVal;
}
Public abstract int calculate();
}
Any concrete subclass that extends class Foo:
A) Must implement a method called calculate.
B) Will not be able to access the instance variable
C) Neither (a) nor (b).
D) Both (a) and (b).
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
21
Which of the following statements is false?
A) An advantage of inheritance over interfaces is that only inheritance provides the is-a relationship.
B) Objects of any subclass of a class that implements an interface can also be thought of as objects of that interface type.
C) When a method parameter is declared with a subclass or interface type, the method processes the object passed as an argument polymorphically.
D) All objects have the methods of class Object.
A) An advantage of inheritance over interfaces is that only inheritance provides the is-a relationship.
B) Objects of any subclass of a class that implements an interface can also be thought of as objects of that interface type.
C) When a method parameter is declared with a subclass or interface type, the method processes the object passed as an argument polymorphically.
D) All objects have the methods of class Object.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
22
Which of the following statements is false?
A) Java allows a class to implement multiple interfaces in addition to extending one class.
B) Classes declared with implementation inheritance are tightly coupled.
C) Classes declared with interface inheritance are tightly coupled.
D) An interface also may extend one or more other interfaces.
A) Java allows a class to implement multiple interfaces in addition to extending one class.
B) Classes declared with implementation inheritance are tightly coupled.
C) Classes declared with interface inheritance are tightly coupled.
D) An interface also may extend one or more other interfaces.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
23
Which of the following statements is false?
A) A class's private helper methods may be called only by the class's other methods
B) You cannot declare helper methods in interfaces.
C) An interface's private instance methods can be called directly (i.e., without an object reference) only by the interface's other instance methods.
D) An interface's private static methods can be called by any of the interface's instance or static methods.
A) A class's private helper methods may be called only by the class's other methods
B) You cannot declare helper methods in interfaces.
C) An interface's private instance methods can be called directly (i.e., without an object reference) only by the interface's other instance methods.
D) An interface's private static methods can be called by any of the interface's instance or static methods.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
24
Which of the following statements is false?
A) You cannot prevent client code from creating objects of a class.
B) One common use of a private constructor is sharing initialization code among a class's other constructors.
C) Another common use of private constructors is to force client code to use so-called "factory methods" to create objects.
D) A factory method is a public static method that creates and initializes an object of a specified type (possibly of the same class), then returns a reference to it.
A) You cannot prevent client code from creating objects of a class.
B) One common use of a private constructor is sharing initialization code among a class's other constructors.
C) Another common use of private constructors is to force client code to use so-called "factory methods" to create objects.
D) A factory method is a public static method that creates and initializes an object of a specified type (possibly of the same class), then returns a reference to it.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
25
Which of the following statements is false?
A) In Java SE 8, an interface may declare default methods-that is, public methods with concrete implementations that specify how an operation should be performed.
B) When a class implements an interface, the class receives the interface's default concrete implementations if it does not override them.
C) When you enhance an existing interface with default methods-any class that implemented the original interface will break.
D) With default methods, you can declare common method implementations in interfaces (rather than abstract classes), which gives you more flexibility in designing your classes.
A) In Java SE 8, an interface may declare default methods-that is, public methods with concrete implementations that specify how an operation should be performed.
B) When a class implements an interface, the class receives the interface's default concrete implementations if it does not override them.
C) When you enhance an existing interface with default methods-any class that implemented the original interface will break.
D) With default methods, you can declare common method implementations in interfaces (rather than abstract classes), which gives you more flexibility in designing your classes.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
26
Aclass that implements an interface but does not declare all of the interface's methods must be declared ________.
A) public.
B) interface.
C) abstract.
D) final.
A) public.
B) interface.
C) abstract.
D) final.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
27
Which of the following is not possible?
A) A class that implements two interfaces.
B) A class that inherits from two classes.
C) A class that inherits from one class, and implements an interface.
D) All of the above are possible.
A) A class that implements two interfaces.
B) A class that inherits from two classes.
C) A class that inherits from one class, and implements an interface.
D) All of the above are possible.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
28
Which of the following does not complete the sentence correctly?
An interface _______________.
A) forces classes that implement it to declare all the abstract interface methods.
B) can be used in place of an abstract class when there is no default implementation to inherit.
C) is declared in a file by itself and is saved in a file with the same name as the interface followed by the .java extension.
D) can be instantiated.
An interface _______________.
A) forces classes that implement it to declare all the abstract interface methods.
B) can be used in place of an abstract class when there is no default implementation to inherit.
C) is declared in a file by itself and is saved in a file with the same name as the interface followed by the .java extension.
D) can be instantiated.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
29
Which of the following statements is false?
A) References to interface types do not have access to method toString.
B) Method toString can be invoked implicitly on any object.
C) With inheritance, classes and their inherited classes tend to be very similar.
D) Dramatically different classes can often meaningfully implement the same interface.
A) References to interface types do not have access to method toString.
B) Method toString can be invoked implicitly on any object.
C) With inheritance, classes and their inherited classes tend to be very similar.
D) Dramatically different classes can often meaningfully implement the same interface.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
30
The UML distinguishes an interface from other classes by placing the word "interface" in________ above the interface name.
A) italics.
B) carets.
C) guillemets.
D) bold.
A) italics.
B) carets.
C) guillemets.
D) bold.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
31
Which of the following statements is false?
A) As of Java SE 8, any interface containing only one method is known as a functional interface.
B) There are many functional interfaces throughout the Java APIs.
C) Functional interfaces are used extensively with Java SE 8's new lambda capabilities.
D) Anonymous methods provide a shorthand notation for creating lambdas.
A) As of Java SE 8, any interface containing only one method is known as a functional interface.
B) There are many functional interfaces throughout the Java APIs.
C) Functional interfaces are used extensively with Java SE 8's new lambda capabilities.
D) Anonymous methods provide a shorthand notation for creating lambdas.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
32
Interfaces can have _______methods.
A) 0
B) 1
C) 2
D) any number of
A) 0
B) 1
C) 2
D) any number of
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
33
Which keyword is used to specify that a class will define the methods of an interface?
A) uses
B) implements
C) defines
D) extends
A) uses
B) implements
C) defines
D) extends
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
34
Which of the following statements is false?
A) Prior to Java SE 8, it was common to associate with an interface a class containing static helper methods for working with objects that implemented the interface.
B) Class Collections contains many static helper methods for working with objects that implement interfaces Collection, List, Set and more.
C) Collections method sort can sort objects of any class that implements interface List.
D) With non-static interface methods, helper methods can now be declared directly in interfaces rather than in separate classes.
A) Prior to Java SE 8, it was common to associate with an interface a class containing static helper methods for working with objects that implemented the interface.
B) Class Collections contains many static helper methods for working with objects that implement interfaces Collection, List, Set and more.
C) Collections method sort can sort objects of any class that implements interface List.
D) With non-static interface methods, helper methods can now be declared directly in interfaces rather than in separate classes.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck