Interface vs Abstract Class in Java — Concept Check
Questions on Interface vs Abstract Class in Java. Understand the differences between interface and abstract class in Java 21, when to use each, and how default methods blur the historic boundary.
Questions in this quiz
8 questions on Interface vs Abstract Class in Java — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.You are designing a class hierarchy for different types of vehicles. Which approach should you use if you want to ensure all vehicles have a method to calculate fuel efficiency, but some vehicles share a common engine implementation?
- AUse only an interface for both fuel efficiency and engine.
- BUse an abstract class for both fuel efficiency and engine.
- CUse an interface for fuel efficiency and an abstract class for the engine.
- DUse separate abstract classes for fuel efficiency and engine.
2.A developer wants to create a class that can both be printed and resized. Which Java feature allows this class to implement both functionalities?
- AUse a single abstract class that combines both functionalities.
- BImplement multiple interfaces for printing and resizing.
- CUse default methods in a single interface for both functionalities.
- DExtend multiple abstract classes for printing and resizing.
3.Which statement is true about default methods in Java interfaces?
- ADefault methods make interfaces similar to abstract classes by allowing multiple inheritance.
- BDefault methods allow interfaces to have constructors.
- CDefault methods allow interfaces to have instance fields.
- DDefault methods enable interfaces to provide a partial implementation.
4.A class implements both the Printable and Resizable interfaces. What must the class do?
- AUse constructors from both interfaces.
- BImplement only the abstract methods from both interfaces.
- CProvide implementations for all methods in both interfaces.
- DOverride the default methods in both interfaces.
5.A developer mistakenly believes that an interface can have instance fields. What is the correct understanding?
- AInterfaces can have package-private instance fields.
- BInterfaces can only have constants, which are public static final.
- CInterfaces can have private instance fields.
- DInterfaces can have protected instance fields.
6.In a scenario where you need to share a common logging format across different logging classes, which Java feature should you use?
- AUse multiple interfaces to define different format methods.
- BUse an interface with default methods for the common format.
- CUse a concrete class to enforce the common format method.
- DUse an abstract class to define the common format method.
7.A class needs to have a constructor and share state among subclasses. Which Java feature is appropriate?
- AUse an abstract class.
- BUse a concrete class.
- CUse multiple interfaces.
- DUse an interface with default methods.
8.What is a key difference between an interface and an abstract class in Java?
- AInterfaces support multiple inheritance, abstract classes do not.
- BAbstract classes can only have public static final fields.
- CAbstract classes support multiple inheritance, interfaces do not.
- DInterfaces can have constructors, abstract classes cannot.