Cambridge International AS & A Level: Computer Science 9618/42 May/June 2022
Cambridge International AS & A Level: Computer Science 9618/42 May/June 2022
Published
This mark scheme is published as an aid to teachers and candidates, to indicate the requirements of the
examination. It shows the basis on which Examiners were instructed to award marks. It does not indicate the
details of the discussions that took place at an Examiners’ meeting before marking began, which would have
considered the acceptability of alternative answers.
Mark schemes should be read in conjunction with the question paper and the Principal Examiner Report for
Teachers.
Cambridge International will not enter into discussions about these mark schemes.
Cambridge International is publishing the mark schemes for the May/June 2022 series for most
Cambridge IGCSE, Cambridge International A and AS Level and Cambridge Pre-U components, and some
Cambridge O Level components.
These general marking principles must be applied by all examiners when marking candidate answers. They should be applied alongside the
specific content of the mark scheme or generic level descriptors for a question. Each question paper and mark scheme will also comply with these
marking principles.
the specific content of the mark scheme or the generic level descriptors for the question
the specific skills defined in the mark scheme or in the generic level descriptors for the question
the standard of response required by a candidate as exemplified by the standardisation scripts.
Marks awarded are always whole marks (not half marks, or other fractions).
marks are awarded for correct/valid answers, as defined in the mark scheme. However, credit is given for valid answers which go beyond the
scope of the syllabus and mark scheme, referring to your Team Leader as appropriate
marks are awarded when candidates clearly demonstrate what they know and can do
marks are not deducted for errors
marks are not deducted for omissions
answers should only be judged on the quality of spelling, punctuation and grammar when these features are specifically assessed by the
question as indicated by the mark scheme. The meaning, however, should be unambiguous.
Rules must be applied consistently, e.g. in situations where candidates have not followed instructions or in the application of generic level
descriptors.
Marks should be awarded using the full range of marks defined in the mark scheme for the question (however; the use of the full mark range may
be limited according to the quality of the candidate responses seen).
Marks awarded are based solely on the requirements as defined in the mark scheme. Marks should not be awarded with grade thresholds or
grade descriptors in mind.
Python
global StackData #integer
global StackPointer
StackData = [0,0,0,0,0,0,0,0,0,0] #integer
StackPointer = 0
Java
import java.util.Scanner;
class Question1{
public static Integer[] StackData;
public static Integer StackPointer;
public static void main(String args[]){
StackData = new Integer[10];
StackPointer = 0;
}
}
Python
def PrintArray():
global StackData
global StackPointer
print(StackPointer)
for x in range (0, 10):
print(StackData[x])
Java
public static void PrintArray(){
System.out.println(StackPointer);
for(int x = 0; x < 10 ;x++){
System.out.println(StackData[x]);
}
}
Python
def Push(DataToPush):
global StackData
global StackPointer
if StackPointer == 10:
return False
else:
StackData[StackPointer] = DataToPush
StackPointer = StackPointer + 1
return True
1(c) Java
public static Boolean Push(Integer DataToPush){
if(StackPointer == 10){
return false;
}else{
StackData[StackPointer] = DataToPush;
StackPointer = StackPointer + 1;
return true;
}
}
If Push(TempNumber) Then
Console.WriteLine("Stored")
Else
Console.WriteLine("Stack full")
End If
Next
PrintArray()
Console.ReadLine()
End Sub
1(d)(i) Python
#main
StackPointer = 0
StackData = [0,0,0,0,0,0,0,0,0,0]
for x in range(0, 11):
TempNumber = int(input("Enter a number"))
if Push(TempNumber) == True:
print("Stored")
else:
print("Stack full")
PrintArray()
Java
public static void main(String[] args){
StackData = new Integer[10];
StackPointer = 0;
Integer TempNumber = 0;
for(int x = 0; x < 10; x++){
System.out.println("Enter a number");
Scanner scanner = new Scanner(System.in);
TempNumber = Integer.parseInt(scanner.nextLine());
if(Push(TempNumber)){
System.out.println("Stored");
}else{
System.out.println("Stack full");
}
}
PrintArray();
}
1(d)(ii) 1 mark for inputting all 11 numbers, message for first 10 saying added (11 to 20), message stating 11th number stating 1
stack full. Full array contents output (11 12 13 14 15 16 17 18 19 20).
e.g.
Python
def Pop():
global StackData
global StackPointer
if StackPointer == 0:
return -1
else:
ReturnData = StackData[StackPointer - 1]
StackPointer = StackPointer - 1
return ReturnData
1(e)(i) Java
public static Integer Pop(){
Integer ReturnData = 0;
if(StackPointer == 0){
return -1;
}else{
ReturnData = StackData[StackPointer - 1];
StackPointer = StackPointer - 1;
return ReturnData;
}
}
Python
import random
#main
ArrayData= [[0]*10 for i in range(10)] #integer
for x in range(0, 10):
for y in range(0,10):
ArrayData[x][y] = random.randint(1, 100)
2(a) Java
import java.util.Scanner;
import java.util.Random;
class Question2{
public static Integer[][] ArrayData;
public static void main(String args[]){
Random Rand = new Random();
ArrayData = new Integer[10][10];
for(int x=0; x < 10; x++){
for(int y = 0; y < 10; y++){
ArrayData[x][y] = Rand.nextInt(100);}
}
}
}
Selection statement …
…swapping the numbers correctly
Python
ArrayLength = 10
for X in range(0, ArrayLength):
for Y in range(0, ArrayLength-1):
for Z in range(0, ArrayLength - Y - 1):
if(ArrayData[X][Z] > ArrayData[X][Z+1]):
TempNumber = ArrayData[X][Z]
ArrayData[X][Z] = ArrayData[X][Z+1]
ArrayData[X][Z+1] = TempNumber
Accept for MP5:
ArrayData[X][Z], ArrayData[X][Z+1] = ArrayData[X][Z+1], ArrayData[X][Z]
2(b)(i) Java
Integer ArrayLength = 10;
for(int X = 0; X < ArrayLength; X++){
for(int Y = 0; Y < ArrayLength; Y++){
for(int Z = 0; Z < ArrayLength - Y - 1; Z++){
if(ArrayData[X][Z] > ArrayData[X][Z + 1]){
TempNumber = ArrayData[X][Z];
ArrayData[X][Z] = ArrayData[X][Z+1];
ArrayData[X][Z + 1] = TempNumber;
}
}
}
}
Console.WriteLine("after")
printarray(ArrayData)
Console.ReadLine()
End Sub
Python
import random
def Printarray(ArrayData):
for x in range(0, 10):
for y in range(0, 10):
print(ArrayData[x][y], " ", end='')
print("")
#main
ArrayData= [[0]*10 for i in range(10)] #integer
for x in range(0, 10):
for y in range(0,10):
ArrayData[x][y] = random.randint(1, 100)
print("Before")
printarray(ArrayData)
ArrayLength = 10
for X in range(0, ArrayLength):
for Y in range(0, ArrayLength):
for Z in range(0, ArrayLength - Y - 1):
if(ArrayData[X][Z] > ArrayData[X][Z+1]):
TempNumber = ArrayData[X][Z]
ArrayData[X][Z] = ArrayData[X][Z+1]
ArrayData[X][Z+1] = TempNumber
print("After")
Printarray(ArrayData)
2(b)(ii) Java
import java.util.Scanner;
import java.util.Random;
class Question2{
System.out.println("Before");
printArray(ArrayData);
Integer ArrayLength = 10;
for(int X = 0; X < ArrayLength; X++){
for(int Y = 0; Y < ArrayLength; Y++){
for(int Z = 0; Z < ArrayLength - Y - 1; Z++){
if(ArrayData[X][Z] > ArrayData[X][Z + 1]){
}
}
2(b)(iii) 1 mark for output showing array unsorted and then sorted on 1 of the dimensions 1
e.g.
Python
def BinarySearch(SearchArray, Lower, Upper, SearchValue):
if Upper >= 0:
Mid = int((Lower + (Upper - 1)) / 2)
If SearchArray[0][Mid] == SearchValue:
return Mid
elif SearchArray[0][Mid] > SearchValue:
return BinarySearch(SearchArray, Lower, Mid-1, SearchValue)
else:
return BinarySearch(SearchArray, Mid+1, Upper, SearchValue)
return -1
2(c)(i) Java
public static Integer BinarySearch(Integer[][] SearchArray, Integer Lower, Integer Upper,
Integer SearchValue){
Integer Mid = 0;
If Upper >= 0 {
Mid = (Lower + (Upper - 1)) / 2;
If SearchArray[0][Mid] == SearchValue ){
return Mid;
}else if SearchArray[0][Mid] > SearchValue {
return BinarySearch(SearchArray, Lower, Mid-1, SearchValue);
}else{
return BinarySearch(SearchArray, Mid+1, Upper, SearchValue);
}
} return -1;
}
Both attributes (Number and Colour) declared with suitable data types …
…as private
correct constructor header (and end where appropriate) with two parameters …
…both parameters assigned to the attributes
Python
class Card:
#Number as Integer
#Colour as string
def __init__(self, Numberp, Colourp):
self.__Number = Numberp
self.__Colour = Colourp
3(a) Java
import java.util.Scanner;
import java.io.*;
class Card{
private Integer Number;
private String Colour;
public Card(Integer pNumber, String pColour){
Number = pNumber;
Colour = pColour;
}
public static void main(String args[]){
}
}
Python
def GetNumber(self):
return self.__Number
def GetColour(self):
return self.__Colour
Java
public Integer GetNumber(){
return Number;
}
Try
Dim Filename As String = "CardValues.txt"
Dim FileReader As New System.IO.StreamReader(filename)
For x = 0 To 29
NumberRead = FileReader.ReadLine()
ColourRead = FileReader.ReadLine()
CardArray(x) = New Card(NumberRead, ColourRead)
Next
FileReader.close()
Catch ex As Exception
Python
CardArray = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] #integer
try:
Filename = "CardValues.txt"
File = open(Filename,'r')
for x in range(0,30):
NumberRead = int(File.readline())
ColourRead = File.readline()
CardArray[x] = Card(NumberRead, ColourRead)
File.close
except IOError:
print("Could not find file")
Java
public static void main(String args[]){
Card[] CardArray = new Card[30];
Integer NumberRead;
String ColourRead;
String FileName = "CardValues.txt";
try{
FileReader F = new FileReader(FileName);
BufferedReader Reader = new BufferedReader(f);
for(Integer x = 0; x < 30; x++){
NumberRead = Integer.parseInt(Reader.readLine());
ColourRead = Reader.readLine();
CardArray[x] = new Card(NumberRead, ColourRead);
}
Reader.close();
}
Function ChooseCard() header (and close) and returning an integer (index) in all cases
Stores the valid Card chosen as taken (using any suitable method)
Python
global NumbersChosen
…
def chooseCard ():
global NumbersChosen
flagContinue = True
while flagContinue == true:
CardSelected = int(input("Select a Card from 1 to 30"))
if CardSelected < 1 or CardSelected > 30:
print("Number must be between 1 and 30")
elif NumbersChosen(CardSelected - 1) == True:
print("Already taken")
else:
print("Valid")
flagContinue = False
NumbersChosen[CardSelected-1] = True
return CardSelected-1
…
#main
…
NumbersChosen = [False for i in range(30)]
3(d) Java
public static Boolean[] NumbersChosen = new Boolean[30];
Python
Player1 = [] #of type Card
for x in range(0, 4):
ReturnNumber = ChooseCard ()
Player1.append(CardArray[ReturnNumber])
for x in range(0, 4):
print(Player1[x].GetColour())
print(Player1[x].GetNumber())
3(e)(i) Java
Card[] Player1 = new Card[5];
for(Integer x = 0; x < 5; x++){
Player1[x] = CardArray[ChooseCard()];
}
for(Integer x = 0; x < 5; x++){
System.out.println(Player1[x].GetColour());
System.out.println(Player1[x].GetNumber());
}