Computer Science · B3

Object-oriented programming

Design classes in UML and build them in code: objects, static members and encapsulation. In 7Quest it is Object Citadel: 8 stages, 70 quick questions and 21 exam-style ones.

What the stages cover

Codes follow the 2027 guide (A1–A4 and B1–B4). HL-only stages are marked. Each stage is a short lesson, quick questions, a minigame and an exam-style question.

  1. OOP fundamentals
    You will be able to distinguish between a class and an object and between state and behaviour, and evaluate the advantages and disadvantages of object-oriented programming.
    Syllabus B3.1.1
  2. Defining classes and instantiating objects
    You will be able to write a class with a constructor, instance attributes and methods, create several objects and call their methods.
    Syllabus B3.1.4
  3. Designing classes with UML
    You will be able to construct the UML class diagram for a real scenario: attributes with types, methods with parameters and return types, visibility and static members.
    Syllabus B3.1.2
  4. Static and instance members
    You will be able to distinguish static variables and methods from instance ones, predict their effect and decide when to use each.
    Syllabus B3.1.3
  5. Encapsulation and information hiding
    You will be able to explain encapsulation and apply it in code with private attributes, and getters and setters with validation.
    Syllabus B3.1.5
  6. Inheritance and polymorphismHL
    You will be able to use inheritance to reuse and extend classes, explain how access modifiers affect subclasses, and construct code that shows dynamic and static polymorphism.
    Syllabus B3.2.1, B3.2.2
  7. Abstraction, composition and aggregationHL
    You will be able to explain abstraction in OOP, write abstract classes, and distinguish composition, aggregation and association using lifecycles and UML notation.
    Syllabus B3.2.3, B3.2.4
  8. Design patternsHL
    You will be able to explain the singleton, factory and observer patterns, implement them in code and choose a suitable pattern for a scenario.
    Syllabus B3.2.5

Two questions to try

Written for 7Quest in the style of the exam, not taken from past papers. Answer first, then open the markscheme.

State3 marksDefining classes and instantiating objects

State the three lines of output produced by the following code.

class Tank:
    def __init__(self, capacity):
        self.capacity = capacity
        self.level = 0

    def fill(self, litres):
        self.level = min(self.level + litres, self.capacity)

t1 = Tank(50)
t2 = Tank(20)
t1.fill(30)
t2.fill(30)
t1.fill(30)
print(t1.level)
print(t2.level)
print(t1.capacity - t1.level)
Show the markscheme

Markscheme (3 marks)

  1. 50
  2. 20
  3. 0

A full-marks answer

Output: 50, 20 and 0. t1 goes from 0 to 30 and then to min(60, 50), which is 50 (it cannot exceed its capacity); t2 receives min(30, 20), which is 20, because its capacity is 20; finally 50−50=050 - 50 = 0. Each tank keeps its own level.

State the three lines of output produced by the following code.

class Tank {
    private int capacity;
    private int level;

    public Tank(int capacity) {
        this.capacity = capacity;
        this.level = 0;
    }

    public void fill(int litres) {
        level = Math.min(level + litres, capacity);
    }

    public int getCapacity() {
        return capacity;
    }

    public int getLevel() {
        return level;
    }
}

public class Main {
    public static void main(String[] args) {
        Tank t1 = new Tank(50);
        Tank t2 = new Tank(20);
        t1.fill(30);
        t2.fill(30);
        t1.fill(30);
        System.out.println(t1.getLevel());
        System.out.println(t2.getLevel());
        System.out.println(t1.getCapacity() - t1.getLevel());
    }
}
Show the markscheme

Markscheme (3 marks)

  1. 50
  2. 20
  3. 0

A full-marks answer

Output: 50, 20 and 0. t1 goes from 0 to 30 and then to Math.min(60, 50), which is 50 (it cannot exceed its capacity); t2 receives Math.min(30, 20), which is 20, because its capacity is 20; finally 50−50=050 - 50 = 0. Each Tank object keeps its own level.

Evaluate4 marksOOP fundamentals

A team of five programmers is developing a school management system that handles students, teachers, courses and timetables. Evaluate the use of object-oriented programming for this project.

Show the markscheme

Markscheme (4 marks)

  1. Modularity/teamwork: each programmer develops and tests different classes (Student, Teacher, Course) in parallel.
  2. Reusability: the Student class is used in several modules (grades, attendance) / many objects of the same class.
  3. Maintainability/encapsulation: internal changes to one class do not affect the rest of the system.
  4. Disadvantage: requires more design time (UML) and greater complexity / memory overhead.
  5. Reasoned conclusion: suitable because the system is large, with many entities and a team, so the benefits outweigh the cost of design.

A full-marks answer

OOP is very suitable. The system has clear entities (student, teacher, course, timetable) that can be modelled as classes, and each programmer can develop and test their own classes separately, which makes teamwork easier. The Student class can be reused in both the grades and the attendance modules, and because its data are encapsulated, a change to its internal implementation does not require changes to the rest of the program. On the downside, it requires more design up front with UML diagrams, and the code is more complex than a procedural program. Even so, given the size of the system and the number of programmers, the benefits of modularity, reusability and maintainability clearly outweigh that cost.

The other 19 questions in this topic are in the app, marked point by point, and anything you miss comes back in your review. If this is the topic you find hardest, you can make it your free topic and do all of it, with short practice tests on it, without paying or giving a card.

Start Computer Science free