Worked solution from the Programming Paper 2 Algorithms, Programming and Logic booklet, Part 3 of 3.
Think about what a file does that variables in memory cannot. Write any two of these:
A file gives a permanent copy of the data, so it is not lost when the program ends or the power is off [1]
The data can be used again by the same program or by another program [1]
Or: the file can be copied or transferred to another computer (also accepted, but only two marks in total)
Write any two of the points above.
Declare a string, open the file for reading, read the line into the string, then output it in upper case with its length.
DECLARE Words : STRING OPENFILE Quotation.txt FOR READ READFILE Quotation.txt, Words OUTPUT UCASE(Words), LENGTH(Words) CLOSEFILE Quotation.txt
Declaration of a string variable [1]
OPENFILE with the file name and FOR READ [1]
READFILE into the string variable [1]
UCASE and LENGTH used and both output [1]
Closing the file with CLOSEFILE also earns a mark, but the maximum is four.
▶ Whole paper
Video solution of the whole paper · by ZAINEMATICS. 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.