AP CSA Interfaces and Abstract Classes 2 — Questions and Answers
Question 1: Which keyword is used to declare an abstract class in Java?
- interface
- abstract (Correct answer)
- virtual
- sealed
Correct answer: abstract
The `abstract` keyword is placed before the `class` keyword to declare a class as abstract, meaning it cannot be instantiated directly.
Question 2: Can an abstract class contain non-abstract (concrete) methods?
- No, all methods in an abstract class must be abstract
- Yes, it can contain both abstract and concrete methods (Correct answer)
- Yes, but only if it also implements an interface
- No, concrete methods belong only in interfaces
Correct answer: Yes, it can contain both abstract and concrete methods
An abstract class can have a mix of abstract methods (no body) and concrete methods (with a body), providing partial implementation.
Question 3: What is the result of trying to instantiate an abstract class directly?
- A NullPointerException at runtime
- A compile-time error (Correct answer)
- An object is created with null fields
- The JVM creates a proxy object
Correct answer: A compile-time error
Attempting to instantiate an abstract class results in a compile-time error because abstract classes are incomplete by design.
Question 4: Which relationship does abstract class inheritance use in Java?
- implements
- extends (Correct answer)
- uses
- includes
Correct answer: extends
A subclass inherits from an abstract class using the `extends` keyword, the same as with concrete class inheritance.
Question 5: Consider: `public abstract class Shape { public abstract double area(); }`. Which of the following correctly creates a subclass?
- public class Circle extends Shape { }
- public class Circle implements Shape { public double area() { return 3.14 * r * r; } }
- public class Circle extends Shape { public double area() { return 3.14 * r * r; } } (Correct answer)
- public abstract class Circle extends Shape { }
Correct answer: public class Circle extends Shape { public double area() { return 3.14 * r * r; } }
Circle must extend Shape (not implement) and must provide a concrete `area()` method to avoid being abstract itself.
Question 6: What distinguishes an abstract class from an interface in Java (in the context of AP CSA)?
- Abstract classes can have constructors; interfaces cannot (Correct answer)
- Abstract classes support multiple inheritance; interfaces do not
- Interfaces can have instance variables; abstract classes cannot
- Abstract classes cannot have abstract methods
Correct answer: Abstract classes can have constructors; interfaces cannot
Abstract classes can have constructors that subclasses can call via `super()`, whereas interfaces cannot have constructors because they cannot be instantiated.
Question 7: A class Truck extends an abstract class Vehicle that has abstract methods `start()` and `stop()`. Truck provides `start()` but NOT `stop()`. What must be true of Truck?
- Truck compiles fine because it provided at least one method
- Truck must also be declared abstract (Correct answer)
- Truck automatically inherits a default `stop()` from Vehicle
- A runtime error occurs when `stop()` is called
Correct answer: Truck must also be declared abstract
Since Truck does not implement all abstract methods inherited from Vehicle, Truck itself must be declared abstract.
Which keyword is used to declare an abstract class in Java?