9618/43 · May/June 2023 · Worked solutions

Step-by-step solutions based on the mark scheme (not a mark-scheme dump).

Question 1

Paper page 2 for Q1
Original paper page 2

Part 1(a)(i) — solution

  1. 1D array with name DataArray (with 25 elements of type Integer)

Visual Basic (VB.NET)

Dim DataArray(24) As Integer

Python

DataArray = [] #25 elements Integer

 2023                                                     Page 4 of 38

9618/43                                      Cambridge International AS & A Level – Mark Scheme   May/June 2023

Mark points

  • 1D array with name DataArray (with 25 elements of type Integer)

Part 1(a)(ii) — solution

  1. Opening file Data.txt to read
  2. Looping through all the 25/EOF …
  3. … reading each line and storing/appending into array
  4. Exception handling with appropriate output
  5. Closing the file (in an appropriate place)

Visual Basic (VB.NET)

try
                 Dim DataReader As New System.IO.StreamReader("Data.txt")
                 Dim X As Integer = 0
                 Do Until DataReader.EndOfStream
                    DataArray(X) = DataReader.ReadLine()
                    X = X + 1
                 Loop
                 DataReader.Close()
               Catch ex As Exception
                 Console.WriteLine("Invalid file")
               End Try

 2023                                                      Page 5 of 38

9618/43                                Cambridge International AS & A Level – Mark Scheme   May/June 2023

Mark points

  • Opening file Data.txt to read
  • Looping through all the 25/EOF …
  • … reading each line and storing/appending into array
  • Exception handling with appropriate output
  • Closing the file (in an appropriate place)

Part 1(a)(ii) — solution

  1. DataFile = open("Data.txt",'r')

Python

try:
                 DataFile = open("Data.txt",'r')
                 for Line in DataFile:
                    DataArray.append(int(Line))
                 DataFile.close()
               except IOError:
                 print("Could not find file")

 2023                                               Page 6 of 38

9618/43                                       Cambridge International AS & A Level – Mark Scheme                May/June 2023

Mark points

  • DataFile = open("Data.txt",'r')

Part 1(b)(i) — solution

  1. Procedure header (and close where appropriate) with (at least) one (integer array) parameter
  2. Outputting all (25) array elements …
  3. …on one line

Visual Basic (VB.NET)

Sub PrintArray(DataArray)
                    Dim OutputData As String = "";
                    For x = 0 To DataArray.length - 1
                         OutputData = OutputData & DataArray(x) & " "
                    Next
                    Console.WriteLine(OutputData)
               End Sub

Python

def PrintArray(DataArray):
                   output = ""
                   for X in range(0, len(DataArray)):
                       output = output + str((DataArray[X])) + " "
                   print(output)

 2023                                                        Page 7 of 38

9618/43                                       Cambridge International AS & A Level – Mark Scheme   May/June 2023

Mark points

  • Procedure header (and close where appropriate) with (at least) one (integer array) parameter
  • Outputting all (25) array elements …
  • …on one line

Part 1(b)(ii) — solution

1 mark for calling PrintArray with the array as a parameter 1 Example program code: VB.NET PrintArray(DataArray) Python PrintArray(DataArray)

Visual Basic (VB.NET)

PrintArray(DataArray)

Python

PrintArray(DataArray)

Mark points

  • Include the ideas shown in the solution above.

Part 1(b)(iii) — solution

1 mark for screenshot 1 e.g. 2023 Page 8 of 38 9618/43 Cambridge International AS & A Level – Mark Scheme May/June 2023

Mark points

  • Include the ideas shown in the solution above.

Part 1(c) — solution

  1. Function header (and close where appropriate) taking array and search value as parameters
  2. Looping through each array element and keeping count of the number of times the parameter appears
  3. Returning the calculated count value

Visual Basic (VB.NET)

Function LinearSearch(DataArray, DataToFind)
                   Dim Count As Integer = 0
                   For x = 0 To DataArray.length - 1
                        If DataArray(x) = DataToFind Then
                            Count = Count + 1
                        End If
                   Next
                   Return Count
               End Function

Python

def LinearSearch(DataArray, DataToFind):
                   Count = 0
                   for X in range(0, len(DataArray)):
                       if(DataArray[X] == DataToFind):
                           Count +=1
                   return Count

 2023                                                      Page 9 of 38

9618/43                                      Cambridge International AS & A Level – Mark Scheme          May/June 2023

Mark points

  • Function header (and close where appropriate) taking array and search value as parameters
  • Looping through each array element and keeping count of the number of times the parameter appears
  • Returning the calculated count value

Part 1(d)(i) — solution

  1. Prompt and reading input …
  2. …with validation for whole number between 0 and 100 inclusive
  3. Calling LinearSearch() with array and valid data input and storing/using return value
  4. Output of the message with return value

Visual Basic (VB.NET)

Console.WriteLine("Enter a number to find ")
               Dim DataToFind As Integer = -1
               Do Until DataToFind >= 0 And DataToFind <= 100
                    DataToFind = Console.ReadLine()
               Loop
               Dim NumberTimes = LinearSearch(DataArray, DataToFind)
               Console.WriteLine("The number " & DataToFind & " is found " & NumberTimes & " times.")

Python

DataToFind = int(input("Enter a number to find "))
               while DataToFind < 0 or DataToFind > 100:
                   DataToFind = int(input("Enter a number to find "))
               NumberTimes = LinearSearch(DataArray, DataToFind)
               print("The number", DataToFind, "is found", NumberTimes, "times")

 2023                                                      Page 10 of 38

9618/43                                     Cambridge International AS & A Level – Mark Scheme   May/June 2023

Mark points

  • Prompt and reading input …
  • …with validation for whole number between 0 and 100 inclusive
  • Calling LinearSearch() with array and valid data input and storing/using return value
  • Output of the message with return value

Part 1(d)(ii) — solution

1 mark for screenshot e.g. 1 2023 Page 11 of 38 9618/43 Cambridge International AS & A Level – Mark Scheme May/June 2023

Mark points

  • Include the ideas shown in the solution above.

Question 2

Paper page 4 for Q2
Original paper page 4

Part 2(a)(i) — solution

  1. Class header (and close where appropriate)
  2. 5 (private) attribute declarations including data types
  3. Constructor header (and close where appropriate) taking 3 parameters (min)
  4. Assigning ID, MaxSpeed and IncreaseAmount to parameters
  5. Assigning CurrentSpeed and HorizontalPosition to 0

Visual Basic (VB.NET)

Class Vehicle
                       Private ID As String
                       Private MaxSpeed As Integer
                       Private CurrentSpeed As Integer
                       Private IncreaseAmount As Integer
                       Private HorizontalPosition As Integer

                       Sub New(IDP, MaxSpeedP, IncreaseAmountP)
                           ID = IDP
                           MaxSpeed = MaxSpeedP
                           CurrentSpeed = 0
                           IncreaseAmount = IncreaseAmountP
                           HorizontalPosition = 0
                       End Sub
               End Class

Mark points

  • Class header (and close where appropriate)
  • 5 (private) attribute declarations including data types
  • Constructor header (and close where appropriate) taking 3 parameters (min)
  • Assigning ID, MaxSpeed and IncreaseAmount to parameters
  • Assigning CurrentSpeed and HorizontalPosition to 0

Part 2(a)(i) — solution

  1. ID = IDP;
  2. MaxSpeed = MaxSpeedP;
  3. IncreaseAmount = IncreaseAmountP;
  4. CurrentSpeed = 0;
  5. HorizontalPosition = 0;
  6. self.__ID = IDP
  7. self.__MaxSpeed = MaxSpeedP
  8. self.__IncreaseAmount = IncreaseAmountP
  9. self.__CurrentSpeed = 0
  10. self.__HorizontalPosition = 0

Python

class Vehicle:
                   #self.__ID string
                   #self.__MaxSpeed integer
                   #self.__CurrentSpeed integer
                   #self.__IncreaseAmount integer
                   #self.__HorizontalPosition

                   def __init__(self, IDP, MaxSpeedP, IncreaseAmountP):
                       self.__ID = IDP
                       self.__MaxSpeed = MaxSpeedP
                       self.__IncreaseAmount = IncreaseAmountP
                       self.__CurrentSpeed = 0
                       self.__HorizontalPosition = 0

 2023                                               Page 13 of 38

9618/43                                      Cambridge International AS & A Level – Mark Scheme   May/June 2023

Mark points

  • ID = IDP;
  • MaxSpeed = MaxSpeedP;
  • IncreaseAmount = IncreaseAmountP;
  • CurrentSpeed = 0;
  • HorizontalPosition = 0;
  • self.__ID = IDP
  • self.__MaxSpeed = MaxSpeedP
  • self.__IncreaseAmount = IncreaseAmountP
  • self.__CurrentSpeed = 0
  • self.__HorizontalPosition = 0

Part 2(a)(ii) — solution

  1. 1 get function header (and end where appropriate) with no parameter …
  2. …returning attribute (without overwriting)
  3. 3 further correct get methods

Visual Basic (VB.NET)

Function GetCurrentSpeed()
                   Return CurrentSpeed
               End Function
               Function GetIncreaseAmount()
                   Return IncreaseAmount
               End Function
               Function GetHorizontalPosition()
                   Return HorizontalPosition
               End Function
               Function GetMaxSpeed()
                   Return MaxSpeed
               End Function

Mark points

  • 1 get function header (and end where appropriate) with no parameter …
  • …returning attribute (without overwriting)
  • 3 further correct get methods

Part 2(a)(ii) — solution

Python def GetCurrentSpeed(self): return self.__CurrentSpeed def GetIncreaseAmount(self): return self.__IncreaseAmount def GetHorizontalPosition(self): return self.__HorizontalPosition def GetMaxSpeed(self): return self.__MaxSpeed 2023 Page 15 of 38 9618/43 Cambridge International AS & A Level – Mark Scheme May/June 2023

Python

def GetCurrentSpeed(self):
                   return self.__CurrentSpeed
               def GetIncreaseAmount(self):
                   return self.__IncreaseAmount
               def GetHorizontalPosition(self):
                   return self.__HorizontalPosition
               def GetMaxSpeed(self):
                   return self.__MaxSpeed

 2023                                               Page 15 of 38

9618/43                                      Cambridge International AS & A Level – Mark Scheme   May/June 2023

Mark points

  • Include the ideas shown in the solution above.

Part 2(a)(iii) — solution

  1. 1 set procedure (and end where appropriate) taking parameter …
  2. … assigns parameter to the attribute (without overriding)
  3. Second correct set method

Visual Basic (VB.NET)

Sub SetCurrentSpeed(CSp)
                   CurrentSpeed = CSp
               End Sub
               Sub SetHorizontalPosition(HPP)
                   HorizontalPosition = HPP
               End Sub

Python

def SetCurrentSpeed(self, CSP):
                   self.__CurrentSpeed = CSP
               def SetHorizontalPosition(self, HPP):
                   self.__HorizontalPosition = HPP

 2023                                                     Page 16 of 38

9618/43                                    Cambridge International AS & A Level – Mark Scheme                         May/June 2023

Mark points

  • 1 set procedure (and end where appropriate) taking parameter …
  • … assigns parameter to the attribute (without overriding)
  • Second correct set method

Part 2(a)(iv) — solution

  1. Method header (and close where appropriate) with no parameter and adding IncreaseAmount to CurrentSpeed
  2. Checking if MaxSpeed is exceeded and limiting to max speed (remove increase or assign maximum)
  3. Adding updated CurrentSpeed to HorizontalPosition in all cases (whether MaxSpeed is exceeded or not)

Visual Basic (VB.NET)

Sub IncreaseSpeed()
                   CurrentSpeed = CurrentSpeed + IncreaseAmount
                   If CurrentSpeed > MaxSpeed Then
                       CurrentSpeed = MaxSpeed
                   End If
                   HorizontalPosition = HorizontalPosition + CurrentSpeed
               End Sub

Python

def IncreaseSpeed(self):
                   self.__CurrentSpeed = self.__CurrentSpeed + self.__IncreaseAmount
                   if(self.__CurrentSpeed > self.__MaxSpeed):
                        self.__CurrentSpeed = self.__MaxSpeed
                   self.__HorizontalPosition = self.__HorizontalPosition + self.__CurrentSpeed

 2023                                                   Page 17 of 38

9618/43                                    Cambridge International AS & A Level – Mark Scheme          May/June 2023

Mark points

  • Method header (and close where appropriate) with no parameter and adding IncreaseAmount to CurrentSpeed
  • Checking if MaxSpeed is exceeded and limiting to max speed (remove increase or assign maximum)
  • Adding updated CurrentSpeed to HorizontalPosition in all cases (whether MaxSpeed is exceeded or not)

Part 2(b)(i) — solution

  1. Class header (and end where appropriate) inheriting from Vehicle
  2. 3 (private) attribute declarations with data types
  3. Constructor (and end where appropriate) with (min) 5 parameters
  4. Calling parent constructor with appropriate parameters
  5. Initialising VerticalPosition to 0 and VerticalChange and MaxHeight to attributes

Visual Basic (VB.NET)

Class Helicopter
                       Inherits Vehicle
                       Private VerticalPosition As Integer
                       Private VerticalChange As Integer
                       Private MaxHeight As Integer

                        Sub New(IDP, MaxSpeedP, IncreaseAmountP, VertChangeP, MaxHeightP)
                            MyBase.New(IDP, MaxSpeedP, IncreaseAmountP)
                            VerticalPosition = 0
                            VerticalChange = VertChangeP
                            MaxHeight = MaxHeightP

                       End Sub
               End Class

Mark points

  • Class header (and end where appropriate) inheriting from Vehicle
  • 3 (private) attribute declarations with data types
  • Constructor (and end where appropriate) with (min) 5 parameters
  • Calling parent constructor with appropriate parameters
  • Initialising VerticalPosition to 0 and VerticalChange and MaxHeight to attributes

Part 2(b)(i) — solution

  1. VerticalPosition = 0;
  2. VerticalChange = VertChangeP;
  3. MaxHeight = MaxHeightP;
  4. self.__VerticalPosition = 0
  5. self.__VerticalChange = VertChangeP
  6. self.__MaxHeight = MaxHeightP

Python

class Helicopter(Vehicle):
                   #VerticalPosition Integer
                   #VerticalChange Integer
                   #MaxHeight Integer

                   def __init__(self, IDP, MaxSpeedP, IncreaseAmountP, VertChangeP, MaxHeightP):
                        Vehicle.__init__(self,IDP, MaxSpeedP, IncreaseAmountP)
                        self.__VerticalPosition = 0
                        self.__VerticalChange = VertChangeP
                        self.__MaxHeight = MaxHeightP

 2023                                               Page 19 of 38

9618/43                                        Cambridge International AS & A Level – Mark Scheme         May/June 2023

Mark points

  • VerticalPosition = 0;
  • VerticalChange = VertChangeP;
  • MaxHeight = MaxHeightP;
  • self.__VerticalPosition = 0
  • self.__VerticalChange = VertChangeP
  • self.__MaxHeight = MaxHeightP

Part 2(b)(ii) — solution

  1. Method header (overriding where required) with no parameter
  2. Adding vertical change to vertical position …
  3. …limiting to maximum height
  4. Repeating/calling/using the code from original for horizontal increase (in every case)

Visual Basic (VB.NET)

Overrides Sub IncreaseSpeed()
                   VerticalPosition = VerticalPosition + VerticalChange
                   If VerticalPosition > MaxHeight Then
                       VerticalPosition = MaxHeight
                   End If
                   Me.SetCurrentSpeed(GetCurrentSpeed() + GetIncreaseAmount())
                   If Me.GetCurrentSpeed() > Me.GetMaxSpeed() Then
                       Me.SetCurrentSpeed(Me.GetMaxSpeed())
                   End If
                   Me.SetHorizontalPosition(Me.GetHorizontalPosition() + Me.GetCurrentSpeed())
               End Sub

Mark points

  • Method header (overriding where required) with no parameter
  • Adding vertical change to vertical position …
  • …limiting to maximum height
  • Repeating/calling/using the code from original for horizontal increase (in every case)

Part 2(b)(ii) — solution

  1. self.__VerticalPosition = self.__VerticalPosition + self.__VerticalChange
  2. self.__VerticalPosition = MaxHeight

Python

def IncreaseSpeed(self):
                   self.__VerticalPosition = self.__VerticalPosition + self.__VerticalChange
                   if(self.__VerticalPosition > self.__MaxHeight):
                       self.__VerticalPosition = MaxHeight
                   Vehicle.SetCurrentSpeed(self, Vehicle.GetCurrentSpeed(self) +
               Vehicle.GetIncreaseAmount(self))
                   if(Vehicle.GetCurrentSpeed(self) > Vehicle.GetMaxSpeed(self)):
                       Vehicle.SetCurrentSpeed(self, Vehicle.GetMaxSpeed(self));
                   Vehicle.SetHorizontalPosition(self, Vehicle.GetHorizontalPosition(self) +
               Vehicle.GetCurrentSpeed(self))

 2023                                               Page 21 of 38

9618/43                                        Cambridge International AS & A Level – Mark Scheme                                   May/June 2023

Mark points

  • self.__VerticalPosition = self.__VerticalPosition + self.__VerticalChange
  • self.__VerticalPosition = MaxHeight

Part 2(c) — solution

  1. Suitable method/procedure heading (and end where appropriate) and outputting horizontal position and current speed
  2. Checking if object is a Vehicle or Helicopter // overriding methods in each class for output // one method in each class
  3. …outputting vertical position only if helicopter with appropriate message

Visual Basic (VB.NET)

Sub OutputCurrentPosition(ObjectToOutput)
                    Console.WriteLine("Current position = " & ObjectToOutput.GetHorizontalPosition())
                    Console.WriteLine("Current speed = " & ObjectToOutput.GetCurrentSpeed())
                    If TypeOf ObjectToOutput Is Helicopter Then
                        Console.WriteLine("Current vertical position = " &
               ObjectToOutput.GetVerticalPosition())
                    End If
               End Sub

Python

def OutputCurrentPosition(self):
                   print("Current position = ", self.__HorizontalPosition)
                   print("Current speed = ", self.__CurrentSpeed)

 2023                                                         Page 22 of 38

9618/43                                Cambridge International AS & A Level – Mark Scheme   May/June 2023

Mark points

  • Suitable method/procedure heading (and end where appropriate) and outputting horizontal position and current speed
  • Checking if object is a Vehicle or Helicopter // overriding methods in each class for output // one method in each class
  • …outputting vertical position only if helicopter with appropriate message

Part 2(c) — solution

  1. print("Current position = ", Vehicle.GetHorizontalPosition(self))
  2. print("Current speed = ", Vehicle.GetCurrentSpeed(self))
  3. print("Current verticalposition = ", self.__VerticalPosition)

Mark points

  • print("Current position = ", Vehicle.GetHorizontalPosition(self))
  • print("Current speed = ", Vehicle.GetCurrentSpeed(self))
  • print("Current verticalposition = ", self.__VerticalPosition)

Part 2(d)(i) — solution

  1. Instantiating an object of type Vehicle with correct parameters ("Tiger", 100, 20)
  2. Instantiating an object of type Helicopter with correct parameters ("Lion", 350, 40, 3, 100)
  3. Calling IncreaseSpeed() twice for the car
  4. Calling IncreaseSpeed() twice for the helicopter
  5. Calling the output for both objects

Visual Basic (VB.NET)

Sub Main()
                   Dim Car As Vehicle
                   Car = New Vehicle("Tiger", 100, 20)
                   Dim Heli1 As Helicopter
                   Heli1 = New Helicopter("Lion", 350, 40, 3, 100)
                   Car.IncreaseSpeed()
                   Car.IncreaseSpeed()
                   OutputCurrentPosition(Car)
                   Console.WriteLine("")
                   Heli1.IncreaseSpeed()
                   Heli1.IncreaseSpeed()
                   OutputCurrentPosition(Heli1)
               End Sub

Mark points

  • Instantiating an object of type Vehicle with correct parameters ("Tiger", 100, 20)
  • Instantiating an object of type Helicopter with correct parameters ("Lion", 350, 40, 3, 100)
  • Calling IncreaseSpeed() twice for the car
  • Calling IncreaseSpeed() twice for the helicopter
  • Calling the output for both objects

Part 2(d)(i) — solution

  1. Car = Vehicle("Tiger", 100, 20)
  2. Heli1 = Helicopter("Lion", 350, 40, 3, 100)

Python

#main
               Car = Vehicle("Tiger", 100, 20)
               Heli1 = Helicopter("Lion", 350, 40, 3, 100)
               Car.IncreaseSpeed()
               Car.IncreaseSpeed()
               Car.OutputCurrentPosition()
               print("")
               Heli1.IncreaseSpeed()
               Heli1.IncreaseSpeed()
               Heli1.OutputCurrentPosition()

Mark points

  • Car = Vehicle("Tiger", 100, 20)
  • Heli1 = Helicopter("Lion", 350, 40, 3, 100)

Part 2(d)(ii) — solution

Screenshot of results e.g. 1 2023 Page 25 of 38 9618/43 Cambridge International AS & A Level – Mark Scheme May/June 2023

Mark points

  • Include the ideas shown in the solution above.

Question 3

Paper page 8 for Q3
Original paper page 8

Part 3(a) — solution

  1. (Global) Animal array (with 20 string elements)
  2. (Global) Colour array (with 10 string elements)
  3. (Global) AnimalTopPointer and ColourTopPointer initialised to 0

Visual Basic (VB.NET)

Dim Animal(0 to 19) As String
               Dim Colour(0 to 9) As String
               Dim AnimalTopPointer As Integer = 0
               Dim ColourTopPointer As Integer = 0

Python

Animal = [] #20 elements
               Colour = [] #10 elements
               global AnimalTopPointer
               global ColourTopPointer
               AnimalTopPointer = 0
               ColourTopPointer = 0

 2023                                                   Page 26 of 38

9618/43                                       Cambridge International AS & A Level – Mark Scheme                                 May/June 2023

Mark points

  • (Global) Animal array (with 20 string elements)
  • (Global) Colour array (with 10 string elements)
  • (Global) AnimalTopPointer and ColourTopPointer initialised to 0

Part 3(b)(i) — solution

  1. Function header (and close where appropriate) with parameter, checking if full (AnimalTopPointer = 20) and returning
  2. If not full, inserting parameter value into AnimalTopPointer
  3. …incrementing pointer and returning true

Visual Basic (VB.NET)

Function PushAnimal(DataToPush)
                  If AnimalTopPointer = 20 Then
                       Return False
                  Else
                       Animal(AnimalTopPointer) = DataToPush
                       AnimalTopPointer = AnimalTopPointer + 1
                       Return True
                  End If
               End Function

Python

def PushAnimal(DataToPush):
                   global AnimalTopPointer
                   global ColourTopPointer
                   if AnimalTopPointer == 20:
                       return False

 2023                                                        Page 27 of 38

9618/43                                      Cambridge International AS & A Level – Mark Scheme                                 May/June 2023

Mark points

  • Function header (and close where appropriate) with parameter, checking if full (AnimalTopPointer = 20) and returning
  • If not full, inserting parameter value into AnimalTopPointer
  • …incrementing pointer and returning true

Part 3(b)(i) — solution

  1. AnimalTopPointer +=1

Mark points

  • AnimalTopPointer +=1

Part 3(b)(ii) — solution

  1. Procedure header (and end where appropriate) with no parameter, checking if empty (AnimalTopPointer = 0) and
  2. If not empty returning the top data item (AnimalTopPointer-1)
  3. … and decrementing AnimalTopPointer

Visual Basic (VB.NET)

Function PopAnimal()
                   Dim ReturnData As String
                   If AnimalTopPointer = 0 Then
                        Return ""
                   Else
                        ReturnData = Animal(AnimalTopPointer - 1)
                        AnimalTopPointer = AnimalTopPointer - 1
                        Return ReturnData
                   End If
               End Function

 2023                                                      Page 28 of 38

9618/43                                Cambridge International AS & A Level – Mark Scheme   May/June 2023

Mark points

  • Procedure header (and end where appropriate) with no parameter, checking if empty (AnimalTopPointer = 0) and
  • If not empty returning the top data item (AnimalTopPointer-1)
  • … and decrementing AnimalTopPointer

Part 3(b)(ii) — solution

  1. if AnimalTopPointer == 0:
  2. ReturnData = Animal[AnimalTopPointer - 1]
  3. AnimalTopPointer -=1

Python

def PopAnimal():
                   global AnimalTopPointer
                   global ColourTopPointer
                   if AnimalTopPointer == 0:
                       return ""
                   else:
                       ReturnData = Animal[AnimalTopPointer - 1]
                       AnimalTopPointer -=1
                       return ReturnData

 2023                                               Page 29 of 38

9618/43                                         Cambridge International AS & A Level – Mark Scheme        May/June 2023

Mark points

  • if AnimalTopPointer == 0:
  • ReturnData = Animal[AnimalTopPointer - 1]
  • AnimalTopPointer -=1

Part 3(b)(iii) — solution

  1. Procedure header (and close where appropriate) and opening correct file for read
  2. Looping until end of file // looping until all animal names read in // looping 8 times
  3. Calling PushAnimal() with each line read from file (for all lines)
  4. Closing the file
  5. Exception handling with appropriate error message

Visual Basic (VB.NET)

Sub ReadData()
                 try
                   Dim AnimalFile As String = "AnimalData.txt"
                   Dim AnimalFileReader As New System.IO.StreamReader(AnimalFile)
                   Do Until AnimalFileReader.EndOfStream
                         PushAnimal(AnimalFileReader.ReadLine())
                   Loop
                   AnimalFileReader.Close()
                 Catch ex As Exception
                   Console.WriteLine("Invalid file")
                 End Try

               End Sub

 2023                                                           Page 30 of 38

9618/43                                Cambridge International AS & A Level – Mark Scheme   May/June 2023

Mark points

  • Procedure header (and close where appropriate) and opening correct file for read
  • Looping until end of file // looping until all animal names read in // looping 8 times
  • Calling PushAnimal() with each line read from file (for all lines)
  • Closing the file
  • Exception handling with appropriate error message

Part 3(b)(iii) — solution

  1. AnimalFile = open("AnimalData.txt", 'r')

Python

def ReadData():
                 try:
                   global AnimalTopPointer
                   global ColourTopPointer
                   AnimalFile = open("AnimalData.txt", 'r')
                   for Line in AnimalFile:
                       PushAnimal(Line)
                   AnimalFile.close()
                 except IOError:
                   print("Could not find file")

 2023                                               Page 31 of 38

9618/43                                Cambridge International AS & A Level – Mark Scheme   May/June 2023

Mark points

  • AnimalFile = open("AnimalData.txt", 'r')

Part 3(b)(iv) — solution

  1. PushColour function
  2. PopColour function

Visual Basic (VB.NET)

Function PushColour(DataToPush)
                  If ColourTopPointer = 10 Then
                     Return False
                  Else
                     Colour(ColourTopPointer) = DataToPush
                     ColourTopPointer = ColourTopPointer + 1
                     Return True
                  End If

 2023                                               Page 32 of 38

9618/43                                Cambridge International AS & A Level – Mark Scheme   May/June 2023

Mark points

  • PushColour function
  • PopColour function

Part 3(b)(iv) — solution

  1. If ColourTopPointer = 0 Then
  2. ReturnData = Colour(ColourTopPointer - 1)
  3. ColourTopPointer = ColourTopPointer - 1
  4. if ColourTopPointer == 10:
  5. ColourTopPointer +=1
  6. if ColourTopPointer == 0:
  7. ReturnData = Colour[ColourTopPointer - 1]
  8. ColourTopPointer -=1

Python

def PushColour(DataToPush):
                   global AnimalTopPointer
                   global ColourTopPointer
                   if ColourTopPointer == 10:
                       return False
                   else:
                       Colour.append(DataToPush)
                       ColourTopPointer +=1
                       return True

               def PopColour():
                   global AnimalTopPointer
                   global ColourTopPointer
                   if ColourTopPointer == 0:
                       return ""
                   else:
                       ReturnData = Colour[ColourTopPointer - 1]
                       ColourTopPointer -=1
                       return ReturnData

 2023                                               Page 33 of 38

9618/43                                      Cambridge International AS & A Level – Mark Scheme           May/June 2023

Mark points

  • If ColourTopPointer = 0 Then
  • ReturnData = Colour(ColourTopPointer - 1)
  • ColourTopPointer = ColourTopPointer - 1
  • if ColourTopPointer == 10:
  • ColourTopPointer +=1
  • if ColourTopPointer == 0:
  • ReturnData = Colour[ColourTopPointer - 1]
  • ColourTopPointer -=1

Part 3(b)(v) — solution

  1. Opening ColourData.txt to read, reading until EOF, closing file and exception handling
  2. Using PushColour() to store each item read from the file for all lines

Visual Basic (VB.NET)

Sub ReadData()
                 try
                   Dim AnimalFile As String = "AnimalData.txt"
                   Dim AnimalFileReader As New System.IO.StreamReader(AnimalFile)
                   Do Until AnimalFileReader.EndOfStream
                       PushAnimal(AnimalFileReader.ReadLine())

 2023                                                      Page 34 of 38

9618/43                                Cambridge International AS & A Level – Mark Scheme   May/June 2023

Mark points

  • Opening ColourData.txt to read, reading until EOF, closing file and exception handling
  • Using PushColour() to store each item read from the file for all lines

Part 3(b)(v) — solution

  1. Dim ColourFile As String = "ColourData.txt"
  2. AnimalFile = open("AnimalData.txt", 'r')
  3. ColourFile = open("ColourData.txt", 'r')

Python

def ReadData():
                 try:
                   global AnimalTopPointer
                   global ColourTopPointer
                   AnimalFile = open("AnimalData.txt", 'r')
                   for Line in AnimalFile:
                       PushAnimal(Line)
                   AnimalFile.close()

                   ColourFile = open("ColourData.txt", 'r')
                   for Line in ColourFile:
                       PushColour(Line)
                   ColourFile.close()
                 except IOError:
                   print("Could not find file")

 2023                                               Page 35 of 38

9618/43                                     Cambridge International AS & A Level – Mark Scheme                            May/June 2023

Mark points

  • Dim ColourFile As String = "ColourData.txt"
  • AnimalFile = open("AnimalData.txt", 'r')
  • ColourFile = open("ColourData.txt", 'r')

Part 3(c) — solution

  1. Procedure heading (and close where appropriate) and outputting the colour and animal using PopColour() and
  2. Checking if no colour and outputting "No colour" …
  3. ….pushing the removed animal back onto the stack
  4. Checking if no animal and outputting "No animal" …
  5. …pushing the removed colour back onto the stack

Visual Basic (VB.NET)

Sub OutputItem()
                  Dim ColourReturned As String = PopColour()
                  Dim Animalreturned As String = PopAnimal()
                  If ColourReturned = "" Then
                      Console.WriteLine("No colour")
                      PushAnimal(AnimalReturned)

 2023                                                     Page 36 of 38

9618/43                                Cambridge International AS & A Level – Mark Scheme   May/June 2023

Mark points

  • Procedure heading (and close where appropriate) and outputting the colour and animal using PopColour() and
  • Checking if no colour and outputting "No colour" …
  • ….pushing the removed animal back onto the stack
  • Checking if no animal and outputting "No animal" …
  • …pushing the removed colour back onto the stack

Part 3(c) — solution

  1. If Animalreturned = "" Then
  2. ColourReturned = PopColour()
  3. AnimalReturned = PopAnimal()
  4. if ColourReturned == "":
  5. if AnimalReturned == "":

Python

def OutputItem():
                   global AnimalTopPointer
                   global ColourTopPointer
                   ColourReturned = PopColour()
                   AnimalReturned = PopAnimal()
                   if ColourReturned == "":
                       print("No colour")
                       PushAnimal(AnimalReturned)
                   else:
                       if AnimalReturned == "":
                           print("No animal")
                           PushColour(ColourReturned)
                       else:
                           print(ColourReturned, AnimalReturned)

 2023                                               Page 37 of 38

9618/43                                    Cambridge International AS & A Level – Mark Scheme   May/June 2023

Mark points

  • If Animalreturned = "" Then
  • ColourReturned = PopColour()
  • AnimalReturned = PopAnimal()
  • if ColourReturned == "":
  • if AnimalReturned == "":

Part 3(d)(i) — solution

  1. Calling ReadData() and calling OutputItem() 4 times

Visual Basic (VB.NET)

Sub Main()
                   ReadData()
                   OutputItem()
                   OutputItem()
                   OutputItem()
                   OutputItem()
               End Sub

Python

ReadData()
               OutputItem()
               OutputItem()
               OutputItem()
               OutputItem()

Mark points

  • Calling ReadData() and calling OutputItem() 4 times

Part 3(d)(ii) — solution

1 mark for output 1 e.g. 2023 Page 38 of 38

Mark points

  • Include the ideas shown in the solution above.