Quiz on Interfaces

This is a practice quiz. The results are not recorded anywhere and do not affect your grade. The questions on this quiz might not appear in any quiz or test that does count toward your grade.

Instructions: For each question, choose the single best answer. Make your choice by clicking on its button. You can change your answers at any time. When the quiz is graded, the correct answers will appear in the box after each question.

1. What is an interface?

a.    An interface is a collection of constants and method declarations.
b.    An interface is a class that a child class can extend.
c.    An interface is a collection of GUI components.
d.    An interface is the collection of public methods of a class.

Correct Answer Is:

2. Can an interface ever contain method bodies?

a.    No.
b.    Yes.
c.    Sometimes.
d.    Always.

Correct Answer Is:

3. When a class implements an interface, what must it do?

a.    It must redefine each constant from the interface.
b.    It must declare and provide a method body for each method in the interface.
c.    It must declare a variable for each constant in the interface.
d.    It must include a private method for each method in the interface.

Correct Answer Is:

4. Which of the following is true?

a.    A child class can extend a parent or implement an interface, but not do both.
b.    A child class can extend just one parent and can implement just one interface.
c.    A child class can extend just one parent and can implement zero or more interfaces.
d.    A child class can extend zero or more parents, and can implement zero or more interfaces.

Correct Answer Is:

5. Is the following a correct way to start out a class definition:

public class SomeClass implements MyInterface

a.    No—SomeClass must also extend a base class.
b.    No—SomeClass cannot be public if it implements an interface
c.    Yes—SomeClass is a child of MyInterface
d.    Yes—SomeClass is automatically a child of the class Object.

Correct Answer Is:

6. Look at the following interface

interface Taxable { double taxRate = 0.06; double calculateTax(); }

Is the interface correct?

a.    No—because it contains a variable and interfaces cannot contain variables.
b.    No—because the interface cannot contain a method that returns a value.
c.    Yes—taxRate will automatically be a constant since it is in an interface.
d.    Yes—the method body will automatically be filled in.

Correct Answer Is:

7. Can an interface name be used as the type of a variable, like this:

public static void main( String[] args ) { SomeInterface x; ... }

a.    No—a variable must always be an object reference type.
b.    No—a variable must always be an object reference type or a primitive type.
c.    No—a variable must always be a primitive type.
d.    Yes—the variable can refer to any object whose class implements the interface.

Correct Answer Is:

8. Is it OK if a class definition implements two interfaces, each of which has the same definition for the constant PI?

a.    No—if a class implements several interfaces, each constant must be defined in only one interface.
b.    No—a class may not implement more than one interface.
c.    Yes—since the definitions are the same it will not matter.
d.    Yes—the more accurate definition of the two will override the other.

Correct Answer Is:

9. Can an interface be given the private access modifier?

a.    No—then the interface could never be used.
b.    No—since only private classes could use the interface.
c.    Yes—this would make all of its methods and constants private.
d.    Yes—this would mean that only classes in the same file could use the interface.

Correct Answer Is:

10. Can an interface extend another interface?

a.    No—only classes can be extended.
b.    No—interfaces can not be part of a hierarchy.
c.    Yes—since all interfaces automatically extend Object.
d.    Yes.

Correct Answer Is:

The number you got right:       Percent Correct:       Letter Grade:   

Click here (If you have just returned here from another page, or have re-loaded this page, you will need to click again on each of your choices for the grading program to work correctly. You may want to press the "shift key" while clicking on reload to clear the old answers.)