AP CSA Interfaces and Abstract Classes 1 — Questions and Answers
Question 1: Which keyword is used to declare an interface in Java?
- abstract
- interface (Correct answer)
- implements
- extends
Correct answer: interface
The `interface` keyword declares an interface in Java, defining a contract of method signatures without implementations.
Question 2: Which keyword does a class use to adopt an interface?
- extends
- inherits
- implements (Correct answer)
- interface
Correct answer: implements
A class uses the `implements` keyword to adopt one or more interfaces and provide concrete implementations for their methods.
Question 3: By default, all methods declared inside an interface are:
- private and abstract
- public and abstract (Correct answer)
- protected and final
- public and static
Correct answer: public and abstract
Interface methods are implicitly public and abstract, meaning any implementing class must provide a concrete implementation.
Question 4: What happens if a class implements an interface but does not override all of its abstract methods?
- The class inherits empty implementations
- A runtime exception is thrown
- The class must be declared abstract (Correct answer)
- The compiler ignores unimplemented methods
Correct answer: The class must be declared abstract
If a class does not implement all abstract methods of an interface, it must itself be declared abstract; otherwise, the code will not compile.
Question 5: Can a single class in Java implement more than one interface?
- No, Java only allows one interface per class
- Yes, by listing them separated by commas after implements (Correct answer)
- Yes, but only if they share no method names
- No, multiple interfaces require an abstract class
Correct answer: Yes, by listing them separated by commas after implements
Java allows a class to implement multiple interfaces by listing them after the `implements` keyword, separated by commas.
Question 6: Which of the following is TRUE about variables declared inside an interface?
- They are private by default
- They are public, static, and final by default (Correct answer)
- They can be instance variables
- They can be reassigned by implementing classes
Correct answer: They are public, static, and final by default
Variables declared in an interface are implicitly public, static, and final — they are constants that cannot be changed.
Question 7: Which of the following best describes the purpose of an interface in Java?
- To provide a partial implementation that subclasses extend
- To define a contract specifying what methods a class must implement (Correct answer)
- To prevent a class from being instantiated
- To allow direct instantiation with default behaviors
Correct answer: To define a contract specifying what methods a class must implement
An interface defines a contract — a set of method signatures — that any implementing class must fulfill, promoting abstraction and polymorphism.
Which keyword is used to declare an interface in Java?