Beginning Java with NetBeans: Chapter 12 How to work with interfaces MULTIPLE CH
ID: 3730612 • Letter: B
Question
Beginning Java with NetBeans: Chapter 12
How to work with interfaces
MULTIPLE CHOICE [Answers are in tables – delete all but the correct answer’s cell]
1. Which of the following statements is not true?
a. An interface can contain abstract methods.
b. An interface can contain instance variables.
c. An interface can contain static constants.
d. A class can implement multiple interfaces.
e. A class can inherit another class and implement an interface.
Code example 12-1
}
public class Printer {
public static void printInvoice(Printable p) {
System.out.println("Printing invoice");
p.print(); }
}
}
public class Rental extends Transaction implements Printable { public void print() {
}
2. (Refer to code example 12-1.) Which of the following statements will definitely not compile?
a. Printablep = new Order();
b. Rentalrental = new Rental(); Printable p = rental;
c. Printablep = new Printable();
d. Transaction t = new Rental();
3. (Refer to code example 12-1.) What happens when the code that follows is executed?
Rental rental = new Rental();
a. “Printing invoice” is printed to the console.
b. “Printing invoice” and “Rental object” are printed to the console.
c. A runtime error occurs because printInvoice can’t accept Rental objects.
d. Nothing happens.
4. A method that accepts an interface as an argument can accept any object that
a. implements that interface
b. defines the same methods as the interface
c. implements the interface or defines the same methods as the interface
d. is created from that interface
5. A variable that stores an object that implements an interface can be declared as which of the following data types?
a. The class that defines the object
b. The interface that the object implements
c. Any subclass of the class that defines the object
d. All of the above
e. A and C only
6. With Java 8 and later, what keyword can you use to code a regular (non-static) method in an interface?
a. the regular keyword
b. the static keyword
c. the nonstatic keyword
d. the default keyword
7. With Java 8 and later, what keyword can you use to code a static method in an interface?
a. the regular keyword
b. the static keyword
c. the nonstatic keyword
d. the default keyword
8. A non-abstract class that implements an interface
a. can use any constants defined by the interface
b. must provide an implementation for each abstract method defined by the
c. interface
d. must inherit the interface
e. all of the above
f. a and b only
9. Which of the following statements is true?
a. An interface can define and use instance variables.
b. An interface can inherit a class.
c. A class can only implement one interface.
d. A class can implement multiple interfaces.
10. If a class implements one interface, what keyword do you use in the class declaraction to specify that interface?
a. extends
b. implements
c. interface
d. inherits
11. If an interface inherits another interface, what keyword do you use to specify the inherited interface?
a. extends
b. implements
c. interface
d. inherits
COMPLETION
An interface that doesn’t contain any constants or methods is called a ____________ interface.
A class that ______________ an interface must contain methods for all abstract methods in the interface. Otherwise, the class must be declared as abstract.
A class that implements an interface can use any ____________ defined by that interface without declaring it.
When you code a class that implements an interface, you don’t need to implement its ___________ methods.
A class can only directly inherit ________ other class, but a class can implement multiple interfaces.
Explanation / Answer
1. b. An interface can contain instance variables.
2. c. Printablep = new Printable(); We can not instanciate interface
3. c. A runtime error occurs because printInvoice can’t accept Rental objects.
4. c. implements the interface or defines the same methods as the interface
5. d. All of the above
6. d. the default keyword
7. b. the static keyword
8. f. a and b only
9. d. A class can implement multiple interfaces.
10. b. implements
11. a. extends
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.