Matching the variable's data type to the file data

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

This Matching the variable's data type to the file data worksheet is part of the IGCSE Computer Science 0478 — 8.3 File Handling journey. Print it as a booklet?

Matching the variable's data type to the file data

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which declaration suits a variable that will hold a line of text read from a file?
A DECLARE LineOfText : INTEGER
B DECLARE LineOfText : STRING
2. A file stores the whole number 42. Which declaration suits the variable used with READFILE?
A DECLARE Score : INTEGER
B DECLARE Score : BOOLEAN
3. Which statement about the variable used with READFILE is correct?
A Its data type should suit the data in the file
B Its data type does not matter, any type works
4. A student wants to write the line "Hello class" to a file. Which variable type should they declare?
A STRING
B INTEGER
5. What is the problem with this code? DECLARE Age : STRING then WRITEFILE "Ages.txt", Age, when Age should be a whole number.
A WRITEFILE can never be used with a variable
B The type should be INTEGER to suit a whole number

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1DECLARE LineOfText : STRING ALine is meant to hold a line of text but is declared as INTEGER, so the text cannot be stored. It should be declared as STRING.
2DECLARE Score : INTEGER BDECLARE Total : INTEGER WRITEFILE "total.txt", Total
3Choose the type when you declare the variable: a line of text needs a STRING variable before it is used with READFILE CCreates a variable that can hold a whole number stored in a file
4A whole number written to a file should be held in an INTEGER variable DAge holds a whole number but is declared as STRING, so using it in arithmetic causes an error. It should be declared as INTEGER.
5DECLARE Age : STRING READFILE "ages.txt", Age Total ← Age + 1 EDECLARE Name : STRING READFILE "names.txt", Name
6DECLARE Line : INTEGER READFILE "poem.txt", Line FCreates a variable that can hold a line of text read from a file or written to a file

Definitions

DECLARE LineOfText : STRING

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
DECLARE LineOfText : ______ READFILE "story.txt", LineOfText
2.
DECLARE Score : _______ READFILE "scores.txt", Score
3.
DECLARE Mark : INTEGER READFILE "marks.txt", ____
4.
DECLARE Total : _______ Total ← 250 WRITEFILE "total.txt", Total
5.
DECLARE Line : STRING OPENFILE "poem.txt" FOR READ ________ "poem.txt", Line

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A DECLARE Total : INTEGER
B OPENFILE "total.txt" FOR READ
C READFILE "total.txt", Total
D Total ← Total + "10"
E CLOSEFILE "total.txt"
Question 2
A DECLARE Score : INTEGER
B OPENFILE "score.txt" FOR WRITE
C Score ← "eighty-five"
D WRITEFILE "score.txt", Score
E CLOSEFILE "score.txt"
Question 3
A DECLARE Name : STRING
B DECLARE Age : STRING
C OPENFILE "pupil.txt" FOR READ
D READFILE "pupil.txt", Name
E READFILE "pupil.txt", Age
F OUTPUT Age + 1
Question 4
A DECLARE Total : INTEGER
B DECLARE Sentence : TEXT
C OPENFILE "story.txt" FOR READ
D READFILE "story.txt", Sentence
E Total ← LENGTH(Sentence)
F OUTPUT Total
Question 5
A DECLARE Message : STRING
B OPENFILE "msg.txt" FOR WRITE
C Message ← 100
D WRITEFILE "msg.txt", Message
E CLOSEFILE "msg.txt"

Answer Key

Matching the variable's data type to the file data
Section A: Quick Questions
Q1
B — DECLARE LineOfText : STRING
Q2
A — DECLARE Score : INTEGER
Q3
A — Its data type should suit the data in the file
Q4
A — STRING
Q5
B — The type should be INTEGER to suit a whole number
Section B: Match the Pairs
Q1
DECLARE LineOfText : STRING → Creates a variable that can hold a line of text read from a file or written to a file
Q2
DECLARE Score : INTEGER → Creates a variable that can hold a whole number stored in a file
Q3
Choose the type when you declare the variable: a line of text needs a STRING variable before it is used with READFILE → DECLARE Name : STRING READFILE "names.txt", Name
Q4
A whole number written to a file should be held in an INTEGER variable → DECLARE Total : INTEGER WRITEFILE "total.txt", Total
Q5
DECLARE Age : STRING READFILE "ages.txt", Age Total ← Age + 1 → Age holds a whole number but is declared as STRING, so using it in arithmetic causes an error. It should be declared as INTEGER.
Q6
DECLARE Line : INTEGER READFILE "poem.txt", Line → Line is meant to hold a line of text but is declared as INTEGER, so the text cannot be stored. It should be declared as STRING.
Section C: Definitions
Q1
DECLARE LineOfText : STRING — The variable used with READFILE or WRITEFILE needs a data type that suits the data: STRING for a line of text, INTEGER for a whole number. A wrong type causes errors.
Section D: Fill in the Blanks
Q1
DECLARE LineOfText : STRING READFILE "story.txt", LineOfText
Q2
DECLARE Score : INTEGER READFILE "scores.txt", Score
Q3
DECLARE Mark : INTEGER READFILE "marks.txt", Mark
Q4
DECLARE Total : INTEGER Total ← 250 WRITEFILE "total.txt", Total
Q5
DECLARE Line : STRING OPENFILE "poem.txt" FOR READ READFILE "poem.txt", Line
Section E: Spot the Error
Q1
D — Total is an INTEGER read from the file, so you can only add a whole number to it. "10" is a string. It should be Total ← Total + 10.
Q2
C — Score is declared as INTEGER, so it must hold a whole number. A string cannot be stored in it. It should be Score ← 85.
Q3
B — Age holds a whole number and is used in the sum Age + 1, so it must be declared as INTEGER, not STRING. It should be DECLARE Age : INTEGER.
Q4
B — TEXT is not a data type in 0478 pseudocode. A line of text read from a file needs the STRING type. It should be DECLARE Sentence : STRING.
Q5
C — Message is a STRING variable for a line of text, so it cannot be given a whole number. It should be Message ← "Hello".