Cambridge A2 Level Computer Science 9618 · 20.2 File Processing and Exception Handling

Q28 · 9618/32/O/N/23 Q12

Worked solution from the Further Programming Paper 3 Advanced Theory booklet, Part 2 of 2.

Q289618/32/O/N/23original Q12Structured · 9 marks
Question 12 from 9618/32/O/N/23

Worked solution

(a)

Write: an exception is an event during a program's run that disrupts the normal flow of instructions or makes the program stop. [1]

Give an example, such as a hardware failure (disk crash), a divide by zero or out-of-range array index, or a user error like a wrong file name. [1]

(b)

Fill each gap. The loop stops when the customer is found or the end of the file is reached.

OPENFILE "AccountRecord.dat" FOR RANDOM
Location ← 1
FoundFlag ← FALSE
OUTPUT "Enter the customer’s name"
INPUT SearchCustomer
WHILE NOT FoundFlag AND Location <= MaxSize
    SEEK "AccountRecord.dat", Location
    GETRECORD "AccountRecord.dat", Customer
    IF SearchCustomer = Customer.Name THEN
        OUTPUT "Customer found: "
        OUTPUT Customer // output customer record
        FoundFlag ← TRUE
    ENDIF
    Location ← Location + 1
ENDWHILE
IF NOT FoundFlag THEN
    OUTPUT "Customer does not exist."
ENDIF

OPENFILE gap: "AccountRecord.dat" FOR RANDOM [1]

Flag gap: FoundFlag [1]

Input line: INPUT SearchCustomer [1]

WHILE gap: WHILE NOT FoundFlag [1]

SEEK gap: SEEK [1]

Position gap: Location [1]

Error message, such as Customer does not exist. [1]

KEY IDEASearch a random file with a WHILE loop that stops when the item is found or the last location is passed.
TOTAL 9 marks

Official mark scheme

Mark scheme for 9618/32/O/N/23 question 12
VIDEO
▶ Whole paper

Video solution of the whole paper · by TEACHING BY SM. Tap the picture to open it on YouTube.

Solutions follow the official Cambridge mark schemes; marks are shown in grey. Videos are from other YouTube channels. Paper codes read component / series / year: M/J = May/June, O/N = October/November. Past-paper questions © UCLES, reproduced for classroom practice.