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

Question 1(e) 2025 · May-June · Paper 42 · 0 marks

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

Official answer for Question 1

Part (e)

Question 1

mark for:

Question 7

  • Calculate() function header (and end where appropriate)
  • Looping until the stack is empty
  • Calling Pop() repeatedly within loop and storing/using return value
  • … working out if return value from Pop() call is an operator or a number / alternating between operator and number
  • Select to determine if the operator is +, -, /, * or ^ and attempt the matching calculation
  • … performing correct calculation using operator, number
  • … updating total from previous loops and returning this final value
✓
Example program code:
✓
Java
✓
public static Double Calculate(){
✓
Double Total = Double.parseDouble(Pop());
✓
String ReturnValue = "";
✓
String LastOperator = "";
✓
Boolean OperatorFlag = true;
✓
Integer TheData = 0;
✓
while(ReturnValue != "-1"){
✓
ReturnValue = Pop();
✓
if(OperatorFlag == false){
✓
TheData = Integer.parseInt(ReturnValue);
✓
if(LastOperator.compareTo("+")==0){
✓
Total = Total + TheData;
✓
}else if(LastOperator.compareTo("-")==0){
✓
Total = Total - TheData;
✓
}else if(LastOperator.compareTo("*")==0){
✓
Total = Total * TheData;
✓
}else if(LastOperator.compareTo("/")==0){
✓
Total = Total / TheData;
✓
}else if(LastOperator.compareTo("^")==0){
✓
Total = Math.pow(Total, TheData);
✓
}
✓
OperatorFlag = true;
✓
9618/42
✓
Cambridge International AS & A Level – Mark Scheme
✓
May/June 2025
✓
PUBLISHED
✓
Question
✓
Answer
✓
Marks
✓
}else{
✓
LastOperator = ReturnValue;
✓
OperatorFlag = false;
✓
}
✓
}
✓
return Total;
✓
}
✓
VB.NET
✓
Function Calculate()
✓
Dim Total As Integer = Pop()
✓
Dim ReturnValue As String = ""
✓
Dim LastOperator As String = ""
✓
Dim OperatorFlag As Boolean = True
✓
Dim TheData As Integer = 0
✓
While (ReturnValue <> "-1")
✓
ReturnValue = Pop()
✓
Select Case OperatorFlag
✓
Case False
✓
TheData = ReturnValue
✓
Select Case LastOperator
✓
Case "+"
✓
Total = Total + TheData
✓
Case "-"
✓
Total = Total - TheData
✓
Case "*"
✓
Total = Total * TheData
✓
Case "/"
✓
Total = Total / TheData
✓
Case "^"
✓
Total = Total ^ TheData
✓
End Select
✓
OperatorFlag = True
✓
Case Else
✓
LastOperator = ReturnValue
✓
9618/42
✓
Cambridge International AS & A Level – Mark Scheme
✓
May/June 2025
✓
PUBLISHED
✓
Question
✓
Answer
✓
Marks
✓
OperatorFlag = False
✓
End Select
✓
End While
✓
Return Total
✓
End Function
✓
Python
✓
def Calculate():
✓
global Stack
✓
global TopOfStack
✓
Total = Pop()
✓
Total = int(Total)
✓
Return = 0
✓
LastOperator = ""
✓
Operator = True
✓
while(Return != "-1"):
✓
Return = Pop()
✓
if Operator == False:
✓
Data = int(Return)
✓
if LastOperator == "+":
✓
Total = Total + Data
✓
elif LastOperator == "-":
✓
Total = Total - Data
✓
elif LastOperator == "*":
✓
Total = Total * Data
✓
elif LastOperator == "/":
✓
Total = Total / Data
✓
elif LastOperator == "^":
✓
Total = Total ** Data
✓
Operator = True
✓
else:
✓
LastOperator = Return
✓
Operator = False
✓
return Total
✓
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 (e)

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 (e)

The values stored in the stack are used to perform mathematical operations.

A
total is initialised to the first value in the stack. The first value in the stack will always be a

number.

The next value will be a mathematical operator and the next value will be the number to apply to the calculation. This is repeated until there are no values left in the stack. There will always be a number following a mathematical operator.

For example, the contents of a stack are:

10 TopOfStack +

Question 3

–

Topic relevance

Why this question appears here

49% assigned-topic match · Programming › Constructs

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