Question 1
2 marksGive two reasons why a program should close a file when it has finished using it.
Model answer
Closing the file releases it so that other programs and users can open it. It also makes sure that everything the program has written is saved to the file and frees the memory the open file was using.
Explanation
While a file is open, the operating system may lock it and keep some of the data waiting in memory before writing it to storage. Calling close() (myFile.close() in OCR Exam Reference Language) finishes the writing, frees those resources and lets other programs use the file. Closing a file does not delete it or its contents.
Mark scheme
-
1 mark
so that other programs or users can open and use the file / the file is no longer locked
Max 2 marks from 4 mark points; award one mark per different reason.
-
1 mark
makes sure all the data written to the file is saved / stops data being lost
-
1 mark
frees up memory or other resources used by the open file
-
1 mark
prevents the file being accidentally changed or corrupted later in the program
Do not accept 'to save space' or 'to delete the file'.