Revision Library

Problem Solving and Programming - Worksheets, Questions and Revision

13 original exam-style questions - 10 pages of questions with a full mark scheme - free printable PDF.

Download PDFJump to mark scheme (page 11)Read the revision guide
« Previous: Elements of Computational ThinkingNext: Algorithms: Complexity, Searching and Sorting »
Revision Library
revisionlibrary.co.uk
A-Level · Computer Science - Problem Solving and Programming

A2.2 Problem Solving and Programming

OCR H446 · Calculators not allowed · about 170 minutes
Total Marks
Name: _______________________________    Date: ____ / ____ / ______
Answer ALL questions. Show all your working.
1
A local library is developing a simple program to manage overdue book fines.
(a)State the most appropriate primitive data type for storing each of the following:
(i) the number of books a member has borrowed
(ii) whether a book is currently overdue
(iii) the title of a book
(3)
(b)A variable is declared as:
DECLARE bookPrice : REAL
Explain why REAL is a more appropriate data type than INTEGER for storing the price of a book in pounds.
(2)
(c)A librarian writes the following algorithm to find the average fine charged, ignoring members with no fine:
count <- 0
total <- 0
FOR i <- 1 TO 5
    INPUT fine
    total <- total + fine
    IF fine > 0 THEN
        count <- count + 1
    ENDIF
NEXT i
average <- total / count
OUTPUT average

The five values entered for fine, in order, are: 0, 2.50, 0, 1.20, 3.30

Complete the trace table below and state the final value output for average.
ifinetotalcount
10______
22.50______
30______
41.20______
53.30______
(5)
(Total for Question 1 is 10 marks)
2
A game awards a bonus based on a player's grade, using the following CASE statement:
CASE OF grade
    "A" : bonus <- 50
    "B" : bonus <- 30
    "C" : bonus <- 10
    OTHERWISE : bonus <- 0
ENDCASE
(a)State the value assigned to bonus when grade = "B".(1)
(b)Rewrite the CASE statement above as an equivalent structure using nested IF...THEN...ELSE statements.(4)
(c)Explain one advantage of using a CASE statement rather than nested IF statements in this situation.(2)
(d)A trainee programmer writes this version of the CASE statement:
CASE OF grade
    "A" : bonus = 50
    "B" : bonus <- 30
    OTHERWISE : bonus <- 0
ENDCASE

Identify the error in this code and state how to fix it.
(2)
(Total for Question 2 is 9 marks)
3
A weather station program reads temperatures until a sentinel value of -999 is entered, and finds the maximum temperature recorded:
maxTemp <- -999
INPUT temp
WHILE temp <> -999
    IF temp > maxTemp THEN
        maxTemp <- temp
    ENDIF
    INPUT temp
ENDWHILE
OUTPUT maxTemp

The values entered, in order, are: 14, 21, 9, 18, -999
(a)Complete a trace table showing the value of maxTemp after each temperature is processed, and state the final value output.(5)
(b)Explain what would happen if the very first value entered was -999, and why the output produced would be unhelpful.(2)
(c)A programmer rewrites the loop using REPEAT...UNTIL instead of WHILE. Write a correct REPEAT...UNTIL version of this algorithm, and explain one adjustment that had to be made compared with the WHILE version.(3)
(d)State the name given to the special value (-999) used to end the input loop.(1)
(Total for Question 3 is 11 marks)
4
A teacher stores the test scores of 3 students, each having sat 4 tests, in a 2D array scores[1:3, 1:4]:

Test1 Test2 Test3 Test4
Student1: 55 62 70 48
Student2: 80 75 90 85
Student3: 40 55 60 65
(a)Write a pseudocode DECLARE statement for the 2D array scores, capable of holding this data.(2)
(b)Write an algorithm to calculate and output the average score for Student2 (row 2) only.(4)
(c)This nested loop sums every score in the array:
total <- 0
FOR row <- 1 TO 3
    FOR col <- 1 TO 4
        total <- total + scores[row, col]
    NEXT col
NEXT row
OUTPUT total

Complete the trace table below, showing the cumulative value of total after each row is fully processed, and state the final value output.

After row 1: total = ___
After row 2: total = ___
After row 3: total = ___
(4)
(d)State the value stored in scores[2, 3].(1)
(Total for Question 4 is 11 marks)
5
A stock system uses product codes in the format "AB-1234": exactly 7 characters, with the first two characters being letters, the third character a hyphen, and the last four characters digits.
(a)Write a function ValidateCode(code : STRING) RETURNS BOOLEAN that checks a product code matches this format, using string handling functions such as LEN, MID and ASC.(6)
(b)State the value returned by MID("AB-1234", 4, 4).(1)
(c)State the value returned by UCASE(LEFT("ab-1234", 2)).(1)
(d)Explain why passing the validation in part (a) does not guarantee that a product code entered is accurate.(2)
(Total for Question 5 is 10 marks)
6
A programmer writes the following subroutines and main program:
PROCEDURE DoubleValue(BYREF x : INTEGER)
    x <- x * 2
ENDPROCEDURE

FUNCTION AddFive(y : INTEGER) RETURNS INTEGER
    y <- y + 5
    RETURN y
ENDFUNCTION

num1 <- 10
num2 <- 10
DoubleValue(num1)
num2 <- AddFive(num2)
OUTPUT num1
OUTPUT num2
(a)State the values output for num1 and num2.(2)
(b)Explain the difference between passing a parameter by reference and by value, using num1 and num2 as examples.(4)
(c)Rewrite DoubleValue as a function, DoubleValueFunc, that takes its parameter by value and returns the doubled value, without modifying the original argument.(3)
(d)State one reason a programmer might choose to write a subroutine as a procedure rather than a function.(1)
(Total for Question 6 is 10 marks)
7
A sorted array holds 8 integers with indices 1 to 8:

list = [3, 8, 15, 19, 24, 31, 42, 50]

The function below performs a binary search for a target value:
FUNCTION BinarySearch(list : ARRAY[1:8] OF INTEGER, target : INTEGER) RETURNS INTEGER
    lower <- 1
    upper <- 8
    WHILE lower <= upper
        mid <- (lower + upper) DIV 2
        IF list[mid] = target THEN
            RETURN mid
        ELSE IF list[mid] < target THEN
            lower <- mid + 1
        ELSE
            upper <- mid - 1
        ENDIF
    ENDWHILE
    RETURN -1
ENDFUNCTION

BinarySearch(list, 24) is called.
(a)Complete a trace table showing the values of lower, upper, mid and list[mid] for each iteration of the WHILE loop, until the target 24 is found.(5)
(b)State the number of comparisons with list[mid] needed to locate the value 24.(1)
(c)Explain why binary search cannot reliably be used on the unsorted array [8, 3, 19, 15, 24, 31, 42, 50].(2)
(d)State the time complexity of binary search in Big O notation, and explain why it is more efficient than linear search, O(n), for very large data sets.(3)
(e)Write pseudocode for a function LinearSearch(list : ARRAY[1:8] OF INTEGER, target : INTEGER) RETURNS INTEGER that returns the index of target in list, or -1 if it is not present.(3)
(Total for Question 7 is 14 marks)
8
The array [8, 3, 6, 1] is to be sorted into ascending order using a bubble sort.
(a)Complete the trace table below, showing the state of the array after each full pass of a standard bubble sort (comparing and swapping adjacent elements left to right), and the number of swaps made in that pass.

Start: [8, 3, 6, 1]
After pass 1: [___] , swaps = ___
After pass 2: [___] , swaps = ___
After pass 3: [___] , swaps = ___
After pass 4: [___] , swaps = ___
(4)
(b)State how a bubble sort can be made more efficient by detecting that the array is already sorted, and explain how this is recognised.(2)
(c)Write a procedure BubbleSort(BYREF arr : ARRAY[1:n] OF INTEGER, n : INTEGER) that sorts arr into ascending order, including the early-exit optimisation from part (b).(6)
(d)State the worst-case time complexity of bubble sort in Big O notation.(1)
(Total for Question 8 is 13 marks)
9
A stack is implemented using an array stack[1:5] with an integer pointer top, initially 0 (empty stack).
(a)State the names of the two operations used to add data to, and remove data from, a stack.(2)
(b)The following sequence of operations is carried out on the empty stack:
Push(5), Push(9), Pop(), Push(3), Push(7), Pop()
Complete a trace table showing the value of top and the contents of the stack after each operation.
(5)
(c)Explain why a stack, rather than a queue, is an appropriate data structure for checking that brackets in an expression such as ((a+b)*(c-d)) are balanced.(3)
(d)State one real-world application of a queue in a computer system.(1)
(Total for Question 9 is 11 marks)
10
The following recursive function is defined:
FUNCTION Mystery(n : INTEGER) RETURNS INTEGER
    IF n = 1 THEN
        RETURN 1
    ELSE
        RETURN n * Mystery(n - 1)
    ENDIF
ENDFUNCTION
(a)State what Mystery(n) calculates.(1)
(b)Complete a trace of the recursive calls made, and the value returned at each stage, for the call Mystery(4).(5)
(c)Explain, using the term "base case", why calling Mystery(0) would cause a runtime error, and state the name commonly given to this type of error.(3)
(d)Rewrite Mystery as an iterative (non-recursive) function, MysteryIterative, using a FOR loop, that calculates the same result for positive integer values of n.(4)
(Total for Question 10 is 13 marks)
11
A computer system uses 8-bit binary representation for integers.
(a)Convert the denary number 179 to 8-bit binary.(2)
(b)Convert the binary number 10110011 to hexadecimal.(2)
(c)A system stores signed integers using 8-bit two's complement. State the denary value represented by 11101001.(3)
(d)Explain why overflow occurs when adding the 8-bit two's complement numbers 01111111 and 00000001, and state the effect this has on the result.(3)
(e)State the number of distinct values that can be represented using 8-bit two's complement.(1)
(Total for Question 11 is 11 marks)
12
A program is required to input 10 integer temperature readings (in degrees Celsius) and output: the number of days above 20 degrees Celsius, and whether the average of all 10 readings is a whole number.
(a)Write pseudocode for a complete program that performs this task, including reading 10 temperatures, counting the number above 20 degrees Celsius, calculating the average, and outputting whether the average is a whole number.(8)
(b)The program validates each temperature reading using the rule -50 ≤ temperature ≤ 50. Identify one appropriate test value for each of: normal data, boundary data, and erroneous data, for this validation rule.(3)
(c)Explain why boundary test data is important when testing this validation rule.(2)
(Total for Question 12 is 13 marks)
13
A software company is developing a mobile app that manages a large, frequently-updated contact list (contacts are regularly added, deleted and searched for). The developers are deciding between using linear search or binary search, and between using a bubble sort or a merge sort, to organise and search the contact data.

Discuss the factors the company should consider when choosing appropriate algorithms and data structures for this application. Your answer should refer to time complexity, the need to keep data sorted for binary search, and the trade-offs of maintaining a sorted list when data changes frequently.
(Total for Question 13 is 8 marks)
Mark scheme · A2.2 Problem Solving and Programming

Question 1

Question 2

Question 3

Question 4

Question 5

Question 6

Question 7

Question 8

Question 9

Question 10

Question 11

Question 12

Question 13

Mark your answers

This checks your answers in your browser, stores nothing on a server and needs no account.

Question 1

10 marks
Did your answer earn the marks?

Question 2

9 marks
Did your answer earn the marks?

Question 3

11 marks
Did your answer earn the marks?

Question 4

11 marks
Did your answer earn the marks?

Question 5

10 marks
Did your answer earn the marks?

Question 6

10 marks
Did your answer earn the marks?

Question 7

14 marks
Did your answer earn the marks?

Question 8

13 marks
Did your answer earn the marks?

Question 9

11 marks
Did your answer earn the marks?

Question 10

13 marks
Did your answer earn the marks?

Question 11

11 marks
Did your answer earn the marks?

Question 12

13 marks
Did your answer earn the marks?

Question 13

8 marks
Did your answer earn the marks?
Mark my answers