Abstract Classes — Concept Check
Questions on Abstract Classes. Use abstract classes to share partial implementations across related subclasses while enforcing a contract through abstract methods.
Questions in this quiz
8 questions on Abstract Classes — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A developer is designing a class hierarchy for shapes. They want to ensure that all shapes have a method to calculate area and perimeter, but some shapes may have additional methods. What should they use as the base class?
- AAn abstract class with abstract methods for area and perimeter
- BA concrete class with empty methods for area and perimeter
- CAn interface with abstract methods for area and perimeter
- DAn interface with default methods for area and perimeter
2.Given an abstract class `Vehicle` with an abstract method `fuelType`, what must a subclass do?
- AImplement the `fuelType` method or declare itself abstract
- BOnly declare itself abstract
- COnly implement the `fuelType` method
- DOverride all methods of `Vehicle`
3.A developer uses an abstract class to define a template method pattern. Why might they declare the template method as final?
- ATo make the template method optional for subclasses
- BTo prevent subclasses from overriding the algorithm structure
- CTo ensure subclasses can override all steps of the algorithm
- DTo allow subclasses to override the algorithm structure
4.In a class hierarchy, why might a developer choose an abstract class over an interface?
- ATo allow multiple inheritance
- BTo enforce a pure contract with no implementation
- CTo share code and state among related classes
- DTo ensure no fields are shared
5.A developer mistakenly tries to instantiate an abstract class. What will happen?
- AThe program will run but the object will have no fields
- BThe program will run but the object will have no methods
- CA compile-time error will occur
- DA runtime error will occur
6.A class `ElectricCar` extends an abstract class `Vehicle` and implements `Trackable`. What is true about `ElectricCar`?
- AIt must implement all abstract methods from `Vehicle` and `Trackable`
- BIt must be declared abstract
- CIt can ignore the abstract methods of `Vehicle`
- DIt can only implement methods from `Trackable`
7.Why might a developer use a constructor in an abstract class?
- ATo enforce mandatory initialization for subclasses
- BTo make the abstract class concrete
- CTo prevent subclasses from having constructors
- DTo allow instantiation of the abstract class
8.A developer creates an abstract class with a concrete method. What does this allow?
- AThe method cannot be used by subclasses
- BSubclasses must override the method
- CThe method must be abstract in subclasses
- DSubclasses inherit the method without overriding