Revision Library

Object-Oriented Programming - Worksheets, Questions and Revision

12 original exam-style questions - 7 pages of questions with a full mark scheme - free printable PDF.

Download PDFJump to mark scheme (page 8)Read the revision guide
« Previous: Databases, SQL and NormalisationNext: Recursion and Advanced Algorithms »
Revision Library
revisionlibrary.co.uk
A-Level · AQA

A1.9 Object-Oriented Programming

AQA 7517 · Calculators not allowed · about 150 minutes
Total Marks
Name: _______________________________    Date: ____ / ____ / ______
Answer ALL questions. Show all your working.
1
Object-oriented programming (OOP) uses a specific set of terms to describe how programs are structured. Define each of the following terms, giving a short example in each case.
(a)class(2)
(b)object(2)
(c)attribute(2)
(d)method(2)
(Total for Question 1 is 8 marks)
2
A programmer writes the following AQA-style pseudocode class definition for a simple Student record.

class Student
private studentName
private examScore

public new(name, score)
     studentName <- name
     examScore <- score
  endnew

public getScore()
     return examScore
  endgetScore
endclass
(a)Explain the purpose of the new() subroutine in the class above.(2)
(b)Write one line of pseudocode that creates a Student object called student1, representing a student named "Priya" with an exam score of 68.(2)
(c)State the value that would be returned by the call student1.getScore(), and identify the visibility keyword that would need to change if examScore was instead to be accessed directly as student1.examScore.(2)
(Total for Question 2 is 6 marks)
3
A class diagram for a BankAccount class is shown below.

BankAccount
-----------------------------------
- accountNumber : String
- balance : Real
-----------------------------------
+ new(accNum : String, openingBalance : Real)
+ deposit(amount : Real) : Void
+ withdraw(amount : Real) : Boolean
+ getBalance() : Real
(a)State what the "-" symbol before accountNumber and balance indicates, and explain why a programmer would choose this visibility for these attributes.(3)
(b)Using AQA pseudocode conventions, write the class definition for BankAccount, including the constructor new(), the deposit() method, and the getBalance() method. The deposit() method should add amount to balance, provided amount is greater than 0.(6)
(c)Using AQA pseudocode conventions, write the withdraw(amount) method. It should return TRUE and reduce balance by amount if there are sufficient funds (balance ≥ amount); otherwise it should return FALSE and leave balance unchanged.(4)
(d)A second programmer suggests making balance a public attribute so that other parts of the program can update it directly, instead of using deposit() and withdraw(). Explain two problems this could cause.(3)
(Total for Question 3 is 16 marks)
4
Explain how encapsulation is achieved in an object-oriented program, and discuss the benefits it brings to the development of a large software system.
(Total for Question 4 is 9 marks)
5
A programmer is designing a class hierarchy to represent different shapes. The superclass Shape stores a colour attribute, and every shape (whatever its type) should have a method area() that calculates its own area.
(a)Using AQA pseudocode conventions, write the superclass Shape. It should have a private attribute colour, a constructor new(col) that sets colour, a method getColour() that returns colour, and a method area() that returns 0 (to be overridden by subclasses).(5)
(b)Write the subclass Circle, which inherits from Shape. It should have an additional private attribute radius, a constructor that accepts a colour and a radius (calling the superclass constructor to set the colour), and an overridden method area() that returns π * radius ^ 2.(6)
(c)Write the subclass Rectangle, which inherits from Shape. It should have additional private attributes width and height, a constructor that accepts a colour, width and height, and an overridden area() method that returns width * height.(4)
(d)Explain what is meant by "overriding" a method, using area() in Circle and Rectangle as your example.(2)
(Total for Question 5 is 17 marks)
6
The program below creates an array of three Shape objects (using the classes from Question 5) and processes them in a loop.
shapes <- new Array(3)
shapes[0] <- new Circle("red", 3)
shapes[1] <- new Rectangle("blue", 4, 5)
shapes[2] <- new Circle("green", 2)
total <- 0
FOR i <- 0 TO 2
   total <- total + shapes[i].area()
ENDFOR
OUTPUT total
(a)Complete a trace table to show the value returned by shapes[i].area() and the running value of total on each pass of the loop, and state the value finally output. Use π = 3.14159 and give values to 2 decimal places.(6)
(b)Explain why the line shapes[i].area() calls a different version of the area() method depending on whether shapes[i] refers to a Circle object or a Rectangle object. Use the term polymorphism in your answer.(3)
(c)Explain why the FOR loop uses FOR i <- 0 TO 2 rather than FOR i <- 0 TO 3, given that the array shapes holds three elements.(2)
(Total for Question 6 is 11 marks)
7
A software developer is modelling a Car class. She considers two possible designs.

Design 1: Car has an attribute engine, which stores an Engine object (so Car contains an Engine).

Design 2: A class SportsCar inherits from Car.
(a)State the type of relationship shown in Design 1, and explain what it means for one class to be related to another in this way.(3)
(b)State the type of relationship shown in Design 2, and explain what it means for one class to be related to another in this way.(3)
(c)A SportsCar is a type of Car, but a Car is not a type of Engine. Explain why this means inheritance is an appropriate relationship between SportsCar and Car, but not between Car and Engine.(2)
(Total for Question 7 is 8 marks)
8
A junior programmer has written the following class, but has not applied good object-oriented design principles.

class Student
public studentName
public examScore

public new(name, score)
     studentName <- name
     examScore <- score
  endnew
endclass

Elsewhere in the program, another part of the code contains the line:
student1.examScore <- 150
(a)Identify two problems with this class and the line of code shown above.(4)
(b)Rewrite the class using AQA pseudocode conventions so that studentName and examScore are properly encapsulated. Include a constructor, a getExamScore() method, and a setExamScore(newScore) method that only updates examScore if newScore is between 0 and 100 inclusive.(6)
(Total for Question 8 is 10 marks)
9
A company is developing a large payroll system. It currently has a single class, Employee, which includes attributes and methods for all types of staff, including office workers, managers and factory workers, with many IF statements inside its methods to handle the different types of employee differently.

A programmer suggests refactoring the system to use inheritance, creating subclasses such as Manager and FactoryWorker that inherit from Employee.

Discuss the extent to which using inheritance would improve the design of this payroll system.
(Total for Question 9 is 9 marks)
10
A library system uses a superclass Book with a private attribute title and a private attribute author. It has a constructor new(t, a) and a method getDetails() that returns a string in the form "title by author".

Two subclasses are needed:

EBook, which adds a private attribute fileSizeMB, with a constructor that also accepts the file size, and overrides getDetails() so it returns the Book version's string followed by " (ebook, " then the file size then "MB)".

PrintBook, which adds a private attribute shelfCode, with a constructor that also accepts the shelf code, and overrides getDetails() so it returns the Book version's string followed by " [Shelf: " then the shelf code then "]".
(a)Write the superclass Book, using AQA pseudocode conventions.(4)
(b)Write the subclass EBook.(5)
(c)Write the subclass PrintBook.(5)
(Total for Question 10 is 14 marks)
11
The following program creates an array of Book objects (using the classes from Question 10) and outputs the details of each one in turn.
library <- new Array(3)
library[0] <- new Book("Harbour Lights", "Amara Osei")
library[1] <- new EBook("The Long Tide", "Fatima Chowdhury", 4)
library[2] <- new PrintBook("Northbound", "Callum Ferguson", "F-12")

FOR i <- 0 TO 2
   OUTPUT library[i].getDetails()
ENDFOR
(a)Complete a trace table to show the exact string output on each pass of the loop.(6)
(b)Explain why it is good practice for EBook's getDetails() method to call super.getDetails() rather than rewriting the "title by author" logic again from scratch.(2)
(c)A fourth element, library[3], containing an Audiobook object (a further subclass of Book, not shown here, which also overrides getDetails()), is added, and the loop is changed to FOR i <- 0 TO 3. State which version of getDetails() would run for this new element, and explain why the existing loop code does not need to change to support it.(2)
(Total for Question 11 is 10 marks)
12
A start-up company is building a simulation of a hospital, modelling wards, patients, doctors and equipment, each with related data and behaviour, and expects to keep extending the simulation with new features for several years.

Discuss whether an object-oriented programming language or a procedural programming language would be more suitable for this project.
(Total for Question 12 is 9 marks)
Mark scheme · A1.9 Object-Oriented Programming

Question 1

Question 2

Question 3

Question 4

Question 5

Question 6

Question 7

Question 8

Question 9

Question 10

Question 11

Question 12

Mark your answers

This checks your answers in your browser, stores nothing on a server and needs no account.

Question 1

8 marks
Did your answer earn the marks?

Question 2

6 marks
Did your answer earn the marks?

Question 3

16 marks
Did your answer earn the marks?

Question 4

9 marks
Did your answer earn the marks?

Question 5

17 marks
Did your answer earn the marks?

Question 6

11 marks
Did your answer earn the marks?

Question 7

8 marks
Did your answer earn the marks?

Question 8

10 marks
Did your answer earn the marks?

Question 9

9 marks
Did your answer earn the marks?

Question 10

14 marks
Did your answer earn the marks?

Question 11

10 marks
Did your answer earn the marks?

Question 12

9 marks
Did your answer earn the marks?
Mark my answers