Question bank › IB Computer Science › B3 Object-oriented programming

OOP for a single class: classes, objects, UML, static members, encapsulation

Answer on paper first, then reveal the mark scheme one mark at a time.

1Core5 marks

Write a Python class Student with attributes name (a string) and marks (a list of integers), set when an object is created, and a method average() that returns the mean of the marks.

[5]

2Hard6 marks

A bank wants to encapsulate its account data. Write a Python class BankAccount that:

  • stores the balance as a private attribute set from an opening balance;
  • has a method get_balance();
  • has a method deposit(amount) that only accepts positive amounts;
  • has a method withdraw(amount) that does not allow the balance to go below zero.

Each method that changes the balance should return True if it succeeded and False otherwise.

[6]

3Standard4 marks

A program models pets using a class Dog. The object rex = Dog("Rex", 3) is created, where "Rex" is the name and 3 is the age.

(a) Distinguish between a class and an object, using the Dog example. [2]

(b) State two benefits of using an object-oriented approach (classes and objects) when designing a large program. [2]

Stuck on OOP for a single class: classes, objects, UML, static members, encapsulation?

Get it sorted in a free 30-minute session

We work through your weakest questions on this topic on Zoom. I reply the same day.

4Core5 marks

The UML class diagram shows the design of a class CinemaTicket used by a cinema booking system. Underlined members are static.

(a) Identify the constructor of the class. [1]

(b) State what the symbol - in front of seat indicates about how seat can be accessed. [1]

(c) Identify the static attribute and state one difference between it and the attribute price. [2]

(d) State the data type of the value returned by applyDiscount. [1]

CinemaTicket- ticketsSold : int- seat : String- price : double- isVip : boolean+ CinemaTicket(seat : String, price : double)+ getPrice() : double+ upgradeToVip() : void+ applyDiscount(percent : int) : boolean+ getTicketsSold() : int
5Standard4 marks

A football stadium counts the people entering through each of its turnstiles. The following Java code is used.

public class Turnstile {
    private static int totalEntries = 0;
    private int entries = 0;

    public void admit() {
        entries++;
        totalEntries++;
    }
    public int getEntries() { return entries; }
    public static int getTotalEntries() { return totalEntries; }
}

// in another class
Turnstile north = new Turnstile();
Turnstile south = new Turnstile();
north.admit();
north.admit();
south.admit();
Turnstile east = new Turnstile();
east.admit();
north.admit();
System.out.println(north.getEntries() + " " + south.getEntries() + " " + east.getEntries());
System.out.println(Turnstile.getTotalEntries());

(a) State the two lines of output produced. [2]

(b) Explain why getTotalEntries() can be called using the class name Turnstile, but getEntries() must be called on a particular object. [2]

6Standard5 marks

A leisure centre is writing software to manage its lockers. The following design notes have been produced for a class Locker.

  • Each locker has a locker number (a whole number), a 4-digit PIN (stored as text) and a flag showing whether it is currently locked.
  • The PIN must not be readable or changeable directly by other classes.
  • A new locker is created with its number and PIN, and starts unlocked.
  • Users can lock the locker. To unlock it they must supply a PIN; the method reports whether unlocking succeeded.
  • Users can ask whether the locker is locked.
  • The class keeps one count of how many Locker objects have been created.

Construct a UML class diagram for the Locker class. [5]

7Standard5 marks

A smart-home system uses the following two Java classes.

public class Thermostat {
    private double target;
    private static final double MAX = 28.0;

    public Thermostat(double t) { target = t; }
    public void setTarget(double t) {
        if (t <= MAX) { target = t; }
    }
    public double getTarget() { return target; }
    public static double getMax() { return MAX; }
}

public class Home {
    public static void main(String[] args) {
        Thermostat hall = new Thermostat(19.5);          // line 1
        hall.target = 30.0;                              // line 2
        hall.setTarget(30.0);                            // line 3
        System.out.println(Thermostat.getTarget());      // line 4
        System.out.println(Thermostat.getMax());         // line 5
    }
}

(a) The class Home does not compile. Identify the two numbered lines that cause errors and explain the cause of each. [4]

(b) Line 2 is deleted and line 4 is replaced by System.out.println(hall.getTarget());. State the output of the program. [1]

8Hard7 marks

A courier company charges for parcels by weight. Each parcel is given a unique ID: the first parcel created has ID 1001, the next 1002, and so on.

The charge is £3.50 for a parcel weighing up to and including 2 kg, plus £1.20 for every extra kilogram or part of a kilogram above 2 kg.

(a) Construct a Python class Parcel that:

  • uses a static (class) variable to generate the IDs;
  • has a constructor that takes the weight in kg, stores it as a private attribute and gives the object the next ID;
  • has a method get_id() that returns the ID;
  • has a method cost() that returns the charge in pounds.

[5]

(b) The objects p1 = Parcel(1.5) and p2 = Parcel(4.2) are the first two parcels created. State the value returned by p2.get_id() and by p2.cost(). [2]

9Hard4 marks

Two programming tasks are being planned.

  • Task A: a 30-line script, written by one student and used once, that renames all the photos in a folder by adding the date to each file name.
  • Task B: a ticketing system for a theatre chain, developed by a team of eight programmers and maintained for several years. It must model seats, performances and customers.

Evaluate the use of an object-oriented approach for Task A and for Task B. [4]

10Stretch5 marks

A conference app should count how many visitor badges have been issued. A programmer writes this Python code.

class Badge:
    issued = 0

    def __init__(self, name):
        self.name = name
        self.issued += 1

a = Badge("Ana")
b = Badge("Ben")
print(a.issued, b.issued, Badge.issued)

(a) State the output of the program. [1]

(b) Explain why Badge.issued does not show the number of badges created. [2]

(c) Rewrite the line self.issued += 1 so that the count works correctly. [1]

(d) State the output after the correction in (c). [1]

Stuck on this topic?

Book a free 30-minute session on WhatsApp and we'll find exactly which step is costing you the marks. Or build a timed paper on it.

Free 30-minute session