ExamBank Scot · Higher Computing Science question bank · All Higher Computing Science questions

Higher Computing Science 2024 Question 6

4 marks · Pre-defined functions

Using a programming language of your choice, write the code for line 7. Using a programming language of your choice, write the code for line 43. A prime number is only divisible by one and itself. The first five prime numbers are shown below: 2, 3, 5, 7, 11, . . . The code below checks if a number is prime. Line 1 FUNCTION checkPrime(INTEGER n) RETURNS BOOLEAN Line 2 DECLARE validPrime INITIALLY TRUE Line 3 IF n < 2 THEN Line 4 SET validPrime TO FALSE Line 5 ELSE Line 6 FOR divisor FROM 2 TO (n−1) DO Line 7 IF <the remainder of n divided by divisor is equal to 0> THEN Line 8 SET validPrime TO FALSE Line 9 END IF Line 10 END FOR Line 11 RETURN validPrime Line 12 END FUNCTION … Line 42 DECLARE inputNum AS INTEGER INITIALLY FROM KEYBOARD Line 43 <set isPrime by calling the function checkPrime to identify if inputNum is prime or not> Line 44 IF isPrime = TRUE THEN Line 45 SEND inputNum & " is prime." TO DISPLAY Line 46 ELSE Line 47 SEND inputNum & " is not prime." TO DISPLAY Line 48 END IF

Marking scheme

• Use of mod function/operator • Arguments (n, divisor) in order with comparison to 0 Example answers: n Mod divisor =0 Python (and Java) accept single = n % divisor == 0 Accept single = symbol VB n Mod divisor = 0 • Use of function name with actual parameter inputNum • Assignment to isPrime Example answers: SET isPrime TO checkPrime(inputNum) isPrime=checkPrime(inputNum)

Previous question: Higher Computing Science 2024 Question 5

Next question: Higher Computing Science 2024 Question 7