Past-paper question · 2025 May-June · Paper 42 · Q1(d)

Question 1(d) 2025 · May-June · Paper 42 · 6 marks

Original past-paper question for 1(D)
Original past-paper question for 1(D)
Original past-paper question for 1(D)
Reveal official mark-scheme answer Official mark-scheme answer for 1(D)Official mark-scheme answer for 1(D)

Official answer for Question 1

Part (d)

Question 1

mark each

Question 6

  • Procedure header (and end where appropriate) taking one (string) parameter
  • Opening the file with the filename parameter and closing the file in an appropriate place
  • Looping through to end of file and reading in each line ...
  • … calling Push() once with each read in value …
  • … if any return value from Push() is integer –1 outputting "Stack full"
  • Exception handling for opening and reading from file with appropriate catch and output
✓
Example program code:
✓
Java
✓
public static void ReadData(String FileName){
✓
Integer ReturnValue;
✓
try{
✓
FileReader f = new FileReader(FileName);
✓
try{
✓
BufferedReader Reader = new BufferedReader(f);
✓
String Line= Reader.readLine();
✓
Line = Line.replace("\n","");
✓
while (Line != null){
✓
Line = Line.replace("\n","");
✓
ReturnValue = Push(Line);
✓
if (ReturnValue == -1){
✓
System.out.println("Stack full");
✓
}
✓
Line = Reader.readLine();
✓
}
✓
Reader.close();
✓
}catch(IOException ex){}
✓
}catch(FileNotFoundException e){
✓
System.out.println("File not found");
✓
}
✓
}
✓
9618/42
✓
Cambridge International AS & A Level – Mark Scheme
✓
May/June 2025
✓
PUBLISHED
✓
Question
✓
Answer
✓
Marks
✓
VB.NET
✓
Sub ReadData(ByVal FileName As String)
✓
Dim ReturnValue As String
✓
Try
✓
Dim FileReader As New System.IO.StreamReader(FileName)
✓
While Not FileReader.EndOfStream
✓
ReturnValue = Push(FileReader.ReadLine())
✓
If ReturnValue = "-1" Then
✓
Console.WriteLine("Stack full")
✓
End If
✓
End While
✓
FileReader.Close()
✓
Catch ex As Exception
✓
Console.WriteLine("Cannot open file")
✓
End Try
✓
End Sub
✓
Python
✓
def ReadData(FileName):
✓
global Stack
✓
global TopOfStack
✓
try:
✓
File = open(FileName)
✓
for Line in File:
✓
ReturnValue = Push(Line.strip())
✓
if ReturnValue == -1:
✓
print("Stack full")
✓
File.close()
✓
except:
✓
print("Cannot open file")
✓
9618/42
✓
Cambridge International AS & A Level – Mark Scheme
✓
May/June 2025
✓
PUBLISHED
✓
Question
✓
Answer
✓
Marks

Mark scheme 9618_s25_ms_42.pdf →

Show text version of question

Question 1

Part (d)

Question 1

A program reads data from a text file and stores it in a stack. The stack Stack is stored as a 1D

array of up to 20 elements. The pointer TopOfStack stores the index of the last element stored in the stack.

Part (d)

The text file StackData.txt stores numbers and mathematical operators. Each number

and operator are on a new line in the text file.

The mathematical operators used in this program are:

mathematical meaning operator + addition

  • subtraction

/ division * multiplication ^ power of

The procedure ReadData():

  • takes a string filename as a parameter
  • opens the file and reads in each line of data
  • uses the Push() function to insert each line of data onto the stack
  • outputs "Stack full" if any data cannot be stored on the stack because the stack is
  • full

  • uses exception handling when opening and reading from the text file.

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

Write program code for ReadData()

Save your program.

Copy and paste the program code into part 1(d) in the evidence document.

[6]

Question 4

Topic relevance

Why this question appears here

82% assigned-topic match · Programming › Structured programming

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_42.pdf

Keep practising this topic