Object Oriented Programming Quiz
Questions: 16 · 10 minutes
1. You need to add logging or caching around a service at runtime without creating many subclasses or changing the service implementation. Which pattern is the best fit?
Singleton
Factory Method
Template Method
Decorator
2. A BankAccount must never have a negative balance. Which design most directly protects this class invariant?
Allow direct balance updates and check the value only when displaying it
Store the balance in several public fields for comparison
Restrict uncontrolled mutation and ensure every public operation preserves the rule
Let calling code decide whether each withdrawal is valid
3. Two Customer objects are separate instances but contain the same account number and customer data. Which statement correctly distinguishes identity from value equality?
They must have the same identity because their data matches
They can be value-equal while still having different object identities
They must be value-unequal because they were constructed separately
Identity and value equality are always interchangeable in OOP
4. Which description best captures encapsulation in object-oriented programming?
Defining several classes with the same method names
Keeping only the essential features of a concept in a model
Creating a new class by extending an existing class
Bundling state with related behavior while controlling direct access to that state
5. What is a constructor primarily responsible for in a well-designed class?
Establishing the object's initial state and required dependencies
Choosing which overridden method will run at compile time
Granting every other class direct access to internal fields
Destroying the object when it is no longer referenced
6. An OrderService directly creates and uses a SqlOrderRepository. Which change best follows the dependency inversion principle?
Make OrderService depend on an injected repository abstraction
Move all OrderService methods into SqlOrderRepository
Create the repository in a static global variable
Let SqlOrderRepository inherit from OrderService
7. A program sends the same draw() call to Circle, Rectangle, and Triangle objects, and each object responds differently. Which concept does this illustrate?
Encapsulation
Polymorphism
Object composition
Constructor chaining
8. Which relationship most strongly supports the appropriate use of inheritance?
The new class contains an instance of the existing class
The new class temporarily calls methods on the existing class
The two classes happen to store similar fields
The new class is a specialized type that can stand in for the existing type
9. A Bird base class requires every subtype to implement fly(). A Penguin subtype can only implement it by throwing an unsupported-operation error. What is the best redesign?
Move flying behavior into a separate capability implemented only by birds that fly
Make Penguin override every method in Bird
Add a type check before every call to fly()
Keep fly() in Bird and return false for penguins
10. What is the main purpose of abstraction in object-oriented design?
To ensure every class inherits from another class
To expose every field needed by collaborating objects
To model essential behavior while hiding unnecessary implementation detail
To convert all instance methods into static methods
11. An OrderService calls an external payment provider. How can you most effectively unit-test the service without making real payment requests?
Copy the payment provider's production code into the test
Make the real payment request and immediately reverse the transaction
Inject a payment-gateway abstraction and substitute a controlled test double
Remove the payment call from OrderService during testing
12. A Money object should be immutable. Which implementation choice best supports that goal?
Expose its amount field so callers can update it directly
Provide setters that silently reject some changes
Keep its state read-only after construction and return new objects for changed values
Store one shared mutable amount for all Money instances
13. A MultiFunctionDevice interface requires print(), scan(), and fax(), but a basic printer can only print. Which redesign best follows interface segregation?
Make the printer methods public fields instead
Split the interface into smaller printing, scanning, and faxing capabilities
Move all three methods into a common abstract superclass
Have the basic printer throw errors for scan() and fax()
14. A Car needs an Engine, but different engine implementations may be installed or replaced. Which design best represents this relationship?
Make Engine inherit from Car
Store all engine operations as global functions
Duplicate the engine behavior inside every Car class
Give Car an Engine reference supplied through composition
15. An application must support new export formats without repeatedly modifying tested export classes. Which design best supports the open/closed principle?
Define an exporter abstraction and add a new implementation for each format
Place every format algorithm in one large exporter class
Add a growing conditional statement for every format
Copy the existing exporter and rename its public methods
16. A variable declared as PaymentMethod refers at runtime to a CreditCardPayment object. When an overridden process() method is called, the credit-card implementation runs. What mechanism selected that method?
Compile-time method overloading
Dynamic dispatch
Constructor delegation
Field shadowing