Skip to content

9618 · 20.1

Programming Paradigms — practice questions

Practice and worked examples for 9618 Programming Paradigms. Short previews only — attempt the full question in MarkScheme against the official scheme.

Worked example 1

The following pseudocode calculates the area of a circle. Identify the programming paradigm and justify your answer with reference to the code.

DECLARE Radius : REAL
DECLARE Area : REAL

PROCEDURE CalculateArea(InRadius : REAL)
  Area <- 3.14159 * InRadius * InRadius
ENDPROCEDURE

OUTPUT "Enter the radius:"
INPUT Radius

CalculateArea(Radius)

OUTPUT "The area is: ", Area
Show solution outline

Paradigm: Procedural Programming (a type of Imperative paradigm).

Justification:

  1. Use of Procedures: The code is structured around a PROCEDURE named CalculateArea which contains a set of instructions to perform a task. [1 mark]
  2. Sequential Execution: The main part of the program follows a clear, step-by-step sequence of commands (OUTPUT, INPUT, procedure call, OUTPUT). [1 mark]
  3. Shared State / Global Variables: The procedure CalculateArea modifies a global variable Area, which is a common feature in procedural programming where data and operations are not tightly coupled. [1 mark]

Worked example 2

Analyse the following pseudocode. Identify the paradigm and two distinct features of the paradigm present in the code.

CLASS Vehicle
  PRIVATE TopSpeed : INTEGER

  PUBLIC PROCEDURE New(Speed : INTEGER)
    TopSpeed <- Speed
  ENDPROCEDURE

  PUBLIC FUNCTION GetTopSpeed() RETURNS INTEGER
    RETURN TopSpeed
  ENDFUNCTION
ENDCLASS

CLASS Car INHERITS Vehicle
  PRIVATE NumberOfDoors : INTEGER

  PUBLIC PROCEDURE New(Speed : INTEGER, Doors : INTEGER)
    SUPER.New(Speed)
    NumberOfDoors <- Doors
  ENDPROCEDURE
ENDCLASS

MyCar : Car
MyCar <- NEW Car(120, 4)
Show solution outline

Paradigm: Object-Oriented Programming (OOP).

Features:

  1. Inheritance: The Car class is defined to INHERIT from the Vehicle class. This means Car automatically has the properties and methods of Vehicle, such as TopSpeed and GetTopSpeed(). [1 mark]
  2. Encapsulation: Data such as TopSpeed is declared as PRIVATE, meaning it can only be accessed or modified through PUBLIC methods (New, GetTopSpeed). This bundling of data and methods and controlling access is encapsulation. [1 mark] (Alternative valid features include: Use of Classes as blueprints (CLASS Vehicle), Instantiation of objects (MyCar<NEWCar(...)MyCar <- NEW Car(...))).