Level:
Correct: 0
Incorrect: 0
Marks: 0/0
Exam Practice
Course:
Topics:
AQA GCSE Computer Science (Paper 1) Smart practice A mixed round aimed at your weak spots
Unit 1
3.1 Fundamentals of algorithms
Unit 2
3.2 Programming
Data types
Data types
Arithmetic operations in a programming language
Arithmetic operations, DIV and MOD
Relational operations in a programming language
Relational operations
Boolean operations in a programming language
Boolean operations in conditions
Data structures
Arrays and records
Input/output
Input and output
String handling operations in a programming language
String handling
Random number generation in a programming language
Random number generation
Structured programming and subroutines (procedures and functions)
Subroutines and structured programming
Avg
score:
–
Topics
done:
0/15

String handling

Practise 35 exam-style questions on String handling for AQA GCSE Computer Science (Paper 1). 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.

  • 35 questions
  • 1–8 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

The algorithm below is written in pseudo-code.

word ← USERINPUT
count ← 0
FOR ch IN word
  IF ch = 'a' OR ch = 'e' OR ch = 'i' OR ch = 'o' OR ch = 'u' THEN
    count ← count + 1
  ENDIF
ENDFOR
OUTPUT count

State the output when the user enters Aberdeen

Model answer

3

Explanation

The loop compares every character with the five lower-case vowels. In Aberdeen the three lower-case e's match, but the capital A does not, because 'A' and 'a' are different characters with different character codes. The common mistake is to answer 4 by counting the capital A.

Mark scheme
  1. 1 mark

    3

    Mark is for AO2 (apply). R. 4 (the capital A is not equal to 'a', so it is not counted). I. Quotation marks.

Question 2

2 marks

The pseudo-code below should output the last four characters of an order reference.

01orderRef ← 'UK-2026-58317'
02last4 ← SUBSTRING(LEN(orderRef) - 4, LEN(orderRef), orderRef)
03OUTPUT 'Order ending ' + last4

SUBSTRING(a, b, s) returns the characters of s from position a to position b inclusive, and strings are indexed from 0.

An error occurs when line 2 is executed. Explain why the error occurs and rewrite line 2 so that the algorithm outputs Order ending 8317

Model answer

The string has 13 characters, so its positions run from 0 to 12. The end position LEN(orderRef) is 13, which does not exist, so SUBSTRING tries to go past the end of the string.
last4 ← SUBSTRING(LEN(orderRef) - 4, LEN(orderRef) - 1, orderRef)

Explanation

Because strings are indexed from 0, a string of length 13 has positions 0 to 12, so the end position must be LEN(orderRef) - 1. The start position LEN(orderRef) - 4, which is 9, is already correct, and positions 9 to 12 hold 8317. A common mistake is to change the start position as well, which then gives the wrong characters.

Mark scheme
  1. 1 mark

    The last character is at position LEN(orderRef) - 1 / 12, so position LEN(orderRef) / 13 does not exist / is outside the string

    Mark is for AO2 (apply). A. index out of range, if linked to the end position. NE. 'the end position is wrong' with no reason.

  2. 1 mark

    last4 ← SUBSTRING(LEN(orderRef) - 4, LEN(orderRef) - 1, orderRef)

    Mark is for AO3 (refine). A. SUBSTRING(9, 12, orderRef). A. any equivalent statement that assigns 8317 to last4. A. answers that use an alternative style of pseudo-code. I. Case.

Question 3

3 marks

Write a program that:
- gets the user to enter a word
- outputs the first character of the word
- outputs the last character of the word.

You can assume that the word contains at least one character.

You should write your answer in C#, Python or VB.NET.

Model answer

word = input()
print(word[0])
print(word[len(word) - 1])

Explanation

Strings are indexed from 0, so the first character is at position 0 and the last is at position len(word) - 1; using len(word) itself goes one past the end and causes an error. Using the length rather than a fixed number means the program works for words of any length, including a single character, which is both the first and the last.

Mark scheme
  1. 1 mark

    Mark A for getting user input and storing it in a variable

    3 marks for AO3 (program).

  2. 1 mark

    Mark B for outputting the character at position 0 (the first character)

  3. 1 mark

    Mark C for outputting the character at position length - 1 (the last character), using the length of the word

    A. word[-1] in Python. R. a fixed position such as word[6]. R. word[len(word)] (one past the end). Maximum 2 marks if any errors in code. I. Case. I. Indentation. I. Messages or no messages with input statements. I. Gaps/spaces throughout the code, except where to do so would explicitly alter the logic of the code in a way that makes it incorrect. A. ( ) instead of [ ] to access a character in Python or C#.
    C#: Console.WriteLine(word[0]); Console.WriteLine(word[word.Length - 1]);
    VB.NET: Console.WriteLine(word(0)) and Console.WriteLine(word(word.Length - 1))

Question 4

5 marks

The algorithm below is written in pseudo-code. s[i] is the character at position i of s, and strings are indexed from 0.

01s ← 'AAABCC'
02result ← ''
03count ← 1
04FOR i ← 1 TO LEN(s) - 1
05  IF s[i] = s[i - 1] THEN
06    count ← count + 1
07  ELSE
08    result ← result + s[i - 1] + INT_TO_STRING(count)
09    count ← 1
10  ENDIF
11ENDFOR
12result ← result + s[LEN(s) - 1] + INT_TO_STRING(count)
13OUTPUT result

Complete the trace table shown in the figure for the algorithm. The first row has been completed for you.

Give your answer as the values in each column, in order.

Trace table with four columns: i, count, result and OUTPUT. Row 1: count = 1; i, result and OUTPUT blank. Rows 2 to 10 are empty, for the student to complete.

Model answer

i: 1, 2, 3, 4, 5
count: (1), 2, 3, 1, 1, 2
result: A3, A3B1, A3B1C2
OUTPUT: A3B1C2

Explanation

The algorithm counts runs of the same character: while each character matches the one before it, count goes up, and when it changes, the previous character and its count are added to result and count restarts at 1. The last run (CC) is still being counted when the loop ends, which is why line 12 adds C2 after the loop. Forgetting that final step, or adding s[i] instead of s[i - 1], are the common errors.

Mark scheme
  1. 1 mark

    i column: 1, 2, 3, 4, 5

    All marks are for AO2 (apply). Values must be in this order. I. Repeated values.

  2. 1 mark

    count column: 2, 3, 1 (for i = 1, 2, 3)

    Ignore the initial 1 that is already printed.

  3. 1 mark

    count column: 1, 2 (for i = 4, 5)

    I. The 1 for i = 4 being left out, as the value of count does not change.

  4. 1 mark

    result column: A3, A3B1 (inside the loop)

    I. Quotation marks.

  5. 1 mark

    result column: A3B1C2 after the loop, and OUTPUT column: A3B1C2, output once

    R. if more than one value is output. I. Quotation marks.

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.