Tracing a validation algorithm

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

Tracing a validation algorithm

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A validation loop asks for a mark from 0 to 100. A student enters 150, then 85. What does the trace table show for the INPUT column?
A 150 on one row, then 85 on the next row
B Only 85, because 150 was rejected so it is not recorded
2. In a trace of a validation loop, the user enters -5 and it fails the range check. Where does the error message go?
A In the OUTPUT column on the same row as the -5 attempt
B In the INPUT column next to the -5
3. The loop is REPEAT ... UNTIL Mark >= 0 AND Mark <= 100. The inputs are 120, -3, 50. How many times does the loop body run?
A 2 times: only the rejected values make the loop run
B 3 times: two rejected values and one accepted value
4. Which set of test inputs for a trace of a range check 1 to 10 uses both normal and abnormal data?
A 3, 7, 10
B 15, 0, 7
5. When does a validation loop stop repeating?
A When the input value passes the check
B After the error message has been output once

Match the Pairs

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

Left columnRight column
1REPEAT INPUT Age UNTIL Age >= 1 AND Age <= 100 AThe error message. In the trace table it is written in the OUTPUT column on the row of each rejected value, and not on the row of the accepted value.
2OUTPUT "Invalid age" BThe rejected value -4 was left out of the trace. Every attempt must be recorded, so there should be two error messages, one for 150 and one for -4, before 35 is accepted.
3Tracing a validation loop with the inputs 150, -4, 35 for a range check 1 to 100 gives two rejected values and 35 as the value finally accepted. CWHILE Age < 1 OR Age > 100 DO OUTPUT "Invalid age" INPUT Age ENDWHILE
4In a trace table the Age column lists every attempt in order, so a row is added for each INPUT the loop asks for until a valid value is entered. DINPUT Age WHILE Age < 1 OR Age > 100 DO INPUT Age ENDWHILE
5Trace of inputs 150, -4, 35 (range 1 to 100): the Age column shows 150, 35 and the output column shows one error message. EA validation loop: it keeps asking for Age until the value is inside the range 1 to 100, so each rejected attempt gets its own row in the trace.
6Trace of inputs 150, -4, 35 (range 1 to 100): the output column shows an error message on the row where Age is 35. F35 is valid normal data, so no error message is output for it. The messages belong on the rows for 150 and -4 only, and 35 is the accepted value that ends the loop.

Definitions

Tracing a validation loop

Fill in the Blanks

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

1.
REPEAT INPUT Age _____ Age >= 1 AND Age <= 100
2.
WHILE Age < 1 __ Age > 100 DO OUTPUT "Invalid age" INPUT Age
3.
INPUT Age WHILE Age < 1 OR Age > 100 DO ______ "Invalid age"
4.
REPEAT INPUT ____ UNTIL Mark >= 0 AND Mark <= 50
5.
REPEAT INPUT Score UNTIL Score __ 0 AND Score <= 100

Spot the Error

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

Question 1
A OUTPUT "Enter age 1-100"
B OUTPUT Age
C WHILE Age < 1 OR Age > 100 DO
D OUTPUT "Invalid"
E INPUT Age
F ENDWHILE
Question 2
A REPEAT
B INPUT Pass
C IF LENGTH(Password) < 8 THEN
D OUTPUT "Too short"
E ENDIF
F UNTIL LENGTH(Password) >= 8
Question 3
A Attempts = 0
B INPUT Pin
C WHILE Pin <> 1234 DO
D Attempts ← Attempts + 1
E INPUT Pin
F ENDWHILE
Question 4
A INPUT Num
B WHILE Num < 0 AND Num > 100 DO
C OUTPUT "Invalid number"
D INPUT Num
E ENDWHILE
Question 5
A DECLARE Name : INTEGER
B INPUT Name
C WHILE LENGTH(Name) = 0 DO
D OUTPUT "Name required"
E INPUT Name
F ENDWHILE

Answer Key

Tracing a validation algorithm
Section A: Quick Questions
Q1
A — 150 on one row, then 85 on the next row
Q2
A — In the OUTPUT column on the same row as the -5 attempt
Q3
B — 3 times: two rejected values and one accepted value
Q4
B — 15, 0, 7
Q5
A — When the input value passes the check
Section B: Match the Pairs
Q1
REPEAT INPUT Age UNTIL Age >= 1 AND Age <= 100 → A validation loop: it keeps asking for Age until the value is inside the range 1 to 100, so each rejected attempt gets its own row in the trace.
Q2
OUTPUT "Invalid age" → The error message. In the trace table it is written in the OUTPUT column on the row of each rejected value, and not on the row of the accepted value.
Q3
Tracing a validation loop with the inputs 150, -4, 35 for a range check 1 to 100 gives two rejected values and 35 as the value finally accepted. → WHILE Age < 1 OR Age > 100 DO OUTPUT "Invalid age" INPUT Age ENDWHILE
Q4
In a trace table the Age column lists every attempt in order, so a row is added for each INPUT the loop asks for until a valid value is entered. → INPUT Age WHILE Age < 1 OR Age > 100 DO INPUT Age ENDWHILE
Q5
Trace of inputs 150, -4, 35 (range 1 to 100): the Age column shows 150, 35 and the output column shows one error message. → The rejected value -4 was left out of the trace. Every attempt must be recorded, so there should be two error messages, one for 150 and one for -4, before 35 is accepted.
Q6
Trace of inputs 150, -4, 35 (range 1 to 100): the output column shows an error message on the row where Age is 35. → 35 is valid normal data, so no error message is output for it. The messages belong on the rows for 150 and -4 only, and 35 is the accepted value that ends the loop.
Section C: Definitions
Q1
Tracing a validation loop — Step through a validation loop with valid and invalid inputs, recording each rejected value, the error message output and the value finally accepted in a trace table.
Section D: Fill in the Blanks
Q1
REPEAT INPUT Age UNTIL Age >= 1 AND Age <= 100
Q2
WHILE Age < 1 OR Age > 100 DO OUTPUT "Invalid age" INPUT Age
Q3
INPUT Age WHILE Age < 1 OR Age > 100 DO OUTPUT "Invalid age"
Q4
REPEAT INPUT Mark UNTIL Mark >= 0 AND Mark <= 50
Q5
REPEAT INPUT Score UNTIL Score >= 0 AND Score <= 100
Section E: Spot the Error
Q1
B — Line 2 must read the first attempt with INPUT Age. OUTPUT only displays a value, so the loop would test an Age that was never entered. The trace table's input column would stay empty.
Q2
B — The variable must be Password, the same name used in the length check and the UNTIL condition. Pass is a different variable, so the tested value never matches the value entered. It should be INPUT Password.
Q3
A — In Cambridge pseudocode = is a comparison, not assignment. The counter must be set with the arrow, so line 1 should be Attempts ← 0. Without it the trace table has no starting value to count from.
Q4
B — No number can be below 0 and above 100 at the same time, so the condition is never true. The loop would never run and invalid values would be accepted. It should use OR.
Q5
A — A name is text, so it must be declared as STRING. INTEGER holds whole numbers only and cannot be used with LENGTH. Line 1 should be DECLARE Name : STRING.