Question bank › GCSE › GCSE Computer Science › Programming

String handling & conversions

10 questions47 marksabout 55 min

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

1Grade 6–75 marks

The string handling functions below are used in AQA pseudocode. Characters in a string are indexed from 0.

word ← 'COMPUTER'
a ← LEN(word)
b ← SUBSTRING(2, 4, word)
c ← POSITION(word, 'U')
d ← CHAR_TO_CODE('C')
e ← CODE_TO_CHAR(d + 2)

State the value stored in each variable after the code has run. Assume the character set used is ASCII, where 'A' is 65. [5]

2Grade 1–33 marks

A program stores a customer's name in two variables.

first ← 'Jo'
last ← 'Kaur'
full ← first + ' ' + last
OUTPUT full
OUTPUT LEN(full)

(a) State the first line of output. [1]

(b) State the second line of output. [1]

(c) The + operator is used in the third line. State the name of the string operation that + performs here. [1]

3Grade 1–34 marks

A program is meant to add two numbers typed in by the user. It is written in AQA pseudocode.

a ← USERINPUT
b ← USERINPUT
OUTPUT a + b

The user types 7 and then 3.

(a) State the output of the program. [1]

(b) Explain why the program does not output 10. [2]

(c) Rewrite the first line so that the program works correctly. [1]

Stuck on String handling & conversions?

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

The algorithm below is written in AQA pseudocode. String positions start at 0.

word ← 'LEAF'
result ← ''
FOR i ← LEN(word) - 1 TO 0 STEP -1
   result ← result + SUBSTRING(i, i, word)
ENDFOR
OUTPUT result

(a) State the value of SUBSTRING(0, 0, word). [1]

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

(c) Describe the purpose of this algorithm. [1]

(d) State the output if the FOR loop were changed to FOR i ← 0 TO LEN(word) - 1. [1]

5Grade 4–55 marks

A simple cipher shifts every letter of a message three places forward in the ASCII character set. Part of the ASCII table is shown.

CharacterADHKTWZ
Code65687275848790

The program uses this line to encrypt one character stored in ch:

newCh ← CODE_TO_CHAR(CHAR_TO_CODE(ch) + 3)

(a) State the value of CHAR_TO_CODE('H'). [1]

(b) State the encrypted version of the message HAT. [2]

(c) Explain why this line of code would not produce a letter when ch is 'Z'. [2]

6Grade 4–54 marks

The subroutine below should return the number of vowels (A, E, I, O, U) in a string of capital letters. Two parts of the subroutine are missing.

SUBROUTINE countVowels(text)
   count ← 0
   FOR i ← 0 TO ..........(1)..........
      ch ← ..........(2)..........
      IF ch = 'A' OR ch = 'E' OR ch = 'I' OR ch = 'O' OR ch = 'U' THEN
         count ← count + 1
      ENDIF
   ENDFOR
   RETURN count
ENDSUBROUTINE

(a) Complete the code at (1). [1]

(b) Complete the code at (2). [1]

(c) State the value returned by countVowels('AUDIO'). [1]

(d) State the value returned by countVowels('RHYTHM'). [1]

7Grade 6–75 marks

A shop's till program asks for the price of an item and the quantity bought, then outputs the total cost. Line numbers are shown but are not part of the program.

1  OUTPUT 'Enter price'
2  price ← STRING_TO_REAL(USERINPUT)
3  OUTPUT 'Enter quantity'
4  quantity ← STRING_TO_INT(USERINPUT)
5  total ← price * quantity
6  OUTPUT 'Total: ' + total

The user enters 4.50 and then 3.

(a) State the data type of price after line 2 has run. [1]

(b) Explain why STRING_TO_INT would not be a suitable conversion to use on line 2. [1]

(c) Line 6 causes an error when the program is run. Explain why, and rewrite line 6 so that it works. [2]

(d) State the value of total that is output once the program is corrected. [1]

8Grade 6–76 marks

A program needs a subroutine initials that takes a full name typed in lower case as a parameter, for example 'ada lovelace', and returns the initials in capital letters, for example 'AL'.

You can assume the name always contains exactly one space, with the first name before it and the surname after it.

In the ASCII character set, the code for a lower-case letter is 32 more than the code for the matching capital letter (for example 'a' = 97 and 'A' = 65).

Write the subroutine initials in AQA pseudocode or a high-level language. Your subroutine must convert the letters to capitals using CHAR_TO_CODE and CODE_TO_CHAR (or the equivalent functions in your chosen language), not a built-in upper-case function. [6]

9Grade 6–75 marks

The algorithm below converts a string of digits into an integer without using STRING_TO_INT. In ASCII, the character '0' has code 48, '1' has code 49, and so on up to '9' with code 57.

digits ← '407'
total ← 0
FOR i ← 0 TO LEN(digits) - 1
   d ← CHAR_TO_CODE(SUBSTRING(i, i, digits)) - 48
   total ← total * 10 + d
ENDFOR
OUTPUT total

(a) Complete the trace table for the algorithm. [3]

idtotal
0
1
2

(b) Explain why 48 is subtracted on the fourth line. [2]

10Grade 8–96 marks

A programmer writes a subroutine to convert a binary number, stored as a string, into denary.

SUBROUTINE binToDen(bits)
   value ← 0
   FOR i ← 1 TO LEN(bits) - 1
      value ← value * 2 + STRING_TO_INT(SUBSTRING(i, i, bits))
   ENDFOR
   RETURN value
ENDSUBROUTINE

(a) State the value returned by binToDen('1011') as the subroutine is written. [1]

(b) The subroutine contains a logic error. Identify the error and state how to correct it. [2]

(c) Explain why STRING_TO_INT is needed on the fourth line. [2]

(d) The programmer wants the subroutine to return -1 if bits contains any character other than '0' or '1'. Describe the check that should be added inside the loop. [1]

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