Which statement best describes an interface?
6 Interfaces and Abstractions Online Quiz Questions
Use this free practice quiz with 20 questions to review 6 Interfaces and Abstractions, test your knowledge, and prepare for your next test or exam.
Why might a closely related family of types use an abstract class instead of only an interface?
- A
It can combine incomplete behavior with shared implementation or state
- B
It must contain only abstract members and no implementation
- C
It can be instantiated directly in every object-oriented language
- D
It is limited to unrelated classes that share one capability
Which design best applies dependency injection to an OrderService that needs to charge payments?
- A
Constructing StripePaymentGateway inside OrderService
- B
Making OrderService inherit from every payment gateway
- C
Passing a PaymentGateway into OrderService from outside
- D
Removing the payment operation from OrderService
Which two situations are especially appropriate for using an interface? Select all correct answers.
- A
The capability may apply to unrelated classes.
- B
Subclasses need substantial shared state and construction rules.
- C
One class needs several independent capabilities.
- D
A base class must control a shared algorithm and protected implementation.
Which two outcomes can result when application logic depends on a focused abstraction rather than a concrete infrastructure class? Select all correct answers.
- A
Every implementation must use the same algorithm internally.
- B
Implementations can be exchanged more easily.
- C
A client automatically gains access to vendor-specific details.
- D
Tests can supply fakes or mocks.
In structural typing, a type can satisfy an interface without explicitly declaring that it implements the interface.
- A
True
- B
False
If two classes have matching method signatures, they necessarily fulfill the same abstraction contract.
- A
True
- B
False
What is the object-oriented concept that lets a function use Book, Subscription, and ServicePlan objects through the common PricedItem abstraction while each calculates its price differently?
What typing approach allows a type to satisfy an interface because it has the required members, even without explicitly declaring the relationship?
Complete the statement: An describes essential behavior while hiding implementation details, and an is a concrete type that fulfills that contract.
Complete the statement: Supplying a PaymentGateway to OrderService from outside, rather than constructing the gateway inside the service, is an example of .
You are designing an application service that currently creates a database client directly and returns database-specific records. Describe a better design using abstractions and dependency injection. Explain how your design affects testing and infrastructure changes, and identify one behavioral detail that the abstraction's contract should document.
What is a likely design problem with an interface that contains many unrelated operations?
- A
It prevents any class from implementing more than one interface.
- B
It guarantees that all implementations share the same internal algorithm.
- C
It can force implementations to provide unrelated operations they do not need.
- D
It makes structural typing impossible.
Which statement best describes an abstraction in object-oriented programming?
- A
It describes essential behavior without exposing every implementation detail.
- B
It requires every class to inherit from the same concrete base class.
- C
It stores all application data in a shared database.
- D
It guarantees that a program has multiple implementations of every type.
True or false: An abstract class normally cannot be instantiated directly.
- A
True
- B
False
A system must allow invoices, photographs, and maps to be printable, even though these types are unrelated and should not share implementation or state. Which design best represents this requirement?
- A
Define an abstract class because every type must share the same inheritance hierarchy.
- B
Define a small interface because invoices, photographs, and maps are unrelated types that share the Printable capability.
- C
Make each type inherit from a separate concrete printing class.
- D
Put all printing logic into the user-interface layer.
An OrderService needs to charge payments. The design should support a real gateway in production and a fake gateway in isolated tests without changing OrderService. Which approach best applies dependency injection?
- A
Have OrderService always construct StripePaymentGateway internally.
- B
Make the database create the payment gateway when an order is saved.
- C
Pass a PaymentGateway into OrderService so production and test implementations can be supplied externally.
- D
Copy the payment code into every method that processes an order.
In a structural type system using a Closeable protocol, a File type defines the required close operation but does not explicitly declare that it implements Closeable. What does this imply?
- A
A File object with a close operation can be accepted as Closeable even without explicitly declaring that relationship.
- B
A File object must inherit from a class named Closeable before it can be accepted.
- C
The compiler accepts only objects whose method names are chosen at runtime.
- D
A type satisfies the interface only when it has more members than the interface requires.
What programming term describes the ability of one interface-typed parameter to refer to objects such as a Book, Subscription, or ServicePlan, each of which can implement an operation differently?
What is the programming term for a member declared by an abstract class that specifies behavior a concrete subclass must provide?