Level:
Correct: 0
Incorrect: 0
Marks: 0/0
Exam Practice
Course:
Topics:
OCR GCSE Computer Science (Paper 2) Smart practice A mixed round aimed at your weak spots
Unit 1
Algorithms
Unit 2
Programming fundamentals
Unit 3
Producing robust programs
Unit 4
Boolean logic
Unit 5
Programming languages and IDEs
Unit 6
Practical programming (Section B)
Avg
score:
–
Topics
done:
0/31

String manipulation

Practise 33 exam-style questions on String manipulation for OCR GCSE Computer Science (Paper 2). Write your answer as you would in the exam and get it marked in seconds against the mark scheme, point by point, with the Study Helper on hand if you get stuck.

  • 33 questions
  • 1–7 marks each
  • Instant marking
  • No sign-up needed
20 questions, marked as you go

Sample questions with model answers

Here are 4 of the questions in this topic, each with a model answer, an explanation of why it earns the marks and the mark scheme an examiner would use.

Question 1

1 mark

A UK postcode, such as "LS6 2QT" or "B1 1AA", always ends with a space followed by a three-character inward code ("2QT" and "1AA" in these examples).

A postcode is stored in the variable postcode. Write an expression that returns its inward code.

Model answer

postcode.right(3)

Explanation

The inward code is always the last three characters, whatever the length of the postcode, so right(3) returns it directly. Using substring with a fixed start position would fail because postcodes are 6, 7 or 8 characters long. The same idea in Python is postcode[-3:].

Mark scheme
  1. 1 mark

    postcode.right(3)

    Accept postcode.substring(postcode.length - 3, 3), or Python postcode[-3:] or postcode[len(postcode) - 3:]. Do not accept a fixed start such as postcode.substring(4, 3), which only works for postcodes of one particular length.

Question 2

2 marks

Dates are stored as strings in the format YYYY-MM-DD, for example "2026-03-07" for 7 March 2026.

The variable today stores a date in this format. Write one statement that stores the month from today as an integer in a variable called month (for "2026-03-07", month would store 3).

Model answer

month = int(today.substring(5, 2))

Explanation

Counting from 0, the characters of "2026-03-07" are the year at indexes 0 to 3, a hyphen at 4, and the month at indexes 5 and 6. substring(5, 2) therefore returns "03", and int() casts it to the integer 3 so it can be used in calculations or comparisons. Without the cast, month would hold the string "03".

Mark scheme
  1. 1 mark

    extracts the two month characters: today.substring(5, 2)

    Accept Python today[5:7]. Do not accept substring(6, 2) (counting from 1) or substring(5, 7) (treating the second value as an end position).

  2. 1 mark

    casts the extracted characters to an integer with int(...) and assigns the result to month

    Full answer: month = int(today.substring(5, 2)). Award this mark if int() is applied to an extracted part of today and stored in month, even if the substring is wrong.

Question 3

4 marks

In ASCII, the lower-case letters "a" to "z" have the codes 97 to 122, and the capital letters "A" to "Z" have the codes 65 to 90.

A function called makeUpper takes a single character as a parameter. If the character is a lower-case letter, the function returns the capital version of that letter; otherwise it returns the character unchanged. The function must use ASC and CHR, not .upper.

Write the function makeUpper using OCR Exam Reference Language or a high-level programming language.

Model answer

function makeUpper(ch)
    code = ASC(ch)
    if code >= 97 AND code <= 122 then
        return CHR(code - 32)
    else
        return ch
    endif
endfunction

Explanation

Each lower-case letter's code is exactly 32 more than its capital (97 - 65 = 32), so subtracting 32 and using CHR gives the capital letter. The range check matters: without it, characters such as digits or punctuation would also be shifted and turned into the wrong symbols. In Python the same function is: def makeUpper(ch): with ord(ch) and chr(ord(ch) - 32).

Mark scheme
  1. 1 mark

    defines a function called makeUpper that takes one parameter

    Answers must be in OCR Exam Reference Language or a high-level programming language (e.g. Python); pseudocode, natural English or bullet points score 0. Ignore minor syntax errors. Max 4 marks from 5 mark points.

  2. 1 mark

    finds the ASCII code of the character using ASC(...)

    Accept Python ord(ch).

  3. 1 mark

    checks that the code is in the lower-case range 97 to 122 inclusive

    Accept >= 97 AND <= 122, > 96 AND < 123, or ch >= "a" AND ch <= "z". Both ends of the range must be checked.

  4. 1 mark

    subtracts 32 from the code and converts it back to a character with CHR(...)

    Accept CHR(ASC(ch) - 32), CHR(code - (97 - 65)), Python chr(ord(ch) - 32).

  5. 1 mark

    returns the converted letter for a lower-case letter, and returns the original character otherwise

    Both cases must return a value.

Question 4

6 marks

A palindrome is a word that reads the same forwards and backwards, such as "level" or "noon".

Write a program that:

- asks the user to enter a word
- checks whether the word is a palindrome, ignoring the difference between upper and lower case (so "Level" counts as a palindrome)
- outputs "Palindrome" or "Not a palindrome".

Write the program using OCR Exam Reference Language or a high-level programming language.

Model answer

word = input("Enter a word")
word = word.lower
backwards = ""
for i = word.length - 1 to 0 step -1
    backwards = backwards + word.substring(i, 1)
next i
if word == backwards then
    print("Palindrome")
else
    print("Not a palindrome")
endif

Explanation

Converting to lower case first means "Level" becomes "level", so case no longer matters. The loop builds a reversed copy by reading from the last index down to 0; if the copy equals the original, the word is a palindrome. A common error is to convert only one of the two strings to lower case, so "Level" is compared with "leveL" and wrongly rejected. In Python the check can be written as word == word[::-1].

Mark scheme
  1. 1 mark

    inputs a word

    Answers must be in OCR Exam Reference Language or a high-level programming language (e.g. Python); pseudocode, natural English or bullet points score 0. Ignore minor syntax errors.

  2. 1 mark

    converts the word to one case (.lower or .upper) before checking it

    Accept converting both the word and its reversed copy. Python .lower() / .upper().

  3. 1 mark

    uses a loop through the characters of the word

    Accept a loop over all characters or over the first half. Python slicing word[::-1] with no loop also earns this mark and the next one.

  4. 1 mark

    builds the reversed word correctly, or compares the character at index i with the character at index word.length - 1 - i

    Indexes must be correct (no off-by-one).

  5. 1 mark

    compares the reversed word with the case-converted word, or checks that every pair matches (e.g. using a Boolean flag)

    For the pairs method, a flag must stay False once any pair differs.

  6. 1 mark

    outputs "Palindrome" when it is one and "Not a palindrome" otherwise

    Must use selection so only one message is output; do not award if a message is output on every pass of the loop.

Ready to try the rest? Every answer you write is marked against the mark scheme, so you can see exactly where the marks are.

Study Helper Hints first, answers after you try

Stuck on this one? Ask for a hint and I’ll point you in the right direction, without giving the answer away. Once you’ve checked your answer, I’ll show you how the marks are awarded.