Final exam
Object-Oriented Programming
Before you start
- 20 questions, drawn at random from a pool of 32.
- Pass mark: 70%. Your final score is 30% practice + 70% exam.
- No time limit, but finish within 3 hours. You can move between questions before you submit.
- Failed? Review the lessons and try again: every attempt gets a new set of questions.
Question 1 of 20Choose one
Which situation is the strongest sign that an object-oriented design would help?
Question 2 of 20True or false
In OOP, a class's methods are written once but run separately for each instance, using this (or self) to reach that instance's fields.
Question 3 of 20Match the pairs
Match each pillar to its best description.
Tap an answer, then tap the place where it belongs (or drag it there).
- Encapsulation
- Abstraction
- Inheritance
- Polymorphism
Question 4 of 20Fill in the blanks
Fill in the blanks
In Python, the constructor method is named and every instance method receives the object as its first parameter, conventionally named .
Question 5 of 20What would you do?
What would you do?
What does this code print?
const x = new Box(1); // Box stores its argument in this.value
const y = x;
y.value = 9;
const z = new Box(9);
console.log(x.value, x === z);Question 6 of 20Choose one
Inside a class, a method passes this.handle to setTimeout. When the timer fires, this.count++ inside handle throws an error. Why?
Question 7 of 20Sort into groups
Sort these members into instance or static.
Tap an item, then tap its group (or drag it there).
Question 8 of 20Work it out
A Rectangle class stores width and height and has a getter get area() { return this.width * this.height; }. After const r = new Rectangle(4, 5); r.width = 7;, what does r.area return?
Question 9 of 20Select all that apply
Which practices help protect an object's invariants? Select all that apply.
Select all that apply.
Question 10 of 20Spot the mistake
A Temperature class must never allow values below absolute zero (−273.15 °C). Click the line that lets an invalid value in.
Question 11 of 20True or false
A class that consists only of private fields with a public getter and setter for each one gives strong encapsulation.
Question 12 of 20Choose one
Which is the clearest example of "tell, don't ask"?
Question 13 of 20Choose one
Which of these is best modeled as an immutable value object?
Question 14 of 20Work it out
An Account class has deposit(a) { this.balance += a; }. A subclass BonusAccount overrides it as deposit(a) { super.deposit(a); if (a >= 100) super.deposit(5); }. Starting from a balance of 0, what is the balance after deposit(150), deposit(40) and deposit(100)?
Question 15 of 20Choose one
In JavaScript, what happens if a subclass constructor uses this before calling super(...)?
Question 16 of 20Sort into groups
Apply the is-a test to each pair.
Tap an item, then tap its group (or drag it there).
Question 17 of 20What would you do?
What would you do?
A Notifications loop calls channel.send(msg) on a list of channel objects: EmailChannel, SmsChannel and PushChannel. A new SlackChannel class is added with its own send(msg) method. What must change in the loop?
Question 18 of 20True or false
Duck typing means an object can be used wherever its needed methods exist, even if it shares no superclass with other objects used the same way.
Question 19 of 20Fill in the blanks
Fill in the blanks
When a parent-class method calls this.calculate() and a subclass has overridden calculate, the subclass version runs. Choosing the method at run time based on the actual object is called .
Question 20 of 20Select all that apply
Which statements about abstract classes and interfaces are true? Select all that apply.
Select all that apply.
Question 21 of 20What would you do?
What would you do?
Bird has a fly() method used throughout an app. A developer adds class Penguin extends Bird and overrides fly() to throw "Penguins can't fly". What's the best response?
Question 22 of 20Work it out
A game has 5 character classes and 4 optional abilities that can be combined freely. How many classes does a design need if it uses composition: one class per character type, one per ability, plus one class that combines them?
Question 23 of 20Choose one
What is delegation?
Question 24 of 20Choose one
An Invoice class calculates totals, saves itself to the database and renders itself as HTML. Which SOLID principle does it violate most directly?
Question 25 of 20Spot the mistake
This code is supposed to follow the dependency inversion principle. Click the line that breaks it.
Question 26 of 20Match the pairs
Match each pattern to its intent.
Tap an answer, then tap the place where it belongs (or drag it there).
- Strategy
- Observer
- Adapter
- Factory
- Decorator
Question 27 of 20True or false
The singleton pattern is generally recommended for any object that a program needs only one of, such as a configuration or database connection.
Question 28 of 20Put in order
Order the layers so that the outermost wrapper is first: a request goes through logging, then caching, then reaches the real API source.
Drag the items, or use the arrow buttons, to put them in order.
- LoggedSource
- ApiSource
- CachedSource
Question 29 of 20Work it out
A library charges $0.25 per day late, and loans last 21 days. A member borrows a book and returns it 35 days later. What is the fine in dollars?
Question 30 of 20Match the pairs
Match each requirement phrase to how it is usually modeled.
Tap an answer, then tap the place where it belongs (or drag it there).
- "Customers place orders"
- "Each order has a total"
- "Cancel an order"
- "An order's line items exist only within that order"
Question 31 of 20Choose one
A PaymentService calls a real payment provider over the network. What makes it easiest to unit test?
Question 32 of 20True or false