ExamBank Scot · Higher Computing Science question bank · All Higher Computing Science questions
3 marks · Constructs and reading code
Using a programming language of your choice, complete Line 5 below. WHILE left < right AND Using a programming language of your choice, write the missing code at Line 26 to call the function. A word is a palindrome if it reads the same backwards as forwards, for example: mum, noon, madam, kayak, racecar A program identifies if a word is a palindrome by checking if the first and last characters are the same. If they are, it then checks the second and second last characters and so on. The incomplete program is shown below. Line 1 FUNCTION checkPalindrome(STRING word) RETURNS BOOLEAN Line 2 DECLARE left INITIALLY 0 Line 3 DECLARE right INITIALLY length(word)- 1 Line 4 DECLARE validPalin INITIALLY TRUE Line 5 WHILE left < right AND ____________________________ DO Line 6 IF word[left] = word[right] THEN Line 7 SET left TO left + 1 Line 8 SET right TO right - 1 Line 9 ELSE Line 10 SET validPalin TO FALSE Line 11 END IF Line 12 END WHILE Line 13 RETURN validPalin Line 14 END FUNCTION … Line 25 RECEIVE userWord FROM KEYBOARD Line 26 _______________________________________________________ Line 27 IF palindrome = TRUE THEN Line 28 SEND userWord & " is a palindrome" TO DISPLAY Line 29 ELSE Line 30 SEND userWord & " is not a palindrome" TO DISPLAY Line 31 END IF
validPalin (= TRUE) • Function checkPalindrome called correctly with single parameter userWord • Assigning result to palindrome SQARL: SET palindrome TO checkPalindrome(userWord)