First character at position zero or one

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

This First character at position zero or one worksheet is part of the IGCSE Computer Science 0478 — 8.1a Data Types and Variables journey. Print it as a booklet?

First character at position zero or one

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. In Cambridge pseudocode, what does SUBSTRING("Computer", 1, 1) return?
A "C" (position 1 is the first character)
B "o" (position 1 is the second character)
2. Which statement matches the syllabus examples for SUBSTRING?
A The first character of a string is at position 1
B The first character of a string is at position 0
3. Counting from 1, what does SUBSTRING("Python", 2, 3) return?
A "yth"
B "tho"
4. A language counts positions from 0. What does the same call, SUBSTRING("Python", 2, 3), return there?
A "yth"
B "tho"
5. Which call gets the first letter of Word in Cambridge pseudocode?
A SUBSTRING(Word, 0, 1)
B SUBSTRING(Word, 1, 1)

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1SUBSTRING(Word, 1, 1) ASUBSTRING(Word, 1, 3)
2Word[0] BReturns the first character of Word, because Cambridge pseudocode numbers the first character as position 1
3With one-based numbering, the first three letters of Word start at position 1 CStarting at 0 is wrong in Cambridge pseudocode, where the first character is position 1. The start should be 1.
4The same call returns different characters under each numbering rule. With Word ← "CAT", this call gives "C" if counting from 1 but "A" if counting from 0 DGets the first character in a language that counts positions from 0, such as Python
5FirstLetter ← SUBSTRING(Name, 0, 1) EThe answer does not state whether positions start at 0 or 1, so the result is ambiguous. State the numbering used in an exam answer.
6// Takes the 2nd letter: SUBSTRING(Word, 2, 1) with no numbering stated FSUBSTRING(Word, 2, 1)

Definitions

SUBSTRING(Word, 1, 1)

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
First ← SUBSTRING(Word, _, 1)
2.
Word ← "CODE" OUTPUT _________(Word, 1, 2)
3.
Word ← "CODE" Second ← SUBSTRING(Word, _, 1)
4.
Word ← "CODE" Last ← SUBSTRING(Word, _, 1)
5.
// Python counts from _, Cambridge from 1

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A DECLARE Word : STRING
B DECLARE First : CHAR
C Word ← "Cambridge"
D First ← SUBSTRING(Word, 0, 1)
E OUTPUT First
Question 2
A Word ← "Python"
B Letter ← SUBSTRING(Word, 2, 1)
C OUTPUT Letter
D // outputs P
Question 3
A Name ← "Zoe"
B Initial ← SUBSTRING(1, Name, 1)
C OUTPUT Initial
Question 4
A // 0 is the first character
B Word ← "Hello"
C Second ← SUBSTRING(Word, 2, 1)
D OUTPUT Second
Question 5
A Word ← "Dog"
B Last ← SUBSTRING(Word, 2, 1)
C OUTPUT Last
D // outputs g

Answer Key

First character at position zero or one
Section A: Quick Questions
Q1
A — "C" (position 1 is the first character)
Q2
A — The first character of a string is at position 1
Q3
A — "yth"
Q4
B — "tho"
Q5
B — SUBSTRING(Word, 1, 1)
Section B: Match the Pairs
Q1
SUBSTRING(Word, 1, 1) → Returns the first character of Word, because Cambridge pseudocode numbers the first character as position 1
Q2
Word[0] → Gets the first character in a language that counts positions from 0, such as Python
Q3
With one-based numbering, the first three letters of Word start at position 1 → SUBSTRING(Word, 1, 3)
Q4
The same call returns different characters under each numbering rule. With Word ← "CAT", this call gives "C" if counting from 1 but "A" if counting from 0 → SUBSTRING(Word, 2, 1)
Q5
FirstLetter ← SUBSTRING(Name, 0, 1) → Starting at 0 is wrong in Cambridge pseudocode, where the first character is position 1. The start should be 1.
Q6
// Takes the 2nd letter: SUBSTRING(Word, 2, 1) with no numbering stated → The answer does not state whether positions start at 0 or 1, so the result is ambiguous. State the numbering used in an exam answer.
Section C: Definitions
Q1
SUBSTRING(Word, 1, 1) — Position 1 is the first character in Cambridge pseudocode, so this returns the first letter. Some languages count from 0 instead, so state which numbering you use.
Section D: Fill in the Blanks
Q1
First ← SUBSTRING(Word, 1, 1)
Q2
Word ← "CODE" OUTPUT SUBSTRING(Word, 1, 2)
Q3
Word ← "CODE" Second ← SUBSTRING(Word, 2, 1)
Q4
Word ← "CODE" Last ← SUBSTRING(Word, 4, 1)
Q5
// Python counts from 0, Cambridge from 1
Section E: Spot the Error
Q1
D — Cambridge pseudocode numbers the first character as position 1, so position 0 does not exist. The start position should be 1 to get the first letter.
Q2
B — Position 2 is the second character, so this would output y, not P. To get the first letter the start position must be 1.
Q3
B — The arguments are in the wrong order. SUBSTRING takes the string first, then the start position (1 for the first character), then the length.
Q4
A — The comment states the wrong numbering. In Cambridge pseudocode the first character is position 1, which is why position 2 gives the second letter, e.
Q5
B — Counting from 0, position 2 would be the last letter, but Cambridge pseudocode counts from 1. Dog has 3 characters, so the last one is at position 3. Position 2 gives o.