Past-paper question · 2025 May-June · Paper 41 · Q2(a)

Question 2(a) 2025 · May-June · Paper 41 · 7 marks

Original past-paper question for 2(A)
Original past-paper question for 2(A)
Reveal official mark-scheme answer Official mark-scheme answer for 2(A)Official mark-scheme answer for 2(A)Official mark-scheme answer for 2(A)

Official answer for Question 2

Part (a)

Question 1

mark each to max 7

Question 7

  • Function header (and end)
  • Prompt to enter filename and reading input
  • Opening the file (to read) and closing the file in an appropriate place
  • Looping until EOF …
  • … reading each line in the file …
  • … (removing line break and) inserting in array
  • Returning populated array
  • Exception handling try catch with appropriate output
✓
9618/41
✓
Cambridge International AS & A Level – Mark Scheme
✓
May/June 2025
✓
PUBLISHED
✓
Question
✓
Answer
✓
Marks
✓
Example program code:
✓
Python
✓
def ReadData():
✓
DataList = []
✓
FileName = input("Enter the filename")
✓
try:
✓
File = open(FileName)
✓
for Line in File:
✓
DataList.append(Line)
✓
File.close()
✓
except:
✓
print("Cannot open file")
✓
return DataList
✓
VB.NET
✓
Function ReadData()
✓
Dim DataList(100) As String
✓
Console.WriteLine("Enter the filename")
✓
Dim FileName As String = Console.ReadLine()
✓
NumberItems = 0
✓
Try
✓
Dim FileReader As New System.IO.StreamReader(FileName)
✓
While Not FileReader.EndOfStream
✓
DataList(NumberItems) = FileReader.ReadLine()
✓
NumberItems = NumberItems + 1
✓
End While
✓
FileReader.Close()
✓
Catch ex As Exception
✓
Console.WriteLine("Cannot open or read from file")
✓
End Try
✓
Return DataList
✓
End Function
✓
9618/41
✓
Cambridge International AS & A Level – Mark Scheme
✓
May/June 2025
✓
PUBLISHED
✓
Question
✓
Answer
✓
Marks
✓
Java
✓
public static String[] ReadData(){
✓
String[] DataList = new String[100];
✓
System.out.println("Enter the filename");
✓
Scanner scanner = new Scanner(System.in);
✓
String FileName = scanner.nextLine();
✓
NumberItems = 0;
✓
try{
✓
FileReader f = new FileReader(FileName);
✓
try{
✓
BufferedReader Reader = new BufferedReader(f);
✓
String Line = Reader.readLine();
✓
Line = Line.replace("\n","");
✓
while (Line != null){
✓
DataList[NumberItems] = Line;
✓
NumberItems++;
✓
Line = Reader.readLine();
✓
if(Line != null){
✓
Line = Line.replace("\n","");
✓
}
✓
}
✓
Reader.close();
✓
}catch(IOException ex){
✓
}
✓
}catch(FileNotFoundException e){
✓
System.out.println("File not found");
✓
}
✓
return DataList;
✓
}
✓
9618/41
✓
Cambridge International AS & A Level – Mark Scheme
✓
May/June 2025
✓
PUBLISHED
✓
Question
✓
Answer
✓
Marks

Mark scheme 9618_s25_ms_41.pdf →

Show text version of question

Question 2

Part (a)

Question 2

A program reads data from a text file, splits the data depending on its content and stores the

separated data into different files.

The text file TheData.txt contains 72 lines of data. Each line of data has an integer number and a string colour that are separated by a comma. For example, the first line in the file is:

10,red

The integer is 10 and the string is "red"

The file contains six different colours: red, green, blue, orange, yellow, pink.

Part (a)

The function ReadData():

  • prompts the user to enter a filename and reads this filename from the user
  • opens the file and reads each line of data into a 1D array
  • returns the populated 1D array.

The function needs to work for a file that contains an unknown number of lines.

Write program code for ReadData()

Save your program as Question2_J25.

Copy and paste the program code into part 2(a) in the evidence document.

[7]
Topic relevance

Why this question appears here

87% assigned-topic match · Data types and structures › Files

Topic-match percentages estimate how closely each question fits the selected syllabus topic. They are a guide for choosing stricter or broader practice.

Permanent source page: Educate A Change · From 9618_s25_qp_41.pdf

Keep practising this topic