7.1 Programming Techniques (OCR A Level Computer Science): Exam Questions

3 hours42 questions
11 mark

A programmer has designed a program that includes a reusable program component

The reusable program component is a function called isInteger(). This will take a string as an argument and then check that each digit is between 0 and 9. For example if 103 is input, it will check that the digits 1, 0 and 3 are each between 0 and 9.

The asc() function returns the ASCII value of each digit. For example asc("1") returns 49.

The ASCII value for 0 is 48. The ASCII value for 9 is 57.

01 function isInteger(number)

02   result = true

03   for count = 0 to number.length-1

04     asciiValue = asc(number.substring(count, 1))

05     if not(asciiValue >= 48 and asciiValue <= 57) then

06       result = false

07     endif

08   next count

09   return result

10 endfunction

Give the line number where the iteration construct starts in the function isInteger().

Did this page help you?

21 mark

A card game uses a set of 52 standard playing cards. There are four suits; hearts, diamonds, clubs and spades. Each suit has a card with a number from; 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13.

The card game randomly gives 2 players 7 cards each. The unallocated cards become known as the deck.

The players then take it in turns to turn over a card. A valid move is a card of the same suit or the same number as the last card played.

The winner is the first player to play all of their cards.

A function, checkValid(), takes the card the player has selected, and the last card played as parameters.

It returns true if the player’s move is valid and returns false if the player’s move is not valid.

State the reason why checkValid() is a function and not a procedure.

Did this page help you?

32 marks

The following pseudocode procedure performs an insertion sort on the array parameter.

01 procedure insertionSort(dataArray:byRef)

02   for i = 1 to dataArray.Length - 1

03     temp = dataArray[i]

04     tempPos = i – 1

05     exit = false

06     while tempPos >= 0 and exit == false

07       if dataArray[tempPos] < temp then

08         dataArray[tempPos + 1] = dataArray[tempPos]

09         tempPos = tempPos - 1

10       else

11         exit = true

12       endif

13     endwhile

14   dataArray[tempPos + 1] = temp

15   next i

16 endprocedure

Explain why dataArray is passed by reference and not by value.

Did this page help you?

41 mark

A doCheck() function takes an integer value as a parameter, carries out a series of calculations and returns an integer value.

The function is shown here.

function doCheck(number)
 temp = str(number)
 max = temp.length – 1
 total = 0
 for x = 0 to max
  total = total + int(temp.subString(x,1))
 next x
 return total MOD 10
endfunction

State the value returned from the function when doCheck(3178) is called

Did this page help you?

5a1 mark

A programmer has designed a program that includes a reusable program component.

The reusable program component is a function called isInteger(). This will take a string as an argument and then check that each digit is between 0 and 9. For example if 103 is input, it will check that the digits 1, 0 and 3 are each between 0 and 9.

The asc() function returns the ASCII value of each digit. For example asc("1") returns 49.

The ASCII value for 0 is 48. The ASCII value for 9 is 57.

 01 function isInteger(number)
 02   result = true
 03   for count = 0 to number.length-1
 04     asciiValue = asc(number.substring(count, 1))
 05     if not(asciiValue >= 48 and asciiValue <= 57) then
 06       result = false
 07     endif
 08   next count
 09   return result
 10 endfunction

Identify one identifier used in the function isInteger().

5b1 mark

Give the line number where the branching (selection) construct starts in the function isInteger().

Did this page help you?

62 marks

Give two reasons why reusable program components are used in programs.

Did this page help you?

7a1 mark

A programmer creates this function shown in Fig. 5 using a high-level language.

function mystery(x,y)

  total = x + y

  while x >= 10 then

    x = x – 10

    y = y – 10

    total = total + x + y

   endwhile

   return total

 endfunction

State the value output by the line print(mystery(10,20))

7b1 mark

State the value output by the line print(mystery(0,70))

7c1 mark

State the value output by the line print(mystery(45,55))

Did this page help you?

8a2 marks

The array words is defined as a global variable and contains these values:

"house"

"boat"

"car"

"telephone"

"garden"

"spice"

"elephant"

The pseudocode function useWords() here uses the global array words.

The number of words in the array words is passed as a parameter.

function useWords(numberOfWords : byVal)

 contents = ""

 for count = 0 to numberOfWords - 1

 contents = contents + words[count] + " "

 next count

 return contents

 endfunction

Identify two variables in the function useWords().

8b2 marks

numberOfWords is a parameter passed by value.

Describe the difference between passing a parameter by value and by reference.

Did this page help you?

92 marks

Give one benefit and one drawback of declaring an array as a global variable instead of a local variable.

Did this page help you?

104 marks

Describe one feature of an Integrated Development Environment (IDE) that can be used to help write the program and one feature that can be used to help test the program.

Did this page help you?

112 marks

Functions and procedures are reusable components.

Give two benefits of writing a program with reusable components.

Did this page help you?

121 mark

A program uses the recursive function calculate(). The function is written in pseudocode.

1. function calculate(number : byVal)
2.   if number == 1 then
3.     return number
4.   else
5.     return number + calculate (number - 1)
6.   endif
7. endfunction

Give the line number in the algorithm calculate() where a recursive call is made.

Did this page help you?

131 mark

A program uses the recursive function calculate(). The function is written in pseudocode.

1. function calculate(number : byVal)
2.   if number == 1 then
3.     return number
4.   else
5.     return number + calculate (number - 1)
6.   endif
7. endfunction

Give the pseudocode function call that would return 55 from the recursive function calculate().

Did this page help you?

141 mark

The algorithm uses a while loop

State a different type of loop that could be used instead of the while loop in the given algorithm.

Snippet of pseudocode for a binary search function includes iterations and conditional statements, showing logic for searching a data array.

Did this page help you?

1a5 marks

A Nonogram is a logic puzzle where a player needs to colour in boxes. The puzzle is laid out as a grid and each square needs to be either coloured black or left white.

The numbers at the side of each row and column tells the player how many of the boxes are coloured in consecutively. Where a row has two or more numbers, there must be a white square between the coloured squares.

Nonogram puzzle, 5x5 grid with black and white squares; number clues on top and left indicate how to fill the grid.

Juan is creating a program that will store a series of Nonograms for a user to play. The game will randomly select a puzzle and display the blank grid with the numbers for each row and column to the user.

The user plays the game by selecting a box to change its colour. If the box is white it will change to black and if it is black it will change to white. The user can choose to check the answer at any point, and the game will compare the grid to the answers and tell the user if they have got it correct or not.

Juan creates a modular program with a number of subroutines. The program will use two integer 2-dimensional arrays to store the puzzles:

  • puzzle(5,5) stores the solution

  • answerGrid(5,5) stores the user’s current grid.

A 0 represents a white box and a 1 represents a black box.

Juan creates a function, countRow(), to count the number of coloured boxes in one row and return the number of consecutive coloured boxes in that row. If there is more than one set of coloured boxes in the row, these are joined together and the string is returned.

For example, in the following grid countRow for row 0 will return "2" as a string, and countRow for row 2 will return "1 1" as a string. If there are no 1s in a row, then "0" is returned as a string.

A 5x5 grid of binary numbers with a pattern of 1s and 0s. Each cell contains either a 1 or 0, arranged in distinct rows and columns.

Complete the pseudocode algorithm countRow().

01 function countRow(puzzle:byref, rowNum:byval)

02   count = 0

03   output = " "

04   for i = 0 to ……………………………………………

05     if puzzle[rowNum, i] == …………………………………………… then

06       count = count + 1

07     elseif count >= 1 then

08       output = output + str(……………………………………………) + " "

09       count = 0

10     endif

11   next i

12   if count>= 1 then

13     output=output+str(count)

14   elseif output == "" then

15     output = "……………………………………………"

16   endif

17   return ……………………………………………

18 endfunction
1b2 marks

Explain the purpose of line 03 in the function countRow.

1c3 marks

Describe the purpose of branching and iteration in the function countRow.

1d6 marks

The procedure displayRowAnswer() takes puzzle as a parameter and outputs the value in each box. Each box in a row is separated by a space. At the end of each row there are two spaces and (by calling the function countRow from part a the clue values for that row.

For example the puzzle below:

A 5x5 grid with binary digits; 1s and 0s arranged in rows and columns. A single red 1 is located in the fourth column of the third row.

Would output

Matrix of binary numbers with a column of sums on the right, displaying five rows and five columns, representing numerical data or logic states.

Write pseudocode or program code for the procedure displayRowAnswer()

1e9 marks

Juan passed the two arrays as parameters, but he did consider making them globally accessible.

Compare the use of global and local variables and data structures in this program. Include the use of parameters and program efficiency in your answer.

Did this page help you?

24 marks

Hugh has written a recursive function called thisFunction() using pseudocode.

01 function thisFunction(theArray, num1, num2, num3)

02   result = num1 + ((num2 - num1) DIV 2)

03   if num2 < num1 then

04     return -1

05   else

06     if theArray[result] < num3 then

07       return thisFunction(theArray, result + 1, num2, num3)

08     elseif theArray[result] > num3 then

09       return thisFunction(theArray, num1, result - 1, num3)

10     else

11       return result

12     endif

13   endif

14 endfunction

The function DIV calculates integer division, e.g. 5 DIV 3 = 1

Hugh could have written thisFunction() using iteration instead of recursion.

Compare two differences between recursion and iteration.

Did this page help you?

33 marks

A programmer has designed a program that includes a reusable program component.

01 function isInteger(number)
02   result = true
03   for count = 0 to number.length-1
04     asciiValue = asc(number.substring(count, 1))
05     if not(asciiValue >= 48 and asciiValue <= 57) then
06       result = false
07     endif
08   next count
09   return result
10 endfunction

Describe the purpose of the following lines in the function isInteger().

  • Line 03

  • Line 04

  • Line 09

Did this page help you?

43 marks

A recursive pseudocode function, recursiveAlgorithm(), is shown.

01 function recursiveAlgorithm(value)
02   if value <= 0 then
03     return 1
04   elseif value MOD 2 = 0 then
05     return value + recursiveAlgorithm(value – 3)
06   else
07     return value + recursiveAlgorithm(value – 1)
08   endif
09 endfunction

Describe the key features of a recursive algorithm.

You may refer to the function, recursiveAlgorithm() in your answer.

Did this page help you?

56 marks

A programmer uses an Integrated Development Environment (IDE).

Complete the table by identifying and describing three IDE features that can help the programmer to develop, or debug a program.

IDE feature

Description

Did this page help you?

6a2 marks

A text-based computer game allows a user to dig for treasure on an island. The island is designed as a grid with 10 rows and 20 columns to store the treasure. Each square is given an x and y coordinate. Some of the squares in the grid store the name of a treasure object. Each treasure object has a value, e.g. 100 and a level, e.g. "Bronze."

The treasure game is being programmed using an object-oriented paradigm.

A class, Treasure, is used to store the treasure objects.

The design for the Treasure class, its attributes and methods is shown here.

class: Treasure

attributes:
private value : integer
private level : string

methods:
new()
function getValue()
function getLevel()

The get method getLevel() will return the appropriate attribute.

Write the method getLevel() using either pseudocode or program code.

6b2 marks

Describe the object-oriented programming technique being used in previous part.

Did this page help you?

72 marks

A program is written using an object-oriented programming paradigm and uses a class called video to organise the videos that are streamed to customers.

The class video has these attributes:

  • name

  • number of views

  • star rating.

The constructor method will set the name attribute to the name that is passed in as a parameter. The constructor will also initially set the number of views to 0 and the star rating to 3.

A public method called updateviews() will update the number of views after a video has been viewed. This method is defined inside the video class.

Write program code or pseudocode for the method updateviews() to increase the number of views by one.

Did this page help you?

83 marks

A programmer creates this function shown in Fig. 5 using a high-level language.

function mystery(x,y)
  total = x + y
  while x >= 10 then
    x = x – 10
    y = y – 10
    total = total + x + y
  endwhile
  return total
endfunction

The programmer creates another function to count and return how many capital letters are in a string that is passed into the function as a parameter.

The asc() function takes in a character and returns its ASCII value. For example asc("A") returns 65. Capital letters have ASCII values between 65 and 90 inclusive.

Complete the function below

function countCapitals(text)
 // initialise counter to 0
 capCount = 0
 // loop through each character in the string passed in
  for x = 0 to text.length-1
    c = text.subString(x, 1)
    // check if character is a capital
    if asc(c) >= 65 ……………………………………………………………
      // if so, increment counter
      …………………………………………………………………………
    endif
  next x
  ……………………………………………………………………
endfunction

Did this page help you?

94 marks

The array words is defined as a global variable and contains these values:

Table with two rows and seven columns; each cell contains a word: house, boat, car, telephone, garden, spice, elephant, all in quotations.
function useWords(numberOfWords : byVal)
 contents = ""
 for count = 0 to numberOfWords - 1
   contents = contents + words[count] + " "
 next count
 return contents
endfunction

The pseudocode function useWords() here uses the global array words.

The number of words in the array words is passed as a parameter.

Rewrite the function useWords() to use a while loop instead of a for loop.

The function header and close have been written for you.

Write your answer using pseudocode or program code.

function useWords(numberOfWords : byVal)

Did this page help you?

103 marks

A card game uses a set of 52 standard playing cards. There are four suits; hearts, diamonds, clubs and spades. Each suit has a card with a number from; 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13.

The card game randomly gives 2 players 7 cards each. The unallocated cards become known as the deck.

The players then take it in turns to turn over a card. A valid move is a card of the same suit or the same number as the last card played.

The winner is the first player to play all of their cards.

A function, checkValid(), takes the card the player has selected, and the last card played as parameters.

It returns true if the player’s move is valid and returns false if the player’s move is not valid.

The programmer will use a branching (selection) construct to make decisions.

Describe the decisions that will be made in the checkValid() function and how these change the return values.

Did this page help you?

112 marks

A program uses the recursive function calculate(). The function is written in pseudocode.

 1. function calculate(number : byVal)
 2.   if number == 1 then
 3.     return number
 4.   else
 5.     return number + calculate (number - 1)
 6.   endif
 7. endfunction

State two features of any recursive algorithm.

Did this page help you?

125 marks
1. function calculate(number : byVal)
2.   if number == 1 then
3.     return number
4.   else
5.     return number + calculate (number - 1)
6.   endif
7. endfunction

Trace the recursive function calculate() and give the final return value, when the following function call is run:

calculate(5)

You may choose to use the table below to give your answer

Function call

number

return

calculate(5)

Did this page help you?

135 marks

Christoff is writing a program to simulate a city using object-oriented programming. He is designing classes to store different types of buildings and their location on the road. He has created the following plan for some of the buildings:

Diagram showing class hierarchy: "building" class with "office" and "house" subclasses. Attributes and methods listed for each class.

Part of the declaration for the class building is shown.

Complete the pseudocode declaration by filling in the missing statements.

class building
  private numberFloors
  private width
  private ………………………………………………………………………
  public procedure new(pFloors, pWidth, pHeight)
    numberFloors = ………………………………………………………………………
    width = pWidth
    height = pHeight
  endprocedure
  public function getNumberFloors()
    return ………………………………………………………………………
  endfunction
  public function setNumberFloors(pFloors)
    //sets the value of numberFloors when the parameter is >= 1
    //returns true if numberFloors is successfully changed,
    //returns false otherwise
    if pFloors >= 1 then
      numberFloors = ………………………………………………………………………
      return true
    else
      return ………………………………………………………………………
    endif
  endfunction
endclass

Did this page help you?

146 marks

Christoff is writing a program to simulate a city using object-oriented programming. He is designing classes to store different types of buildings and their location on the road. He has created the following plan for some of the buildings:

Diagram showing class hierarchy: "building" class with "office" and "house" subclasses. Attributes and methods listed for each class.

Write program code or pseudocode to declare the class house

Define the attributes and constructor method in your answer.

You do not need to write the get or set methods.

Did this page help you?

154 marks

Christoff is writing a program to simulate a city using object-oriented programming. He is designing classes to store different types of buildings and their location on the road. He has created the following plan for some of the buildings:

Diagram showing class hierarchy: "building" class with "office" and "house" subclasses. Attributes and methods listed for each class.

Christoff develops a new class to store the houses in one road.

His class design is shown:

class : houseRoad

attributes:

private buildings(100) //array of class house

private numberBuildings //records the number of houses currently stored in the array buildings

methods:

new(building)

function getBuilding(buildingNum)

procedure newbuilding(pBuilding)

The method newbuilding() takes a new building as a parameter, and stores this in the next free space in the array buildings.

Write pseudocode or program code for the method newbuilding().

Did this page help you?

164 marks

Christoff is writing a program to simulate a city using object-oriented programming. He is designing classes to store different types of buildings and their location on the road. He has created the following plan for some of the buildings:

UML diagram showing a 'building' class with attributes and methods, and two subclasses: 'office' with desk attributes and 'house' with bedroom attributes.

Christoff wants to create a new house called houseOne. It has the properties: 2 floors, 8(m) width, 10(m) height, 3 bedrooms and 2 bathrooms.

The house is located on a road with the identifier limeAvenue of type houseRoad, houseOne is the first house in this road.

Write pseudocode or program code to declare the house houseOne, road limeAvenue and assign houseOne to the first array position in the road.

Did this page help you?

176 marks

Barney is writing a program to store data in a linked list. He is writing the initial program for a maximum of 10 data items.

Each node in the linked list has a data value and a pointer (to the next item).

A null pointer is stored with the value –1.

The procedure printLinkedList() follows the pointers to print all of the elements in the linked list.

01 procedure printLinkedList(headPointer) 
02   tempPointer = headPointer - 1 
03   dataToPrint = ″″ 
04   if tempPointer == -1 then 
05     print(″List is full″) 
06   else 
07     while linkedList[pointer].getPointer() != -1 
08       dataToPrint = dataToPrint + ″ ″ + linkedList[tempPointer,0] 
09       linkedList[tempPointer].getPointer() = tempPointer 
10     endwhile 
11   print(dataToPrint + ″ ″ + linkedList[tempPointer].getData() 
12   endif
13 endprocedure

Barney will use an Integrated Development Environment (IDE) to debug his program code.

Describe three features commonly found in IDEs that Barney could use to debug his program code.

Did this page help you?

183 marks

The function checkWon() takes answerGrid and puzzle as parameters and compares each element in the grids. If they are identical, it returns true, otherwise returns false.

01 function checkWon(puzzle)
02   for row = 0 to 4
03     for column = 0 to 4
04       if puzzle[row, column] == answerGrid[row, column] then
05         return false
06       endif
07     next column
08   next column
09   return true
10 endfunction

There are three logic errors in the function checkWon.

State the line number of each error and give the corrected line

Did this page help you?

15 marks

A class, Board, is used to store a 10 row (x coordinate) by 20 column (y coordinate) grid

The design for the Board class, its attributes and methods is shown here.

class: Board

attributes:
private grid : Array of Treasure

methods:
new()
function getGridItem(x, y)
function setGridItem(x, y, treasureToInsert)

The constructor initialises each space in the grid to a treasure object with value as -1 and level as an empty string.

Complete the following pseudocode for the constructor method.

public procedure new()
  for row = ............................. to 9
    for column = 0 to .............................
      ............ [row, column] = new Treasure(...........,"")
    next .............................
  next row
endprocedure

Did this page help you?

27 marks

A text-based computer game allows a user to dig for treasure on an island. The island is designed as a grid with 10 rows and 20 columns to store the treasure. Each square is given an x and y coordinate. Some of the squares in the grid store the name of a treasure object. Each treasure object has a value, e.g. 100 and a level, e.g. "Bronze."

A procedure, guessGrid():

  • takes a Board object as a parameter

  • accepts the row (x) and column (y) coordinates from the user

  • outputs "No treasure" if there is no treasure found at the coordinate (level is an empty string)

  • if there is treasure at that coordinate, it outputs the level and the value of the treasure in an appropriate message.

Write the procedure guessGrid() using either pseudocode or program code.

Did this page help you?

36 marks

The recursive function thisFunction().

01 function thisFunction(theArray, num1, num2, num3)
02   result = num1 + ((num2 - num1) DIV 2)
03   if num2 < num1 then
04     return -1
05   else
06     if theArray[result] < num3 then
07       return thisFunction(theArray, result + 1, num2, num3)
08     elseif theArray[result] > num3 then
09       return thisFunction(theArray, num1, result - 1, num3)
10     else
11       return result
12     endif
13   endif
14 endfunction

Rewrite the function thisFunction() so that it uses iteration instead of recursion.

You should write your answer using pseudocode or program code.

Did this page help you?

45 marks

A recursive pseudocode function, recursiveAlgorithm(), is shown.

01 function recursiveAlgorithm(value)
02   if value <= 0 then
03     return 1
04   elseif value MOD 2 = 0 then
05     return value + recursiveAlgorithm(value – 3)
06   else
07     return value + recursiveAlgorithm(value – 1)
08   endif
09 endfunction

Trace the recursive function, recursiveAlgorithm(), and give the final return value when called with recursiveAlgorithm(10).

You may choose to use the table below to give your answer.

Function call

value

return

recursiveAlgorithm(10)

Did this page help you?

56 marks

Octal is a base 8 number system

To convert a denary number to base 8:

  • the denary value is divided by 8 and the remainder is stored

  • the integer value after division is divided by 8 repeatedly until 0 is reached

  • the remainders are then displayed in reverse order.

Example 1:
Denary 38
38 / 8 = 4 remainder 6 6
4 / 8 = 0 remainder 4 4
Octal = 46

Example 2:
Denary 57
57 / 8 = 7 remainder 1 1
7 / 8 = 0 remainder 7 7
Octal = 71

Write an algorithm to:

  • take a denary value as input from the user

  • convert the number to octal

  • output the octal value.

You do not need to validate the input from the user.

Write your algorithm using pseudocode or program code.

Did this page help you?

65 marks

A treasure game is being programmed using an object-oriented paradigm.

A class, Treasure, is used to store the treasure objects.

The design for the Treasure class, its attributes and methods is shown here

class: Treasure

attributes:
private value : integer
private level : string

methods:
new()
function getValue()
function getLevel()

The constructor method takes a value as an integer, e.g. 100, and a level, e.g. "bronze", as parameters and assigns these to the attributes.

Write pseudocode or program code to declare the class Treasure.

You should define the attributes and constructor method in your answer.

You do not need to write the get methods.

Did this page help you?

79 marks

A class, Board, is used to store the 10 row (x coordinate) by 20 column (y coordinate) grid.

The design for the Board class, its attributes and methods is shown here.

class: Board

attributes:

private grid : Array of Treasure

methods:

new() function getGridItem(x, y) function setGridItem(x, y, treasureToInsert)

The main program initialises a new instance of Board. The programmer is considering declaring this as a global variable or as a local variable and then passing this into the subroutines that control the game.

Compare the use of variables and parameters in this game.

You should include the following in your answer:

  • what is meant by a local variable and global variable

  • how local and global variables can be used in this program

  • the use of passing parameters by value and by reference.

Did this page help you?

87 marks

A video streaming service uses a relational database. An extract of the data from two tables from this database is shown in Fig. 2.

Membership contains data about current memberships that customers hold and package contains data about different streaming packages available.

Table showing user membership details with start dates and package types, and a separate table listing package costs and advert presence for Premium, Basic, and Free plans.

A program is written using an object-oriented programming paradigm and uses a class called video to organise the videos that are streamed to customers.

The class video has these attributes:

  • name

  • number of views

  • star rating.

The constructor method will set the name attribute to the name that is passed in as a parameter. The constructor will also initially set the number of views to 0 and the star rating to 3.

Write program code or pseudocode to declare the class video and initialise the required attributes as private.

You should include both the attribute definitions and the constructor method in your answer.

Did this page help you?

99 marks

Anna currently writes her program code in a text editor and then runs the compiler.

She has been told that using an Integrated Development Environment (IDE) would be more helpful.

Discuss the benefits of Anna using an IDE to write and test her program rather than using a text editor.

Did this page help you?

105 marks

A doCheck() function takes an integer value as a parameter, carries out a series of calculations and returns an integer value.

The function is shown here.

function doCheck(number)
  temp = str(number)
  max = temp.length – 1
  total = 0
  for x = 0 to max
    total = total + int(temp.subString(x,1))
  next x
  return total MOD 10
endfunction

Write an algorithm that will:

  • allow the user to enter an integer value

  • pass the value entered into the doCheck() function as a parameter

  • store both the value input and the value returned from the function in a text file with name "storedvalues.txt"

You should write your algorithm using either pseudocode or program code.

Did this page help you?