Paper 2 OOP Drills

These are original Paper 2-style practice questions. They use exact class names, constructors, method names, test calls, and expected output evidence.

Detailed answers are in Paper 2 OOP Answers.

Revise the topic hub first:

Questions

Question 1: Define Class

Write a class Book with:

  • constructor parameters title and copies;
  • attributes title and copies;
  • method available() returning True when copies > 0.

Test:

book = Book("Algorithms", 3)
print(book.title)
print(book.available())

Expected output:

Algorithms
True

[7]

Question 2: Create Objects

Assume the Book class from Question 1 has already been defined. Create these objects:

Book("Algorithms", 3)
Book("Networks", 0)
Book("Databases", 2)

Store them in variables b1, b2, and b3. Print each title.

Expected output:

Algorithms
Networks
Databases

[5]

Question 3: Getter Setter

Write a class Student with private-style attribute _mark.

Requirements:

  • constructor takes name and mark;
  • get_mark() returns _mark;
  • set_mark(mark) updates _mark only when 0 <= mark <= 100;
  • set_mark(mark) returns True if updated, otherwise False.

Test:

s = Student("Amy", 60)
print(s.get_mark())
print(s.set_mark(75))
print(s.get_mark())
print(s.set_mark(120))
print(s.get_mark())

Expected output:

60
True
75
False
75

[6]

Question 4: Inheritance

Write a superclass Person and subclass Student.

Requirements:

  • Person stores name and email;
  • Student inherits from Person and also stores class_name;
  • Student.summary() returns the name, class name, and email separated by single spaces.
  • Student.summary() returns "Amy 24S1 amy@example.com" for the test data.

Test:

s = Student("Amy", "amy@example.com", "24S1")
print(s.summary())

Expected output:

Amy 24S1 amy@example.com

[8]

Question 5: Override Method

Write superclass Notification with method send() returning "generic".

Write subclasses:

  • EmailNotification.send() returns "email sent";
  • SMSNotification.send() returns "sms sent".

Test:

items = [EmailNotification(), SMSNotification()]
for item in items:
    print(item.send())

Expected output:

email sent
sms sent

[8]

Question 6: Object List

Assume Book(title, copies) stores title and copies as instance attributes. Write a function available_titles(books) that returns a list of titles for books with copies > 0.

Test:

books = [Book("Algorithms", 3), Book("Networks", 0), Book("Databases", 2)]
print(available_titles(books))

Expected output:

['Algorithms', 'Databases']

[7]

Question 7: Class Diagram to Code

Implement this design:

Device
class attribute: count
- code
- name
+ label()

Device.count should start at 0 and increase by 1 whenever a new Device object is created. label() should return the device code and name separated by one space.

Test:

d1 = Device("D01", "Laptop")
d2 = Device("D02", "Tablet")
print(d1.label())
print(d2.label())
print(Device.count)

Expected output:

D01 Laptop
D02 Tablet
2

[10]

Question 8: Encapsulated Validation

Write class BankAccount with:

  • private-style attribute _balance;
  • method deposit(amount) that accepts only positive amounts;
  • deposit(amount) returns True if the balance changed, otherwise False;
  • method get_balance().

Test:

account = BankAccount(100)
print(account.deposit(50))
print(account.get_balance())
print(account.deposit(-20))
print(account.get_balance())

Expected output:

True
150
False
150

[7]

Question 9: Implementation Independence

Write class Wallet with the public methods:

  • deposit(amount)
  • withdraw(amount)
  • get_balance()

The original design stored one _balance value. In this revised version, store signed transaction amounts internally in a list called _transactions.

Requirements:

  • the constructor takes an opening balance and records it as the first transaction;
  • deposit(amount) accepts only positive amounts and returns True if accepted;
  • withdraw(amount) accepts only positive amounts when there is enough balance and returns True if accepted;
  • rejected deposits or withdrawals return False;
  • get_balance() returns the sum of _transactions;
  • the public method names must remain exactly as shown.

Test:

wallet = Wallet(100)
print(wallet.get_balance())
print(wallet.deposit(40))
print(wallet.withdraw(30))
print(wallet.withdraw(200))
print(wallet.get_balance())

Expected output:

100
True
True
False
110

[7]

Question 10: Display Method

Write class Item with attributes code, name, and price. Implement __str__() to return:

P01 Pen $1.50

for Item("P01", "Pen", 1.5).

Test:

item = Item("P01", "Pen", 1.5)
print(item)

Expected output:

P01 Pen $1.50

[5]

Review Checklist

After attempting these questions, check whether you can:

  • define classes with constructors and instance attributes;
  • create and use multiple objects;
  • protect state changes with methods;
  • implement inheritance and method overriding;
  • store objects in lists;
  • use class attributes and instance attributes correctly;
  • preserve a public interface while changing internal implementation;
  • show exact output evidence for object methods.