AQA A-Level Computer Science Paper 1, June 2025: Question 10

7 marks · Hard difficulty · Programming

Modify the Skeleton Program subroutines CheckIfUserInputValid, ConvertToRPN, and EvaluateRPN to support right-associative exponentiation.

Practise this question

Question

Question 10.1 asks students to modify the Skeleton Program to allow the player to use exponentiation (denoted by the caret symbol) in entered expressions. It explains operator precedence and associativity, noting that standard operators are left-associative while exponentiation is right-associative, with illustrative examples. Students are instructed in Task 1 to modify CheckIfUserInputValid, ConvertToRPN, and EvaluateRPN subroutines, and in Task 2 to test the program with specified inputs. The question asks for the program source code of the three amended subroutines for 7 marks.
Question text

10 This question extends the Skeleton Program by allowing the player to use

exponentiation in the expressions they enter.

The player will be allowed to use the ^ symbol to denote exponentiation in an

expression they enter. Exponentiation has higher precedence than the other

operators.

The four operators currently allowed in expressions are left-associative.

Example

12-4-3 is equal to 5

as 12-4 is evaluated to 8 and then 8-3 is evaluated to 5

If subtraction was right-associative, then 12-4-3 would evaluate to 11

as 4-3=1 and 12-1=11

The exponentiation operator will be right-associative.

Example

2^3^2 is equal to 512

as 3^2 is evaluated to 9 and then 2^9 is evaluated to 512

If exponentiation was left-associative, then 2^3^2 would evaluate to 64

as 2^3=8 and 8^2=64

What you need to do

Task 1

Modify the CheckIfUserInputValid, ConvertToRPN and EvaluateRPN

subroutines so the exponentiation operator is allowed and works correctly.

Note: you will be able to achieve most of the marks for this question if you

implement the exponentiation operator as left-associative.

Task 2

Test that the changes you have made work:

• run the Skeleton Program

• select the training game

• enter 512/2^3^2+8

• enter 3^2

Evidence that you need to provide

Include the following evidence in your Electronic Answer Document.

10.1 Your PROGRAM SOURCE CODE for the amended subroutines

CheckIfUserInputValid, EvaluateRPN, and ConvertToRPN.

[7 marks]

Mark scheme

Show the mark scheme Mark scheme for question 10.1 allocates 7 marks for AO3 programming across the modified subroutines: modifying the regular expression in CheckIfUserInputValid (1 mark); updating the operator list, condition, and exponentiation calculation in EvaluateRPN (3 marks); and adding the caret operator to the precedence dictionary with higher precedence and handling right-associativity logic when comparing operators in ConvertToRPN (3 marks). A maximum of 6 marks is awarded if the code contains errors.

Question Marks

10 1 All marks for AO3 (programming) 7

Changes to CheckIfUserInputValid subroutine:

1. Correctly modified regular expression;

Changes to EvaluateRPN subroutine:

2. Operator ^ added to list used in condition for second iterative structure;

3. Selection structure with correct condition for ^ operator;

4. Calculates Num1 to the power of Num2 and stores result in Result;

Changes to ConvertToRPN subroutine:

5. New operator (^) added to dictionary and will have higher precedence than

other operators;

6. Condition that checks if current operator is (or is not) ^;

7. If current operator is ^ and last item in Operators is a ^ then

CurrentOperator is added to end of Operators, the last item is not

removed from Operators and the last item in Operators is not added to

UserInputInRPN

Max 6 marks if code contains errors–A-LEVEL COMPUTER SCIENCE – –

10 2 Mark is for AO3 (evaluate) 1

**** SCREEN CAPTURE ****

Must match code from 10.1.

Code for 10.1 must be sensible.

Screen capture(s) showing training game was played followed by first expression

being entered with 9 being removed from the targets and score increasing to one,

then second expression being entered and score becoming zero;

How to answer it

Extending the Expression Evaluator: Exponentiation (^)

📋 What this question tests

This question assesses Section D of Paper 1: adapting and extending the supplied Skeleton Program. Specifically, it tests your ability to:

  • Regex Validation: Updating a regular expression pattern to accept an additional operator ( ^ ).
  • Reverse Polish Notation (RPN) Evaluation: Adding the power operation ( ** or language equivalent) into the stack-based evaluation algorithm.
  • Shunting-Yard Infix-to-RPN Conversion: Handling operator precedence and the notoriously subtle rules of right-associativity.
  • Testing & Evidence: Running exact test cases and capturing terminal output accurately.
Part 10.1 • 7 Marks

Program Source Code Modifications

Modifying CheckIfUserInputValid, EvaluateRPN, and ConvertToRPN

💡 Key Knowledge: Left vs Right Associativity

Standard operators like + , - , * , / are left-associative:

  • 12 - 4 - 3 = (12 - 4) - 3 = 5

Exponentiation ( ^ ) is right-associative:

  • 2 ^ 3 ^ 2 = 2 ^ (3 ^ 2) = 2 ^ 9 = 512

In Dijkstra's Shunting-Yard algorithm:

  • For left-associative operators: pop from stack if stack top has precedence greater than or equal to incoming operator.
  • For right-associative operators: pop from stack only if stack top has strictly greater precedence. If equal (both ^ ), do not pop! Push directly.

📐 Tracing the Shunting-Yard Condition

Evaluating expression: 512 / 2 ^ 3 ^ 2 + 8

  1. Encounter first ^ : Stack has ['/'] (prec 2). Precedence of ^ is 3. Since 3 > 2, push ^ . Stack: ['/', '^'] .
  2. Encounter second ^ : Stack top is already ^ (prec 3).
  3. Right-associative rule: Because the incoming operator is ^ , it must not pop the existing ^ . Push it directly! Stack: ['/', '^', '^'] .
  4. When popped later, the second ^ is evaluated first, achieving 3 ^ 2 = 9 , then 2 ^ 9 = 512 , then 512 / 512 = 1 , then 1 + 8 = 9 .

✅ Correct Code Changes (Python Reference Implementation)

1. Subroutine: CheckIfUserInputValid (Mark 1)

# Add \^ into the allowed character class in the regex # E.g., change r"^([0-9]|[\+\-\*/])+$" to: r"^([0-9]|[\+\-\*/\^])+$"

2. Subroutine: EvaluateRPN (Marks 2, 3, 4)

# Mark 2: Add '^' to the operators list checked in the loop while len(UserInputInRPN) > 0: CurrentSymbol = UserInputInRPN.pop(0) if CurrentSymbol in ['+', '-', '*', '/', '^']: # Mark 2 Num2 = S.pop() Num1 = S.pop() ... elif CurrentSymbol == '^': # Mark 3 Result = Num1 ** Num2 # Mark 4 (Note: Num1 ^ Num2, NOT Num2 ^ Num1) S.append(Result)

3. Subroutine: ConvertToRPN (Marks 5, 6, 7)

# Mark 5: Add '^' with highest precedence (e.g. 3) to the precedence dictionary Precedence = {'+': 1, '-': 1, '*': 2, '/': 2, '^': 3} # In the operator handling while loop: # Marks 6 & 7: Check if CurrentOperator is right-associative while (len(Operators) > 0 and Operators[-1] != '(' and (Precedence[Operators[-1]] > Precedence[CurrentSymbol] or (Precedence[Operators[-1]] == Precedence[CurrentSymbol] and CurrentSymbol != '^'))): UserInputInRPN.append(Operators.pop()) Operators.append(CurrentSymbol)

❌ Common Errors & Traps

  • Operand Order in Stack Pop: For exponentiation, order matters! In RPN, Num2 = pop() and Num1 = pop() means the calculation must be Num1 ** Num2 , not Num2 ** Num1 .
  • Bitwise XOR trap: In Python, ^ is the bitwise XOR operator, not power. You must use ** or math.pow() .
  • Treating ^ as left-associative: Simply adding '^': 3 into the dictionary without altering the pop loop will treat ^ as left-associative. The question states you get most marks (up to 5 of 7), but you lose Marks 6 & 7.
  • Escaping regex: Forgetting to escape \^ or putting it at the wrong place in the regex set, causing it to act as a negate operator.

🧠 Exam Technique & Mark Scheme Insights

  • Partial Credit Strategy: Notice the question prompt: "you will be able to achieve most of the marks for this question if you implement the exponentiation operator as left-associative". If right-associativity confuses you under time pressure, just add ^ with precedence 3 and move on to bank 5/7 marks quickly!
  • Max 6 Marks Cap: If your code contains any syntax errors or fatal bugs, the mark scheme caps you at a maximum of 6 marks, regardless of other correct lines. Ensure your code compiles cleanly.
Mark Breakdown (7 Marks AO3):
• M1: Regex modified correctly to include ^
• M2: ^ added to the list in condition for second iterative structure in EvaluateRPN
• M3: Selection condition specifically checking for ^
• M4: Computes Num1 raised to the power of Num2 and assigns to Result
• M5: ^ added to dictionary/lookup with strictly higher precedence than +,-,*,/
• M6: Condition checking whether current operator is / is not ^
• M7: If current is ^ and top of stack is ^ , it pushes without popping top of stack (right-associative logic)
Part 10.2 • 1 Mark

Evidence / Screen Capture Test

Executing the Required Test Sequence

✅ Required Evidence Checklist

Your Electronic Answer Document must contain a clear screenshot showing all of the following steps executed in sequence:

  1. Select training game ( y ).
  2. Enter first expression: 512/2^3^2+8
  3. Observe target 9 is hit and removed from the target list, and score increases from 0 to 1.
  4. Enter second expression: 3^2
  5. Observe that an invalid or miss condition occurs (or targets update) and the current score resets/becomes 0.

🧠 How Examiners Award the Mark

  • Strict Match to 10.1: The screenshot output must match your actual code submitted in 10.1. If your code is left-associative, 512/2^3^2+8 evaluates to 512 / 8 ^ 2 + 8 = 512 / 64 + 8 = 8 + 8 = 16 , which will NOT remove 9!
  • Zero Tolerance on Incomplete Captures: Do not crop out the prompt where you type y for the training game, or the final score line. The entire sequence from start to finish must be clearly legible.
Mark Breakdown (1 Mark AO3 Evaluate):
• 1 mark for screen capture showing training game played, followed by first expression entered with 9 being removed and score becoming 1, followed by second expression entered and score becoming 0.

Topics

4.1 Fundamentals of programming · 4.3 Fundamentals of algorithms · 4.4 Theory of computation · 4.1.1 Programming · 4.3.3 Reverse Polish · 4.4.2 Regular languages

Question and mark scheme from the AQA A-Level Computer Science examination, Paper 1, June 2025. QuestionVault is an independent revision resource; questions remain the copyright of the awarding body.