Length check

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

Length check

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which check makes sure a password has at least 8 characters?
A Length check
B Range check
2. Which condition is a valid length check for a password of at least 8 characters?
A LENGTH(Password) >= 8
B Password >= 8
3. A four-digit PIN must have exactly 4 characters. Which condition accepts only valid PINs?
A LENGTH(PIN) > 4
B LENGTH(PIN) = 4
4. A user enters the PIN 123. Does it pass the length check LENGTH(PIN) = 4?
A No, it has only 3 characters
B Yes, because the digits are all numbers
5. A username must be no more than 12 characters long. Which condition is correct?
A LENGTH(Username) >= 12
B LENGTH(Username) <= 12

Match the Pairs

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

Left columnRight column
1LENGTH(Password) >= 8 ALENGTH(Username) <= 12
2LENGTH(PIN) = 4 BChecks that the password has at least 8 characters (a minimum length check)
3A length check can set a maximum length, so a username must not be longer than 12 characters CThe function name is written in the wrong case. Keywords and functions must be in capital letters, so it should be LENGTH(PIN).
4A length check can set both a minimum and a maximum, so a username must be 6 to 12 characters D= tests for exactly 8 characters, so longer passwords are wrongly rejected. A minimum length needs >= 8.
5IF Length(PIN) = 4 THEN ELENGTH(Username) >= 6 AND LENGTH(Username) <= 12
6IF LENGTH(Password) = 8 THEN FChecks that the PIN has exactly 4 characters (an exact length check)

Definitions

LENGTH(Password) >= 8

Fill in the Blanks

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

1.
IF ______(Password) >= 8 THEN
2.
IF LENGTH(PIN) _ 4 THEN
3.
IF LENGTH(Password) __ 8 THEN
4.
IF LENGTH(Username) __ 12 THEN
5.
REPEAT INPUT PIN UNTIL LENGTH(PIN) = _

Spot the Error

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

Question 1
A DECLARE Password : INTEGER
B INPUT Password
C IF LENGTH(Password) >= 8 THEN
D OUTPUT "Accepted"
E ENDIF
Question 2
A INPUT PIN
B IF PIN = 4 THEN
C OUTPUT "PIN OK"
D ELSE
E OUTPUT "Must be 4 digits"
F ENDIF
Question 3
A INPUT Password
B WHILE LENGTH(Password) < 8 DO
C OUTPUT Too short
D INPUT Password
E ENDWHILE
Question 4
A INPUT Username
B IF LENGTH(Username) > 12 THEN
C PRINT "Too long"
D ELSE
E OUTPUT "Saved"
F ENDIF
Question 5
A DECLARE PIN STRING
B INPUT PIN
C IF LENGTH(PIN) = 4 THEN
D OUTPUT "Accepted"
E ENDIF

Answer Key

Length check
Section A: Quick Questions
Q1
A — Length check
Q2
A — LENGTH(Password) >= 8
Q3
B — LENGTH(PIN) = 4
Q4
A — No, it has only 3 characters
Q5
B — LENGTH(Username) <= 12
Section B: Match the Pairs
Q1
LENGTH(Password) >= 8 → Checks that the password has at least 8 characters (a minimum length check)
Q2
LENGTH(PIN) = 4 → Checks that the PIN has exactly 4 characters (an exact length check)
Q3
A length check can set a maximum length, so a username must not be longer than 12 characters → LENGTH(Username) <= 12
Q4
A length check can set both a minimum and a maximum, so a username must be 6 to 12 characters → LENGTH(Username) >= 6 AND LENGTH(Username) <= 12
Q5
IF Length(PIN) = 4 THEN → The function name is written in the wrong case. Keywords and functions must be in capital letters, so it should be LENGTH(PIN).
Q6
IF LENGTH(Password) = 8 THEN → = tests for exactly 8 characters, so longer passwords are wrongly rejected. A minimum length needs >= 8.
Section C: Definitions
Q1
LENGTH(Password) >= 8 — A length check tests the number of characters in an input is the required length or within a limit, using LENGTH, e.g. a password of at least 8 characters or a PIN of exactly 4.
Section D: Fill in the Blanks
Q1
IF LENGTH(Password) >= 8 THEN
Q2
IF LENGTH(PIN) = 4 THEN
Q3
IF LENGTH(Password) >= 8 THEN
Q4
IF LENGTH(Username) <= 12 THEN
Q5
REPEAT INPUT PIN UNTIL LENGTH(PIN) = 4
Section E: Spot the Error
Q1
A — A password is text, so it must be declared as STRING. LENGTH counts the characters in a string, so Password cannot be an INTEGER. The line should be DECLARE Password : STRING.
Q2
B — A length check must test the number of characters, not the value typed. It should be IF LENGTH(PIN) = 4 THEN, so a PIN such as 1234 passes because it has exactly 4 characters.
Q3
C — Text in an OUTPUT statement must be inside double quotes. The line should be OUTPUT "Too short".
Q4
C — Cambridge pseudocode uses OUTPUT to display a message, not PRINT. The line should be OUTPUT "Too long".
Q5
A — A DECLARE statement needs a colon between the identifier and the data type. The line should be DECLARE PIN : STRING.