A2 · Practice questions · Object-oriented programming

Write the class.

Six original Cambridge-style questions on object-oriented programming in 9618 pseudocode, from terminology to writing and designing classes, with mark-scheme solutions.

Original questions All questions on this page are original work, written in the Cambridge AS & A Level style. They are not from past papers. They test the same concepts and skills the syllabus rewards.
Get the marks

Blueprint, instance, is-a, has-a.

01
Recall
[4 marks]

Explain the meaning of each of these OOP terms: (a) instance (b) encapsulation (c) getter (d) inheritance.

  • (a) An object created from a class, with its own values for the class's attributes. ✓
  • (b) Bundling attributes and methods together in a class, and hiding the attributes (PRIVATE) so they can only be accessed through the class's methods. ✓
  • (c) A method that returns the value of a (private) attribute. ✓
  • (d) A subclass takes on the attributes and methods of its superclass and can add its own or override them. ✓
02
Write
[6 marks]

A class Pupil has private attributes PupilName (a string) and Score (an integer). Write pseudocode for the class, including: a constructor that takes the name and sets Score to 0; a getter for Score; a setter SetScore that only accepts a score from 0 to 100 inclusive.

  • CLASS Pupil ... ENDCLASS. ✓
  • PRIVATE PupilName : STRING and PRIVATE Score : INTEGER. ✓
  • PUBLIC PROCEDURE NEW(GivenName : STRING) setting PupilName ← GivenName and Score ← 0, ENDPROCEDURE. ✓
  • PUBLIC FUNCTION GetScore() RETURNS INTEGER / RETURN Score / ENDFUNCTION. ✓
  • PUBLIC PROCEDURE SetScore(NewScore : INTEGER) with IF NewScore >= 0 AND NewScore <= 100 THEN Score ← NewScore ENDIF. ✓
  • ENDPROCEDURE and correct use of PUBLIC / PRIVATE throughout. ✓
03
Write
[4 marks]

A class Prefect inherits from Pupil and has an extra private attribute Duty (a string). Write the class header, the attribute declaration and the constructor, which takes a name and a duty.

  • CLASS Prefect INHERITS Pupil. ✓
  • PRIVATE Duty : STRING. ✓
  • PUBLIC PROCEDURE NEW(GivenName : STRING, GivenDuty : STRING) calling SUPER.NEW(GivenName). ✓
  • Duty ← GivenDuty, ENDPROCEDURE, ENDCLASS. ✓
04
Trace
[3 marks]

Using the Pupil class from question 2, state the output of:

P1 ← NEW Pupil("Amir")
P1.SetScore(72)
P1.SetScore(130)
OUTPUT P1.GetScore()
P2 ← NEW Pupil("Bea")
OUTPUT P2.GetScore()
  • SetScore(72) is accepted, so P1's Score is 72. ✓
  • SetScore(130) is rejected by the setter, so P1's Score stays 72: output 72. ✓
  • P2 is a separate instance whose constructor set Score to 0: output 0. ✓
05
Explain
[3 marks]

Explain, with an example, what is meant by polymorphism.

  • A method in a subclass has the same name as a method in the superclass but a different implementation (it overrides it). ✓
  • The same method call behaves differently depending on the class of the object it is called on. ✓
  • Example: Shape has Area(); Circle and Rectangle override Area(), so calling Area() on each object in an array of Shape calculates the correct area for each. ✓
06
Analysis
[4 marks]

A garage system records vehicles. Every vehicle has a registration and a year. Cars also have a number of doors and vans have a load capacity. A garage stores up to 50 vehicles. Describe a suitable class design, naming the classes, their relationships and the attributes of each.

  • Superclass Vehicle with PRIVATE Registration : STRING and PRIVATE Year : INTEGER (with constructor and getters). ✓
  • Car INHERITS Vehicle adding PRIVATE NumDoors : INTEGER; Van INHERITS Vehicle adding PRIVATE Capacity : REAL. ✓
  • Garage contains vehicles: PRIVATE Vehicles : ARRAY[1:50] OF Vehicle and a count of vehicles stored. ✓
  • Car and Van use inheritance (is-a); Garage uses containment (has-a). ✓

Mark this once you have attempted all six and checked your working. It records a Practiced badge on the topic and adds a one-time bonus. Revealing the solutions alone does not count.