Algorithms - Worksheets, Questions and Revision

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

Download PDFJump to mark scheme (page 10)Read the revision guide
« Previous: Ethical, Legal, Cultural and Environmental ImpactNext: Programming Fundamentals »
Revision Library
revisionlibrary.co.uk
GCSE · Computer Science

P2.1 Algorithms

OCR J277 · Calculators not allowed · about 100 minutes
Total Marks
Name: _______________________________    Date: ____ / ____ / ______
Answer ALL questions. Show all your working.
1
Computational thinking involves breaking down complex problems using specific techniques before an algorithm is designed.
(a)Define what is meant by 'decomposition' when designing a solution to a problem.(2)
(b)Define what is meant by 'abstraction' when designing an algorithm.(2)
(Total for Question 1 is 4 marks)
2
A systems analyst is designing a new algorithm to calculate a student's average test score.
(a)State two ways in which an algorithm can be represented, other than by using a written description in ordinary sentences.(2)
(b)Identify one advantage of using a flowchart, rather than pseudocode, to represent an algorithm.(1)
(Total for Question 2 is 3 marks)
3
Flowcharts use standard symbol shapes to represent different steps in an algorithm.
(a)State the flowchart symbol shape used to represent a decision.(1)
(b)State the flowchart symbol shape used to represent an input or output step.(1)
(c)State the flowchart symbol shape used to represent the start or end (terminator) of an algorithm.(1)
(Total for Question 3 is 3 marks)
4
A programmer writes the following pseudocode, using the OCR Exam Reference Language.
total <- 0
FOR i <- 1 TO 5
    total <- total + i
NEXT i
OUTPUT total
(a)Complete a trace table for this algorithm, showing the value of i and total at the end of each pass through the loop.(4)
(b)State the value output by the algorithm.(1)
(Total for Question 4 is 5 marks)
5
A programmer writes the following pseudocode.
count <- 0
num <- 18
WHILE num > 1
    num <- num / 2
    count <- count + 1
ENDWHILE
OUTPUT count
(a)Complete a trace table showing the values of num and count each time the loop body executes.(5)
(b)State the number of times the loop body executes.(1)
(Total for Question 5 is 6 marks)
6
An array called scores contains the values [55, 62, 48, 71, 60, 39] at indices 0 to 5.
(a)Describe how a linear search algorithm would locate the value 71 in this array, stating the number of comparisons needed.(3)
(b)State one disadvantage of using a linear search, rather than a binary search, on a large, sorted dataset.(1)
(Total for Question 6 is 4 marks)
7
A sorted array called nums, in ascending order, contains: [3, 9, 14, 22, 27, 35, 41, 48, 55, 62] at indices 0 to 9.
(a)State the condition that must be met before a binary search can be used on this array.(1)
(b)A binary search uses mid <- (lower + upper) DIV 2 to find the middle index at each step. Complete a trace table to show the values of lower, upper and mid, and the outcome of each comparison, as a binary search locates the value 41.(5)
(c)State the number of comparisons made to find the value using binary search in part (b), and explain why this is fewer than a linear search would need in the worst case for this array.(2)
(Total for Question 7 is 8 marks)
8
An array contains the values [8, 3, 6, 1, 9], which need to be sorted into ascending order using a bubble sort that compares and swaps adjacent items.
(a)Show the state of the array after each comparison in the first full pass of the bubble sort.(4)
(b)State the maximum number of full passes a bubble sort would need to fully sort a list of 5 items.(1)
(Total for Question 8 is 5 marks)
9
An array contains the values [5, 2, 9, 1, 6], to be sorted into ascending order using insertion sort.
(a)Complete a trace table to show the state of the array after each new element has been inserted into its correct position.(4)
(b)Insertion sort is described as an 'in-place' sorting algorithm. State what is meant by an in-place algorithm.(1)
(Total for Question 9 is 5 marks)
10
Merge sort is an example of a 'divide and conquer' algorithm.
(a)Describe how merge sort would sort a list of 8 unsorted numbers, referring to both the dividing stage and the merging stage.(4)
(b)State one advantage of merge sort over bubble sort when sorting a very large dataset.(1)
(Total for Question 10 is 5 marks)
11
An array called scores has 10 elements, at indices 0 to 9.
(a)Write an algorithm, using pseudocode, that finds and outputs the largest value in the array scores.(4)
(Total for Question 11 is 4 marks)
12
A programmer needs to validate user input.
(a)Write an algorithm, using pseudocode, that repeatedly asks the user to input a number, and only accepts a value between 1 and 10 inclusive, then outputs the message 'Valid' once an acceptable value has been entered.(4)
(Total for Question 12 is 4 marks)
13
A programmer intends the following pseudocode to output the sum of all even numbers between 1 and 20 inclusive, but it contains errors.
total <- 0
FOR i <- 1 TO 20
    IF i / 2 = 0 THEN
        total <- total + 1
    ENDIF
NEXT i
OUTPUT total
(a)Identify two errors in the algorithm above which mean it does not correctly output the sum of all even numbers between 1 and 20.(2)
(b)Rewrite the algorithm with these errors corrected.(2)
(Total for Question 13 is 4 marks)
14
A programmer compares linear search and binary search on datasets of different sizes.
(a)Explain why the difference in efficiency between linear search and binary search becomes more significant as the size of the dataset increases.(3)
(b)Calculate the maximum number of comparisons required by a binary search on a sorted list of 1024 items.(2)
(Total for Question 14 is 5 marks)
15
An array called numbers contains 6 elements, at indices 0 to 5.
(a)Write an algorithm, using pseudocode, for a bubble sort that sorts the array numbers into ascending order. Your algorithm should use nested loops.(6)
(Total for Question 15 is 6 marks)
16
A school is choosing which sorting algorithm to use to sort a list of 5000 student records by surname. The list is already nearly in alphabetical order, because it was exported from an existing system that keeps records mostly sorted. Discuss which of bubble sort, insertion sort and merge sort would be most suitable for this task, justifying your answer by referring to how each algorithm works and how efficient it is.
(Total for Question 16 is 6 marks)
17
A student is asked to write a program to help organise a school's sports day.
(a)State two sub-problems the student could identify by decomposing the problem 'organise a school's sports day'.(2)
(b)Explain, using this scenario, what is meant by 'abstraction' when designing an algorithm.(2)
(Total for Question 17 is 4 marks)
18
A programmer writes the following pseudocode.
FUNCTION doubleIfEven(num)
    IF num MOD 2 = 0 THEN
        RETURN num * 2
    ELSE
        RETURN num
    ENDIF
ENDFUNCTION

total <- 0
FOR i <- 1 TO 4
    total <- total + doubleIfEven(i)
NEXT i
OUTPUT total
(a)Complete a trace table to show the value returned by doubleIfEven for each value of i, and the running value of total.(4)
(b)State the value output by the program.(1)
(Total for Question 18 is 5 marks)
19
An algorithm uses a FOR loop that runs from 0 to 15 inclusive, using a variable called cycle to count each iteration.
(a)State the 8-bit binary representation of the maximum value that cycle reaches.(1)
(b)Convert the maximum value of cycle to hexadecimal.(1)
(c)Explain why programmers often use hexadecimal, rather than binary, when reading values from trace tables or debugging output.(2)
(Total for Question 19 is 4 marks)
20
Two algorithms both correctly sort the same array. One algorithm sorts the array in place, while the other creates a new array during sorting.
(a)State which of these two algorithms is likely to use more memory, and explain your reasoning.(2)
(Total for Question 20 is 2 marks)
Mark scheme · P2.1 Algorithms

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

Question 14

Question 15

Question 16

Question 17

Question 18

Question 19

Question 20