Question bank › GCSE › GCSE Computer Science › Programming fundamentals

String manipulation and file handling

10 questions41 marksabout 50 min

Answer on paper first, then reveal the mark scheme one mark at a time.

1Grade 4–54 marks

The variable word holds the string "Football". State the output of each line.

print(word.length)
print(word.subString(4, 3))
print(word.upper)
print(word.left(3))

[4]

2Grade 6–74 marks

A text file called scores.txt contains one player's name and score on each line.

Write code that opens the file, outputs every line in the file, then closes the file. Use OCR Exam Reference Language or a high-level programming language. [4]

3Grade 1–33 marks

A program contains the following lines.

a = "12" + "3"
b = 12 + 3
c = str(12) + "3"

(a) State the value stored in a and the value stored in b. [2]

(b) State the value stored in c and give the name of the string operation used to produce it. [1]

Stuck on String manipulation and file handling?

Get it sorted in a free 30-minute session

We work through your weakest questions on this topic on Zoom. I reply the same day.

4Grade 4–54 marks

Write an algorithm that asks the user to enter a word, then outputs the word reversed. For example, if the user enters stream the output is maerts.

You must use string manipulation. Use OCR Exam Reference Language or a high-level programming language. [4]

5Grade 4–54 marks

The algorithm below counts the vowels in a string.

01 word = "Keyboard"
02 count = 0
03 for i = 0 to word.length - 1
04     letter = word.substring(i, 1).upper
05     if letter == "A" OR letter == "E" OR letter == "I" OR letter == "O" OR letter == "U" then
06         count = count + 1
07     endif
08 next i
09 print(count)

(a) State the value of letter when i is 0 and when i is 4. [2]

(b) State the final output of the algorithm. [1]

(c) Explain why line 04 uses .upper. [1]

6Grade 4–53 marks

OCR Exam Reference Language includes the file-handling operations shown in the table.

OperationPurpose
newFile("log.txt")…
myFile.writeLine("text")…
myFile.endOfFile()…

Describe the purpose of each of the three operations. [3]

7Grade 6–75 marks

A teacher wants to save a list of three book titles to a text file.

Write an algorithm that:

  • creates a new file called books.txt and opens it
  • asks the user to enter three book titles, writing each one to the file on its own line
  • closes the file.

Use OCR Exam Reference Language or a high-level programming language. [5]

8Grade 6–74 marks

A file called sales.txt stores one whole number on each line. A student writes the following algorithm to output the total of all the numbers in the file.

01 myFile = open("sales.txt")
02 total = 0
03 while myFile.endOfFile()
04     total = total + myFile.readLine()
05     myFile.close()
06 endwhile
07 print(total)

The algorithm contains three errors.

Identify and correct two of the errors. [4]

9Grade 8–96 marks

A file called emails.txt stores one email address on each line, for example:

[email protected]
[email protected]
[email protected]
[email protected]

Write an algorithm that reads every line of emails.txt and:

  • counts how many email addresses end in .ac.uk
  • writes each of those addresses to a new file called uni.txt
  • outputs the count after all the lines have been read.

For the example file, the output would be 2 and uni.txt would contain two lines.

Use OCR Exam Reference Language or a high-level programming language. [6]

10Grade 8–94 marks

The algorithm below encrypts a message of upper-case letters. ASC() returns the ASCII code of a character and CHR() returns the character with a given ASCII code. The ASCII code of A is 65 and of Z is 90.

01 message = input("Enter message")
02 output = ""
03 for i = 0 to message.length - 1
04     code = ASC(message.substring(i, 1)) + 1
05     if code > 90 then
06         code = code - 26
07     endif
08     output = output + CHR(code)
09 next i
10 print(output)

(a) State the output when the user enters HAL. [1]

(b) State the output when the user enters ZAP. [1]

(c) Explain the purpose of lines 05 to 07. [2]

Stuck on this topic?

Book a free 30-minute session on WhatsApp and we'll find exactly which step is costing you the marks. Or build a timed paper on it.

Free 30-minute session