File Processing & Exception Handling (Cambridge (CIE) A Level Computer Science): Exam Questions

Exam code: 9618

2 hours12 questions
11 mark

Module

Description

CheckAll()

  • called with a parameter of type integer representing the number of candidate marks in the Result array

  • uses CheckMark() to check each candidate mark

  • for each paper that needs to be checked, write the corresponding candidate ID on a separate line in a new file named GRList.txt

  • outputs a message with a count of how many papers need to be checked

The requirement changes. Instead of a new file, the module described above needs to add the corresponding candidate ID for each paper that needs to be checked to an existing file.

Explain the change that will need to be made to CheckAll().

2a7 marks

A program is being developed to implement a game for up to six players.

During the game, each player assembles a team of characters. At the start of the game there are 45 characters available.

Each character has four attributes, as follows:

Attribute

Examples

Comment

Player

0, 1, 3

The player the character is assigned to.

Role

Builder, Teacher, Doctor

The job that the character will perform in the game.

Name

Bill, Lee, Farah, Mo

The name of the character. Several characters may perform the same role, but they will each have a unique name.

Level

14, 23, 76

The skill level of the character. An integer in the range 0 to 100 inclusive.

The programmer has defined a record type to define each character.

A new module will store the contents of the Character array in a text file.

The module is defined as follows:

Module

Description

Save()

  • form a string from each record with fields separated by the character '^'

  • write each string to a separate line of the new file named SaveFile.txt

Complete the pseudocode for module Save().

PROCEDURE Save()

2b2 marks

The program is changed and the record type definition is modified as follows:

TYPE CharacterType

DECLARE Player : INTEGER

DECLARE Role : STRING

DECLARE Name : STRING

DECLARE Level : INTEGER

DECLARE Status : BOOLEAN

ENDTYPE

Describe how the additional Boolean field may be stored with the rest of the fields on one line of a text file.

3a7 marks

The Character array data has been saved in the text file SaveFile.txt Each line of the file contains one element of the array (one record).

New modules are defined:

Module

Description

Extract() (already written)

  • called with two parameters:

    • a string representing a complete line from the text file

    • an integer representing a field number (see structure below)

    • returns a string representing the required field

Restore()

  • opens the text file SaveFile.txt

  • reads lines from the file and assigns values to each record in the Character array using data from each line of the file

As a reminder, the record structure is repeated here:

TYPE CharacterType

DECLARE Player : INTEGER //Field number 1

DECLARE Role : STRING //Field number 2

DECLARE Name : STRING //Field number 3

DECLARE SkillLevel : INTEGER //Field number 4

ENDTYPE

Write pseudocode for module Restore().

You must use the module Extract().

3b2 marks

The game can last for several days and users often find that they have to close and rerun the game program many times in order to complete it.

Describe the benefit of using the file SaveFile.txt as described above.

47 marks

A second module is defined:

Module

Description

Stage_1()

  • called with a parameter of type string representing a student name

  • creates a new stage 1 file

  • copies each line from the student’s project file to the stage 1 file after removing any comment from each line

  • does not write blank lines to the stage 1 file

  • returns the number of lines written to the stage 1 file

Write pseudocode for module Stage_1().

Module DeleteComment() must be used in your solution

58 marks

Two modules are defined:

Module

Description

DeleteComment() (already written)

  • called with a parameter of type string representing a line of pseudocode from a student’s project file

  • returns the line after removing any comment

Stage_2()

  • called with two parameters:

    • a string representing an input file name

    • a string representing an output file name

  • copies each line from the input file to the existing output file having first removed all leading spaces and comments from that line

  • does not write blank lines to the output file

  • outputs a final message giving the number of blank lines removed

Write pseudocode for module Stage_2(). Modules DeleteComment() and DeleteSpaces() must be used in your solution.

65 marks

A program uses a global 1D array of type string and a text file.

An algorithm that forms part of the program is expressed as follows:

  • copy the first line from the file into the first element of the array

  • copy the second line from the file into the second element of the array

  • continue until all lines in the file have been copied into the array

Stepwise refinement is applied to the algorithm.

Outline five steps for this algorithm that could be used to produce pseudocode.

Assume there are more elements in the array than lines in the file.

Do not use pseudocode statements in your answer.

Step 1 ...............................................

Step 2 ...........................................

Step 3 ............................................

Step 4 ................................................

Step 5 ................................................

78 marks

A new module is required:

Module

Description

FindModules()

  • called with a parameter of type string representing a student project file name

  • uses module Header() to check each line of the project

  • assigns values to the ModInfo array for each module declaration in the student project

As a reminder, the previous example of part of the array is repeated below:

x = 1

x = 2

x = 3

ModInfo[10, x]

"27"

"P"

"MyProc(Z : CHAR)"

ModInfo[11, x]

"35"

"F"

"MyFun(Y : CHAR) RETURNS BOOLEAN"

Write pseudocode for module FindModules().

Assume that the array contains enough rows for the number of modules in each project.

8a7 marks

A class of students are developing a program to send data between computers. Many computers are connected together to form a wired network. Serial ports are used to connect one computer to another.

Each computer:

• is assigned a unique three-digit ID
• has three ports, each identified by an integer value
• is connected to between one and three other computers.

Messages are sent between computers as a string of characters organised into fields as shown:

<STX><DestinationID><SourceID><Data><ETX>

Field number

Field name

Description

n/a

STX

a single character marking the start of the message (ASCII value 02

1

DestinationID

three numeric characters that identify the destination computer

2

SourceID

three numeric characters that identify the source computer

3

Data

a variable length string containing the data being sent (Minimum length is 1 character)

n/a

ETX

a single character marking the end of the message (ASCII value 03

For example, the following message contains the data "Hello Kevin" being sent from computer "101" to computer "232":

<STX>"232101Hello Kevin"<ETX>

Each computer will run a copy of the same program. Each program will contain a global variable, MyID of type string, that contains the unique ID of the computer in which the program is running.

The programmer has defined the first two program modules as follows:

Module

Description

Transmit() (already written)

• takes two parameters:

o a string containing a message
o an integer containing a port number

• transmits the message using the given port

SendFile()

• takes three parameters:
o a string containing a text file name
o a string containing a Destination ID
o an integer containing a Port number
• transmits the file one line at a time
• transmits a final message with data string "****"

Write pseudocode for module SendFile().

Assume:

  • module Transmit() has already been written and is used to transmit a message

  • the value of MyID may be used as SourceID

  • the file specified contains no blank lines

  • e file specified does not contain the line "****"

8b2 marks

Module SendFile() is used to copy a file from one computer to another.

A module within the program running on the destination computer will receive the data and write it to a new file.

Explain why module SendFile() transmits the message with data string "****" after the last line of the file.

9a6 marks

A procedure CreateFiles() will take two parameters:

  • a string representing a file name

  • an integer representing the number of files to be created.

The procedure will create the number of text files specified.

Each file is given a different name. Each file name is formed by concatenating the file name with a suffix based on the file number. The suffix is always three characters.

For example, the call CreateFiles("TestData", 3) would result in the creation of the three files, TestData.001, TestData.002 and TestData.003.

Each file will contain a single line. For example, file TestData.002 would contain the string:

This is File TestData.002

Write pseudocode for CreateFiles().

Assume both parameters are valid and that the integer value is between 1 and 999, inclusive.

9b1 mark

A module CheckFiles() will count the number of files produced by CreateFiles() in part (a).

CheckFiles() will take a string representing a file name and return the number of files found.

State the file mode that should be used in CheckFiles().

10a7 marks

A class of students are developing a program to send data between computers. Many computers are connected together to form a wired network. Serial ports are used to connect one computer to another.

Each computer:

  • is assigned a unique three-digit ID

  • has three ports, each identified by an integer value

  • is connected to between one and three other computers.

Messages are sent between computers as a string of characters organised into fields as shown:

<STX><DestinationID><SourceID><Data><ETX>

Field name

Description

STX

a single character marking the start of the message (ASCII value 02)

DestinationID

three numeric characters identifying the destination computer

SourceID

three numeric characters identifying the source computer

Data

a variable length string containing the data being sent (Minimum length is 1 character)

ETX

a single character marking the end of the message (ASCII value 03)

For example, the following message contains the data "Hello Jack" being sent from computer "202" to computer "454":

<STX> "454202Hello Jack" <ETX>

Each computer will run a copy of the same program. Each program will contain a global variable MyID of type string which contains the unique ID of the computer in which the program is running.

The first two program modules are defined as follows:

Module

Description

GetData() (already written)

  • returns the data field from a message that has been received

  • If no message is available, the module waits until one has been received.

ReceiveFile()

  • takes a file name as a parameter of type string

  • creates a text file with the given file name (no checking required)

  • writes the data field returned by GetData() to the file

  • repeats until the data field is "****", which is not written to the file

  • outputs a final message giving the total number of characters written to the file, for example:

    132456 characters were written to newfile.txt

Write pseudocode for module ReceiveFile().

Module GetData() has already been written and must be used.

10b3 marks

The use of the string "****" as explained in the module description for ReceiveFile() may cause a problem.

Explain the problem and suggest a solution.

Problem .....................................................

Solution ........................................................

11a5 marks

A pseudocode algorithm finds a customer account record in a random file and outputs it. The records are stored using the user-defined data type TAccount.

TYPE TAccount

DECLARE AccountNumber : INTEGER

DECLARE LastName : STRING

DECLARE FirstName : STRING

DECLARE Address : STRING

DECLARE ContactNumber : STRING

ENDTYPE

Complete the file handling pseudocode.

The function Hash() takes the customer account number as a parameter, calculates and returns the hash value.

DECLARE Customer : TAccount

DECLARE Location : INTEGER

DECLARE AccountFile : STRING

.................................................................................................. leftwards arrow"AccountRecords.dat"

............................................................... AccountFile ..........................................................

OUTPUT "Please enter an account number"

INPUT Customer.AccountNumber

Location leftwards arrow Hash( .............................................................................................................. )

SEEK ..................................................................................................................... , Location

............................................................. AccountFile, ..........................................................

OUTPUT Customer // output customer record

CLOSEFILE AccountFile

11b1 mark

Define the term exception handling.

11c2 marks

State two possible causes of an exception.

12a2 marks

Describe, with an example, what is meant by an exception.

12b7 marks

A pseudocode algorithm searches for a customer record in a random file AccountRecord.dat. A user inputs the name of the customer.

The records are stored using the user‑defined data type TAccount.

TYPE TAccount

DECLARE AccountNumber : INTEGER

DECLARE Name : STRING

DECLARE Address : STRING

DECLARE Telephone : STRING

ENDTYPE

If the record is found, it is output, otherwise an error message is displayed.

Complete the file handling pseudocode.

DECLARE Customer : TAccount

DECLARE Location : INTEGER

DECLARE MaxSize : INTEGER

DECLARE FoundFlag : BOOLEAN

DECLARE SearchCustomer : STRING

MaxSize leftwards arrow1000

OPENFILE ...............................................................................................................

Location leftwards arrow 1

............................................................................................................... leftwards arrow FALSE

OUTPUT "Enter the customer’s name"

...............................................................................................................

................................................................................... AND Location <= MaxSize

................................... "AccountRecord.dat", ............................................

GETRECORD "AccountRecord.dat", Customer

IF SearchCustomer = Customer.Name THEN

OUTPUT "Customer found: "

OUTPUT Customer // output customer record

FoundFlag leftwards arrow TRUE

ENDIF

Location leftwards arrow Location + 1

ENDWHILE

IF NOT FoundFlag THEN

OUTPUT ".........................................................................................................."

ENDIF