Question bank › IB Computer Science › Theme B – Computational thinking & problem-solving

B3 Object-oriented programming (classes, inheritance, polymorphism) (HL-heavy)

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]

2StandardHL only5 marks

Consider the program below.

class Vehicle:
    def __init__(self, reg, wheels=4):
        self.reg = reg
        self.wheels = wheels

    def describe(self):
        return self.reg + " has " + str(self.wheels) + " wheels"

class Motorbike(Vehicle):
    def __init__(self, reg):
        super().__init__(reg, 2)
        self.sidecar = False

    def describe(self):
        return super().describe() + " and no sidecar"

v = Vehicle("AB12")
m = Motorbike("XY99")
print(v.describe())
print(m.describe())
print(isinstance(m, Vehicle))

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

(b) Identify the method, other than the constructor __init__, that is overridden in Motorbike. [1]

(c) State the purpose of super().__init__(reg, 2). [1]

3StandardHL only4 marks

Consider the program below.

import math

class Shape:
    def area(self):
        return 0

class Circle(Shape):
    def __init__(self, r):
        self.r = r
    def area(self):
        return math.pi * self.r ** 2

class Rectangle(Shape):
    def __init__(self, w, h):
        self.w = w
        self.h = h
    def area(self):
        return self.w * self.h

for s in [Circle(3), Rectangle(4, 5)]:
    print(type(s).__name__ + ": " + format(s.area(), ".2f"))

(a) State the output. [2]

(b) Define the term polymorphism. [1]

(c) Outline how the for loop demonstrates polymorphism. [1]

4Hard6 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]

5Standard4 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]

6HardHL only6 marks

A library system uses the class diagram shown. Item is implemented as:

class Item:
    def __init__(self, title, item_id):
        self.title = title
        self.item_id = item_id

    def late_fee(self, days_late):
        return 0.10 * days_late

Book and DVD are subclasses of Item. A DVD costs £0.50 per day late; an ordinary Item costs £0.10 per day late.

(a) State the superclass and identify one attribute that a DVD object inherits. [2]

(b) Write the class DVD, including its constructor and a version of late_fee that charges £0.50 per day. [4]

Itemtitle, item_idlate_fee(days_late)BookauthorDVDdurationlate_fee(days_late)

Still losing marks on this topic?

Book a free introductory session and we'll find exactly which step is costing you.

Book a free intro