LENGTH function

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

LENGTH function

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What does LENGTH("Happy Days") return?
A 9
B 10
2. What does the LENGTH function return?
A The number of characters in a string, as an integer
B The first character of a string, as a CHAR
3. Which is the correct way to write the function call in Cambridge pseudocode?
A Password.LENGTH
B LENGTH(Password)
4. What does LENGTH("Hi there") return?
A 8
B 7
5. Which check would normally use the LENGTH function?
A Length check
B Presence check

Match the Pairs

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

Left columnRight column
1LENGTH("Happy Days") AA length check that is true only when the password has at least 8 characters
2LENGTH(Password) >= 8 BThe comment is wrong because spaces count as characters. LENGTH("Hi there") returns 8, not 7.
3Store the number of characters in a name in a variable CNameSize ← LENGTH(Name)
4Reject a PIN that does not have exactly 4 characters DReturns 10, because the space between the two words counts as a character
5Size ← LENGTH "Cat" EThe string is not inside brackets. LENGTH is a function, so its argument must be in brackets: LENGTH("Cat").
6// LENGTH("Hi there") returns 7 OUTPUT LENGTH("Hi there") FIF LENGTH(PIN) <> 4 THEN

Definitions

LENGTH(<string>)

Fill in the Blanks

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

1.
OUTPUT ______("Happy Days")
2.
DECLARE Name : STRING INPUT Name Size ← LENGTH(____)
3.
IF LENGTH(Password) __ 8 THEN
4.
OUTPUT LENGTH("Happy Days") // shows __
5.
IF LENGTH(Code) <> 4 THEN OUTPUT "Wrong length" _____

Spot the Error

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

Question 1
A DECLARE Word : STRING
B Word ← "Happy Days"
C OUTPUT LEN(Word)
Question 2
A DECLARE Size : INTEGER
B Word ← "Happy Days"
C Size ← LENGTH Word
D OUTPUT Size
Question 3
A INPUT Password
B Size ← LENGTH("Password")
C IF Size ≥ 8 THEN
D OUTPUT "Accepted"
E ENDIF
Question 4
A DECLARE Count : STRING
B Count ← LENGTH("Happy Days")
C OUTPUT Count
Question 5
A INPUT Code
B Size ← LENGTH(Code)
C IF Size = 5 THEN
D OUTPUT "Wrong length"
E ENDIF

Answer Key

LENGTH function
Section A: Quick Questions
Q1
B — 10
Q2
A — The number of characters in a string, as an integer
Q3
B — LENGTH(Password)
Q4
A — 8
Q5
A — Length check
Section B: Match the Pairs
Q1
LENGTH("Happy Days") → Returns 10, because the space between the two words counts as a character
Q2
LENGTH(Password) >= 8 → A length check that is true only when the password has at least 8 characters
Q3
Store the number of characters in a name in a variable → NameSize ← LENGTH(Name)
Q4
Reject a PIN that does not have exactly 4 characters → IF LENGTH(PIN) <> 4 THEN
Q5
Size ← LENGTH "Cat" → The string is not inside brackets. LENGTH is a function, so its argument must be in brackets: LENGTH("Cat").
Q6
// LENGTH("Hi there") returns 7 OUTPUT LENGTH("Hi there") → The comment is wrong because spaces count as characters. LENGTH("Hi there") returns 8, not 7.
Section C: Definitions
Q1
LENGTH(<string>) — Returns the number of characters in a string as an integer, counting spaces. LENGTH("Happy Days") returns 10. Used in length checks.
Section D: Fill in the Blanks
Q1
OUTPUT LENGTH("Happy Days")
Q2
DECLARE Name : STRING INPUT Name Size ← LENGTH(Name)
Q3
IF LENGTH(Password) >= 8 THEN
Q4
OUTPUT LENGTH("Happy Days") // shows 10
Q5
IF LENGTH(Code) <> 4 THEN OUTPUT "Wrong length" ENDIF
Section E: Spot the Error
Q1
C — The Cambridge pseudocode function is called LENGTH, not LEN. The line should be OUTPUT LENGTH(Word).
Q2
C — LENGTH is a function, so its argument must be inside brackets. The line should be Size ← LENGTH(Word).
Q3
B — The quotes make LENGTH measure the word Password (8 characters) instead of what the user typed. Remove the quotes: LENGTH(Password).
Q4
A — LENGTH returns a whole number, so the variable that stores it must be INTEGER, not STRING.
Q5
C — The error message would show when the length is correct. The length check should test for an invalid length: IF Size <> 5 THEN.