Programming Paradigms and OOP - 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)
« Previous: Algorithms: Complexity, Searching and SortingNext: Data Structures and Their Operations »
Revision Library
revisionlibrary.co.uk
A-Level · Computer Science - Programming Paradigms and Object-Oriented Programming

A2.4 Programming Paradigms and OOP

OCR H446 · Calculators not allowed · about 100 minutes
Total Marks
Name: _______________________________    Date: ____ / ____ / ______
Answer ALL questions. Show all your working.
1
A programming paradigm is a general style or approach used to structure a solution to a problem. Different paradigms lead to programs being organised in very different ways, even when solving the same problem.
(a)Define what is meant by a 'programming paradigm'.(1)
(b)State one feature of procedural programming.(1)
(c)State one feature of object-oriented programming.(1)
(d)State one feature of functional programming.(1)
(Total for Question 1 is 4 marks)
2
A small program for a Bristol newsagent totals up the cost of a customer's magazines. It is written using OCR's Exam Reference Language conventions (assignment <-, iteration FOR ... NEXT, selection IF ... ENDIF).
PROCEDURE CalculateTotal(price, quantity)
  total <- price * quantity
  RETURN total
ENDPROCEDURE

PROCEDURE PrintReceipt(item, price, quantity)
  total <- CalculateTotal(price, quantity)
  OUTPUT item, " x ", quantity, " = GBP ", total
ENDPROCEDURE

CALL PrintReceipt("Weekly Puzzle Magazine", 2.50, 4)
(a)Identify the programming paradigm used in the code above.(1)
(b)Give two reasons for your answer to part (a), referring directly to the code.(2)
(c)State one change that would need to be made to this code for it to become object-oriented.(1)
(Total for Question 2 is 4 marks)
3
A school in Preston is building a simple pseudocode class to represent a student's exam mark. The starting point is shown below, using OCR's Exam Reference Language class conventions.
CLASS ExamResult
  PRIVATE mark : INTEGER

PUBLIC PROCEDURE NEW(startingMark)
    mark <- startingMark
  ENDPROCEDURE

PUBLIC FUNCTION GetMark() RETURNS INTEGER
    RETURN mark
  ENDFUNCTION
ENDCLASS
(a)Identify which attribute of ExamResult is encapsulated, and state the keyword in the code that enforces this.(2)
(b)Explain why mark is declared PRIVATE rather than PUBLIC in this class.(2)
(c)Complete the pseudocode for a public procedure SetMark(newMark) that only updates mark if newMark is between 0 and 100 inclusive; otherwise it should output an error message and leave mark unchanged.(2)
(Total for Question 3 is 6 marks)
4
A secondary school in Norwich wants a pseudocode class to represent a book held in its school library.
(a)Write pseudocode for a class LibraryBook with four private attributes: title (STRING), author (STRING), isbn (STRING) and isOnLoan (BOOLEAN); and a constructor PROCEDURE NEW(t, a, i) that sets title, author and isbn from its parameters and sets isOnLoan to FALSE.(3)
(b)Write a public procedure BorrowBook() for LibraryBook that sets isOnLoan to TRUE and outputs a confirmation message if the book is not already on loan; otherwise it should output a message stating the book is already on loan and leave isOnLoan unchanged.(3)
(Total for Question 4 is 6 marks)
5
A payroll system for a chain of shops in Glasgow uses a class Employee, shown below, and needs a subclass Teacher for staff at the company's in-house training academy.
CLASS Employee
  PRIVATE name : STRING
  PRIVATE salary : REAL

PUBLIC PROCEDURE NEW(n, s)
    name <- n
    salary <- s
  ENDPROCEDURE

PUBLIC FUNCTION GetDetails() RETURNS STRING
    RETURN name + ", salary GBP " + STR(salary)
  ENDFUNCTION
ENDCLASS
(a)Complete the class header line below so that Teacher inherits from Employee and adds one new private attribute subject (STRING).
CLASS Teacher ______
  PRIVATE subject : STRING
  ...
ENDCLASS
(2)
(b)Write pseudocode for Teacher's constructor PROCEDURE NEW(n, s, subj), which should call the Employee constructor to set name and salary, then set subject from its own parameter.(3)
(c)Explain what is meant by 'method overriding', using a version of GetDetails written for Teacher as your example.(2)
(Total for Question 5 is 7 marks)
6
A graphics package for a design studio in Manchester defines a superclass Shape and two subclasses, Circle and Rectangle, each overriding a function CalculateArea.
CLASS Shape
  PUBLIC FUNCTION CalculateArea() RETURNS REAL
    RETURN 0
  ENDFUNCTION
ENDCLASS

CLASS Circle INHERITS Shape
  PRIVATE radius : REAL
  PUBLIC PROCEDURE NEW(r)
    radius <- r
  ENDPROCEDURE
  PUBLIC FUNCTION CalculateArea() RETURNS REAL
    RETURN pi * radius ^ 2
  ENDFUNCTION
ENDCLASS

CLASS Rectangle INHERITS Shape
  PRIVATE width : REAL
  PRIVATE height : REAL
  PUBLIC PROCEDURE NEW(w, h)
    width <- w
    height <- h
  ENDPROCEDURE
  PUBLIC FUNCTION CalculateArea() RETURNS REAL
    RETURN width * height
  ENDFUNCTION
ENDCLASS

A list shapes contains a mixture of Circle and Rectangle objects. The following loop is used to print each shape's area:
FOR EACH s IN shapes
  OUTPUT s.CalculateArea()
NEXT s
(a)Explain what is meant by polymorphism in object-oriented programming, referring to the Shape, Circle and Rectangle classes above.(3)
(b)Using the loop given in the question, explain why the code that processes the shapes list does not need to know whether each item is a Circle or a Rectangle.(3)
(Total for Question 6 is 6 marks)
7
A vehicle-hire company in Oxford models its fleet using the classes below.
CLASS Vehicle
  PROTECTED make : STRING

PUBLIC PROCEDURE NEW(m)
    make <- m
  ENDPROCEDURE

PUBLIC FUNCTION Describe() RETURNS STRING
    RETURN make + ": generic vehicle"
  ENDFUNCTION
ENDCLASS

CLASS Car INHERITS Vehicle
  PUBLIC FUNCTION Describe() RETURNS STRING
    RETURN make + ": car, 4 wheels"
  ENDFUNCTION
ENDCLASS

CLASS Motorbike INHERITS Vehicle
  PUBLIC FUNCTION Describe() RETURNS STRING
    RETURN make + ": motorbike, 2 wheels"
  ENDFUNCTION
ENDCLASS

The following driver program is then run:
fleet <- []
fleet.APPEND(NEW Vehicle("Generic Co"))
fleet.APPEND(NEW Car("Skoda"))
fleet.APPEND(NEW Motorbike("Triumph"))
FOR EACH v IN fleet
  OUTPUT v.Describe()
NEXT v
(a)State the three lines of output produced by the driver program, in the order they are produced.(4)
(b)Explain why the second and third lines of output use the overridden versions of Describe() in Car and Motorbike, rather than the version defined in Vehicle, even though the loop only ever calls v.Describe().(4)
(Total for Question 7 is 8 marks)
8
A small business owner in Cardiff is developing a new student grade-tracking system for a secondary school. They are deciding whether to use the procedural paradigm or the object-oriented paradigm to build it. The system must store many students' names, subjects and marks, calculate averages and grade boundaries, and will be extended over several years by different developers as new features (e.g. attendance tracking) are added.
(a)Compare and contrast the procedural and object-oriented approaches for developing this grade-tracking system, discussing at least two relevant factors (for example: maintainability, code reuse, protection of data, or suitability for a system that will be extended by a team of developers over time).(8)
(Total for Question 8 is 8 marks)
9
A weather-logging tool for a network of stations across the UK uses functions to process a list of daily temperature readings (in degrees Celsius).
globalTotal <- 0

FUNCTION AddToGlobalTotal(x) RETURNS REAL
  globalTotal <- globalTotal + x
  RETURN globalTotal
ENDFUNCTION

FUNCTION CelsiusToFahrenheit(c) RETURNS REAL
  RETURN (c * 9 / 5) + 32
ENDFUNCTION
(a)Define what is meant by a 'pure function' in functional programming.(2)
(b)State one benefit of avoiding mutable/global state when writing functions.(2)
(c)Identify which of the two functions given in the question, AddToGlobalTotal or CelsiusToFahrenheit, is impure, and give a reason for your answer.(2)
(Total for Question 9 is 6 marks)
10
A car-leasing company in Leicester wants to model different types of vehicle and calculate their estimated annual running cost.
(a)Write pseudocode for a class Vehicle with one private attribute annualMileage (REAL), a constructor PROCEDURE NEW(miles) that sets annualMileage, and a public function CalculateRunningCost() RETURNS REAL that simply returns 0 (this will be overridden by every subclass).(4)
(b)Write pseudocode for a subclass ElectricCar that inherits from Vehicle, and overrides CalculateRunningCost() so that it returns annualMileage multiplied by a fixed cost of 0.04 (pounds per mile for electricity).(3)
(c)Write a procedure PrintTotalRunningCost(vehicles) that takes a list of Vehicle (and/or subclass) objects and outputs the total combined running cost of every vehicle in the list, using polymorphism so that it works correctly whatever mixture of vehicle subclasses the list contains.(3)
(Total for Question 10 is 10 marks)
11
A developer is designing a class hierarchy for a delivery-tracking system and is considering whether a DeliveryDriver class should inherit from both a Person class and a VehicleOperator class (multiple inheritance), or whether it would be better to keep the hierarchy simpler.
(a)Discuss one potential problem that can arise from using multiple inheritance (a class inheriting directly from two or more superclasses).(3)
(b)Discuss one potential problem that can arise from overusing deep inheritance hierarchies in general (a long chain of subclasses inheriting from subclasses), even without multiple inheritance.(3)
(Total for Question 11 is 6 marks)
12
A leisure centre in Swansea wants a new booking system for its fitness classes. Classes have different formats: some are individually instructed sessions (e.g. personal training), and some are group sessions (e.g. spin classes, yoga) with a fixed maximum number of participants. The centre expects to add further class formats in future years and wants the system to be easy to maintain and extend.
(a)Design an object-oriented model for this booking system. Your answer should describe (in words and/or pseudocode) at least: a base class shared by all class formats, two subclasses with genuinely different (polymorphic) behaviour, appropriate encapsulation with validated attributes, and a justification of why an object-oriented approach suits a system that will keep being extended with new class formats.(10)
(Total for Question 12 is 10 marks)
Mark scheme · A2.4 Programming Paradigms and OOP

Question 1

Question 2

Question 3

Question 4

Question 5

Question 6

Question 7

Question 8

Question 9

Question 10

Question 11

Question 12