Process the file.

Six original Cambridge-style questions on file processing in 9618 pseudocode, including writing file-handling code and hashing calculations, with mark-scheme solutions.

Original questions All questions on this page are original work, written in the Cambridge AS & A Level style. They are not from past papers. They test the same concepts and skills the syllabus rewards.
Get the marks

Open, loop, close.

01
Explain
[3 marks]

Describe the difference between a serial, a sequential and a random file.

  • Serial: records are stored one after another in the order they were added / no particular order. ✓
  • Sequential: records are stored in order of a key field. ✓
  • Random: records are stored at addresses (often calculated from the key by hashing) and can be accessed directly without reading earlier records. ✓
02
Write
[6 marks]

The text file Parts.txt holds one part code per line. A code that begins with the letter X is faulty. Write pseudocode for a procedure that reads every line and writes each faulty code to a new file Faulty.txt. It then outputs how many faulty codes were written.

  • Declare LineOfText : STRING and Count : INTEGER; Count ← 0. ✓
  • OPENFILE "Parts.txt" FOR READ and OPENFILE "Faulty.txt" FOR WRITE. ✓
  • WHILE NOT EOF("Parts.txt") ... ENDWHILE. ✓
  • READFILE "Parts.txt", LineOfText inside the loop. ✓
  • IF MID(LineOfText, 1, 1) = "X" THEN WRITEFILE "Faulty.txt", LineOfText and Count ← Count + 1. ✓
  • CLOSEFILE both files and OUTPUT Count. ✓
03
Trace
[3 marks]

A sequential file holds records with keys 105, 118, 140 and 162. Using the insertion method in this lesson, state the order of records in the new file when a record with key 170 is inserted, and explain which part of the algorithm writes it.

  • No existing key is greater than 170, so the record is not written inside the loop. ✓
  • All four existing records are copied: 105, 118, 140, 162. ✓
  • After the loop, Inserted is still FALSE, so the final IF writes 170 at the end: 105, 118, 140, 162, 170. ✓
04
Calculate
[3 marks]

A random file stores records at the address Key MOD 200. (a) Calculate the address for keys 1450 and 2650. (b) State the problem this shows and one way of dealing with it.

  • (a) 1450 MOD 200 = 50 (1450 = 7 × 200 + 50); 2650 MOD 200 = 50 (2650 = 13 × 200 + 50). ✓
  • (b) Both keys hash to the same address: a collision. ✓
  • Store the second record in the next free address (or an overflow area) and search from the calculated address until it is found. ✓
05
Write
[5 marks]

A random file Accounts.dat holds records of type AccountRecord, which has fields AccountNo (INTEGER) and Balance (REAL). The record for account number AccNum is at address AccNum MOD 1000. Write pseudocode to add 25.00 to the balance of the account whose number is in AccNum.

  • DECLARE ThisAccount : AccountRecord and calculate Address ← AccNum MOD 1000. ✓
  • OPENFILE "Accounts.dat" FOR RANDOM. ✓
  • SEEK "Accounts.dat", Address then GETRECORD "Accounts.dat", ThisAccount. ✓
  • ThisAccount.Balance ← ThisAccount.Balance + 25.00. ✓
  • SEEK "Accounts.dat", Address then PUTRECORD "Accounts.dat", ThisAccount and CLOSEFILE "Accounts.dat". ✓
06
Analysis
[3 marks]

A cinema stores seat bookings. The booking office must find and update a single seat instantly, and once a night every booking is printed in seat order. Recommend a file organisation and justify it.

  • A random file, with the address calculated from the seat number. ✓
  • Any seat's record can be reached directly with SEEK and updated with PUTRECORD, without reading earlier records, so updates are fast. ✓
  • If the address is the seat number itself, reading addresses in order also prints the bookings in seat order (an index or sequential copy could be used otherwise). ✓

Mark this once you have attempted all six and checked your working. It records a Practiced badge on the topic and adds a one-time bonus. Revealing the solutions alone does not count.