ExamBank Scot · Higher Computing Science question bank · All Higher Computing Science questions
3 marks · Constructs and reading code
Complete line 249 in the space below. Explain why this version of the function is inefficient. A function is used to ensure that a user’s input for their date of birth contains exactly six numbers. For example, someone with a date of birth of 31st May 2003 would be entered as 310503. The first version of the code for this function is shown below. … Line 235 FUNCTION validateDob (STRING dob) RETURN BOOLEAN Line 236 DECLARE valid INITIALLY false Line 237 DECLARE lengthCheck INITIALLY false Line 238 DECLARE countValid INITIALLY 0 Line 239 DECLARE thisValue INITIALLY 0 Line 240 IF length(dob) = 6 THEN Line 241 SET lengthCheck TO true Line 242 END IF Line 243 FOR EACH character FROM dob Line 244 SET thisValue TO <ASCII value of character> Line 245 IF thisValue >= 48 AND thisValue <=57 THEN Line 246 SET countValid TO countValid + 1 Line 247 END IF Line 248 END FOR EACH Line 249 IF _____________ AND _____________ THEN Line 250 SET valid TO true Line 251 END IF Line 252 RETURN valid Line 253 END FUNCTION
• Condition for length of string • Condition for count of valid characters 1 mark for each bullet Example answers IF lengthCheck =true AND countValid=6 THEN IF length(dob)=6 and countValid=6 THEN • checks characters even if the length <>6 • keeps checking characters even when an invalid character is found 1 mark for any one bullet Do not accept generic answers relating to efficiency. Answer must be in context and refer to the code.