[ OCR J277 ERL ]

Exam Practice Hub (Component 02)

Welcome to the ultimate exam preparation zone. Component 02 of the OCR J277 specification tests your ability to read, trace, debug, and write algorithms. Use this hub to hone your skills in specific areas.

Timed Mock Paper

Take a 15-minute simulated exam under pressure.

Launch Simulator →

Skill 1: Code Comprehension & Tracing

Q1. Nested Iteration Trace

[4 marks]

Trace the following algorithm mentally or using scratch paper. State the exact output that will be printed.

total = 0
for i = 1 to 3
    for j = 1 to 2
        total = total + i
    next j
next i
print(total)
Answer: 12
  • 1 mark for identifying the outer loop runs 3 times (i=1,2,3).
  • 1 mark for identifying the inner loop runs 2 times for each outer loop iteration (6 total iterations).
  • 1 mark for correct math per iteration: (1+1) + (2+2) + (3+3).
  • 1 mark for the final correct number: 12.

Q2. Array Traversal

[3 marks]

What does the following sub-program do? Explain its purpose in one sentence.

function mystery(arr)
    target = arr[0]
    for i = 1 to arr.length - 1
        if arr[i] > target then
            target = arr[i]
        endif
    next i
    return target
endfunction
Answer: It finds and returns the maximum (highest) value in the array.
  • 1 mark for mentioning it loops through the array.
  • 1 mark for identifying the comparison logic (checking if next item is greater).
  • 1 mark for stating it finds/returns the maximum value.

Skill 2: Debugging Algorithms

Q3. Linear Search Logic Error

[4 marks]

A student has written a Linear Search to find a name in an array. However, it doesn't work correctly. Identify the error and rewrite the corrected algorithm in the editor below.

array students[4]
students[0] = "Alice"
students[1] = "Bob"
students[2] = "Charlie"
students[3] = "David"

found = False
targetName = input("Who to find?")

for i = 0 to 3
    if students[i] == targetName then
        found = False
    else
        found = True
    endif
next i

if found == True then
    print("Found them")
else
    print("Not here")
endif
linear_search.erl
Error Identified: The boolean logic is inverted inside the `if` statement. Also, if a name is found early, the loop continues and the `else` clause might overwrite `found` back to False on subsequent iterations.

Corrected Logic:
for i = 0 to 3
    if students[i] == targetName then
        found = True   // FIXED here
    endif
next i
(No else block needed. 2 marks for fixing the boolean assignment. 2 marks for removing the else block to prevent overwriting).

Skill 3: Writing Algorithms

Q4. Validation Loop

[5 marks]

Write an algorithm that prompts the user to enter a rating between 1 and 5 inclusive. The algorithm should repeatedly reject inputs outside this range and ask again until a valid rating is entered. Finally, print "Thank you".

validation.erl
Model Answer:
rating = 0
do
    ratingStr = input("Enter rating (1-5):")
    rating = int(ratingStr)
until rating >= 1 AND rating <= 5
print("Thank you")
  • 1 mark: Using a loop (WHILE or DO...UNTIL).
  • 1 mark: Using input() correctly inside the loop.
  • 1 mark: Casting the input to an integer.
  • 1 mark: Correct boundary logic (>= 1 AND <= 5 for DO loop, or < 1 OR > 5 for WHILE loop).
  • 1 mark: Printing the final message outside the loop.

Q5. Array Calculations

[6 marks]

A teacher has stored 5 test scores in an array called results. Write a complete program that calculates the average (mean) score of all 5 tests and outputs it. Assume the array is already populated for you.

average.erl
Model Answer:
total = 0
for i = 0 to 4
    total = total + results[i]
next i
average = total / 5
print("Average is: " + str(average))
  • 1 mark: Declaring a total variable and setting to 0.
  • 1 mark: Creating a correctly bounded loop (e.g., 0 to 4).
  • 2 marks: Correctly adding array elements to total inside loop.
  • 1 mark: Dividing the final total by 5 outside the loop.
  • 1 mark: Correctly outputting the result.