You are on page 1of 23

Object Oriented Exam Questions Name:_________________________

1. Some high-level languages are object-oriented.

Describe three features of an object-oriented language.

 
 
  

 
 
  

 
 
  

 
[6]
2. A taxi firm is investigating replacing its drivers with self-driving cars.

The code for the self-driving system has been written using an object-oriented programming language.

It recognises obstacles in the road and then classifies them.


The class for Obstacle is shown below.
 
public class Obstacle
 
private moving /Boolean value
  private distance /Real num ber given in metres
private direction /Integer given as between 1 and 360 degrees

  public procedure new(givenMoving, givenDistance, givenDirection)


 
moving=givenMoving
  distance=givenDistance
direction=givenDirection
 
  endprocedure
 
  public procedure updateDist ance(givenDistance)
 
  distance=givenDistance
 
  endprocedure
 
endclass

i. Write a line of code to create an object called bollard of type Obstacle which is not moving and is 7.8 metres away in a
direction of 8 degrees.

[2]

© OCR 2022. You may photocopy this Page 1 of 23 Created in ExamBuilder


page.
ii. Describe an example of encapsulation in the class definition code above.

[2]

iii. Describe the advantages of using encapsulation.

[2]

 
3. A program is needed to plan the layout of a garden.

The program will allow the user to create an image of the garden, for example:
 

The program is to be built using object oriented programming.

All items that can be added to the garden are declared as instances of the class GardenItem.

The class has the following attributes:

 
Attribute Description Example
 itemName  The name of the item  Flowerbed
 length  The length of the item in metres  2
 width  The width of the item in metres  1

i. The constructor method sets the attributes to values that are passed as parameters.

Write pseudocode or program code to declare the class GardenItem and its constructor. All attributes should be private and
initialised through the constructor (e.g. daisies = new GardenItem("Flowerbed",2,1)).








© OCR 2022. You may photocopy this Page 2 of 23 Created in ExamBuilder


page.



[4]

ii. The trees in the garden layouts are defined by the class Tree. This class inherits from GardenItem.

The class Tree has the additional attributes: height, sun, shade.

If sun is true then the tree can grow in full sun, if it is false then it cannot.

If shade is true then the tree can grow in full shade, if it is false then it cannot.

The length and width of a tree are the same. Only one value for these measurements is passed to the constructor.

Write an algorithm, using pseudocode or program code, to declare the class Tree.
Declare all attributes as private.

[5]

iii. The Common Oak is a type of tree. It has a maximum height, length and width of 40 m. It can grow in sun and shade.

Write a statement, using pseudocode or program code, to declare an instance of tree for the Common Oak. Give the object the
identifier firstTree.

© OCR 2022. You may photocopy this Page 3 of 23 Created in ExamBuilder


page.

[4]

iv. The classes GardenItem and Tree use get and set methods to access and alter their private attributes.

Write the get method getItemName and set method setItemName for class GardenItem. The set method takes the new value
as a parameter.

Do not write any other methods, or re-declare the class.

[4]

v. The trees in the garden layouts are stored in a 1-dimensional array, treeArray. The array can store a maximum of 1000 items.
The array has global scope.

A procedure, findTree, takes as parameters:

 
• The maximum height of a tree
• The maximum width of a tree
• Whether the tree can live in full sun
• Whether the tree can live in full shade.

It searches the array, treeArray, for all trees that do not exceed the maximum height and width, and that can grow in the conditions
available. If there are no suitable trees, a suitable message is output.

It outputs the name and details of the trees found in an appropriate message.

Call the get methods, getItemName, getHeight, getWidth, getSun, getShade, to access the attributes.

Write, using pseudocode or program code, the procedure findTree.







© OCR 2022. You may photocopy this Page 4 of 23 Created in ExamBuilder


page.









[6]
 

4(a). Livid Lizards is a computer game in which players get to fire lizards from a cannon to knock down walls. Players get to pick different
types of lizards, each with qualities and special powers.

The game is coded using an object-oriented language. Below is the code for the lizard class:

i. Describe what is meant by the term inheritance.

[3]

ii. Explain one way the game’s developers might use inheritance for Livid Lizards.

 
© OCR 2022. You may photocopy this Page 5 of 23 Created in ExamBuilder
page.
 

[3]

  (b). Identify an attribute in the Lizard class.


[1]
 

  (c). Lizard is a class. Describe what is meant by a class.


 
 
[2]
 

5. An object-oriented system is implemented to organize a company’s information about staff attendance. Classes, objects, methods and
attributes are used in this system.

i. State the meaning of each of the following terms:

Object  

Method  

Attribute  

[3]

ii. Each worker has a name and an attendance figure which can be between 0 and 100.

Write a definition for a fully encapsulated customer class, providing both get and set methods for all attributes. You do not have to
write code for the constructor method.

© OCR 2022. You may photocopy this Page 6 of 23 Created in ExamBuilder


page.

[5]

6(a). Kim is writing an object-oriented program for a four player board game. The board has 26 squares that players move around, as
shown in Fig. 5.1.

Fig. 5.1

Each player takes it in turn to roll two dice. They then move that number of spaces on the board. If they roll a double (both dice have the
same value), they then take a card from the deck. The deck contains 40 cards that each include a sentence (such as “You have won the
lottery”). The sentence on the card determines if money is given or taken away from the player.

© OCR 2022. You may photocopy this Page 7 of 23 Created in ExamBuilder


page.
Fig. 5.2

Each square (apart from Start and Miss a turn) has an animal associated with it that the player can purchase, if it has not been purchased
already, for example square 6 has a Squirrel. Fig. 5.2 shows an example of one of these animals. Once a player has purchased the
animal, any opposing player which subsequently lands on the square/animal has to pay a fine.

Each animal can be upgraded, with each upgrade the game charges more each time a player stops on them. For example, with no
upgrade the level 0 squirrel costs £10 when a player stops on it. If £1000 is paid to upgrade, the squirrel is then a level 1 animal and now
charges £50 for a stop.

The cost to purchase and upgrade the animal is the same.

Each animal can be upgraded to a maximum of level 3.

When a player lands on, or passes the square ‘Start’ (position 0), they receive £500. If they land on ‘Miss a turn’ (position 13), they miss
their next turn.

i. A class, Player, stores the player's ID (P1, P2, P3, P4), their current board position and the amount of money they have.

Fig. 5.3 shows a class diagram for Player. A class diagram describes a class. It contains the class name, followed by the
attributes, then the methods.

Fig. 5.3

The constructor creates a new instance of Player, taking the player's ID as a parameter. The board position is set to 0, and money
to £2000.

Write, using pseudocode, the constructor method for the Player class.

[3]

© OCR 2022. You may photocopy this Page 8 of 23 Created in ExamBuilder


page.
ii. A class, Animal, define the attributes and methods for the animals stored in each square.
Fig. 5.4 shows a class diagram for Animal.

Fig. 5.4

The constructor takes the required data as parameters and then sets currentLevel to 0, and assigns the parameters as the
remaining attributes for the new object.

Write, using pseudocode, the constructor method for the Animal class.

[4]

© OCR 2022. You may photocopy this Page 9 of 23 Created in ExamBuilder


page.
 

iii. Write, using pseudocode, the code to create an instance of Animal for the Squirrel shown in Fig. 5.2, positioned on square
number 6, for the constructor function you wrote in part (a)(ii).

[2]

  (b). The board is stored as a 1D array, board, of data type Animal. The spaces at 0, and 13, are left as empty elements that are checked
using separate functions.

i. Complete, using pseudocode, the function to:


o Roll both dice
o Move the player, the dice number of spaces
o If a double is rolled, calls the procedure pickDeck
o Adds £500 if they have passed or landed on Start
o Calls the procedure missAGo if they land on space 13 or
o Calls the procedure checkAnimal
o Return the new position

[6]

© OCR 2022. You may photocopy this Page 10 of 23 Created in ExamBuilder


page.
ii. *The parameter currentPlayer from part (b)(i) can be passed by value or by reference.

Explain the difference, benefits and drawbacks between passing by value and by reference. Recommend which should be used
for currentPlayer, justifying your decision.

[9]

© OCR 2022. You may photocopy this Page 11 of 23 Created in ExamBuilder


page.
  (c). The deck is stored as a zero-indexed 1D array, named deck, of type Card.

The class diagram for Card is shown in Fig. 5.5.

Fig. 5.5

The array, deck, is treated as a queue, with a variable, headPointer, identifying the first card in the deck. When a card has been used, the
head pointer increases to move to the next position. If the end of the deck is reached, the head pointer returns to 0 and starts again.

The procedure pickDeck:

 takes the current player as a parameter


 outputs the text to be displayed from the first card in the queue
 adds or subtracts the amount to/from the current player's money
 increases the head pointer

Write, using pseudocode, the procedure pickDeck.


 
 
 
 
 
 
 
 
 
 
 
 
 
[6]
 

  (d). The procedure checkAnimal:

 Takes the current player as a parameter


 Accesses the data for the animal at the player's position in the array board
 If the animal is free, asks the player if they would like to purchase the animal and outputs its name and cost, if they choose to buy
the animal, it calls the procedure purchase() with the player and animal as parameters
 If that player owns the animal, and it is not at level 3, it asks if they would like to upgrade the animal
 If they would like to upgrade, it calls the method upgrade for that animal with the current player as a parameter
 If a different player owns the animal, it calls the method getAmountToCharge() for that animal, sending this value and the current
player as parameters to the procedure chargeStay()

Write, using pseudocode, the procedure checkAnimal.


[10]
 
 
 
 
 
 
 
 
 

© OCR 2022. You may photocopy this Page 12 of 23 Created in ExamBuilder


page.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

7. * Procedural programming and object-oriented programming are two paradigms commonly used by programmers when developing
computer games.

Discuss the advantages of using object-oriented programming over procedural programming when developing computer games. You
should refer to inheritance, encapsulation and polymorphism in your answer.





























[12]
 

8. 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.

© OCR 2022. You may photocopy this Page 13 of 23 Created in ExamBuilder


page.
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.

Barney wants the nodes to be stored as objects using object-oriented programming. He designs the following class.

 
 class:    node

 attributes:
 private    data : Real
 private    pointer : Integer

 methods:
 new (newData, newPointer)
 getData()
 getPointer()
 setData(newData)
 setPointer(newPointer)

The constructor assigns the parameters to the attributes to create an object.

i. Write an algorithm, using pseudocode or program code, to create the class node, its attributes and constructor.

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

[4]

ii. The class node, uses get methods and set methods.

Describe one difference between get methods and set methods.

© OCR 2022. You may photocopy this Page 14 of 23 Created in ExamBuilder


page.

[2]

9. A supermarket uses an object-oriented approach to organise items that it offers for sale. Part of the class definition for the
ItemForSale class is shown below.

class ItemForSale
     public itemName
     public price
     public discount

     …

endclass

The discount attribute represents a percentage discount on the price. The discount can be between 0 and 50 (inclusive). All new items
for sale initially have a discount value of 0.

i. Write the constructor method for the ItemForSale class.

[4]

ii. Write a line of code to create an object of type ItemForSale called mushypeas that has a name of “mushy peas” and a price of
£0.89

[3]

iii. Write the calculatePrice() method, which applies the percentage discount to the price and returns the new value.

© OCR 2022. You may photocopy this Page 15 of 23 Created in ExamBuilder


page.

[3]

10. A software development company is building an operating system for a mobile phone that is in the process of being designed.

* The code is written using an object-oriented programming (OOP) language. Discuss the advantages and disadvantages to the team of
developers of using OOP over procedural programming. You should refer to inheritance, encapsulation and polymorphism in your answer.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
[9]
 

11. The layout for a 2-player board game is shown in Fig 2.1

© OCR 2022. You may photocopy this Page 16 of 23 Created in ExamBuilder


page.
The game is played by rolling two 6-sided dice and moving that number of spaces. Both players start on the START space. If a player
lands on a space occupied by the other player, they move to the next available space.

The board is to be stored as a 2-dimensional array.

Each time a player moves, a series of obstacles are to be added to the board.

On their turn, each player rolls two dice. The smaller number from the two dice is taken, and that many obstacles will appear on the board
in random locations.

For example, if a 3 and 6 are rolled, then 3 obstacles will appear.

A recursive function is written in pseudocode to perform this task.

The code generates an instance of the object obstacle.

i. Explain the purpose of the code in line in the algorithm.

[2]

ii. Identify the line of code where recursion occurs.

[1]

iii. The recursive function could have been written using iteration.

Describe the benefits and drawbacks of using recursion instead of iteration.

Benefits  

Drawbacks  

© OCR 2022. You may photocopy this Page 17 of 23 Created in ExamBuilder


page.
 

[4]

iv. Rewrite the function so it uses iteration instead of recursion.

[4]

v. If a position on the board is not occupied, its value is set to a blank string ("").

The current algorithm does not check if the random space generated is currently occupied.

Write a subroutine that takes the generated position of the board, checks if it is free and returns if free, or if occupied.

[3]

© OCR 2022. You may photocopy this Page 18 of 23 Created in ExamBuilder


page.
12. A large health centre employs a number of nurses, each with a qualification. Some nurses are senior nurses, and these are paid an
extra fee in addition to their salary.

The diagram below shows part of the system used.

i. State the type of diagram shown.

[1]

ii. State the term that describes setSalary( ), setQualification( ) or setExtraFee( ).

[1]

iii. Explain the meaning of the arrows in the diagram, using an example.

[2]

iv. Mary Jones is a new employee at the health centre.

State why the following program statement is not valid.

Jones.setSalary(12000)

[1]

v. You may assume that thisSeniorNurse has been correctly defined as an object of SeniorNurse, and that x is a number.

© OCR 2022. You may photocopy this Page 19 of 23 Created in ExamBuilder


page.
Explain why the program statement

thisSeniorNurse.setSalary(x)

is valid even though setSalary( ) is not shown in the SeniorNurse part of the diagram.

[3]

13(a). A supermarket uses an object-oriented approach to organise items that it offers for sale. Part of the class definition for the
ItemForSale class is shown below.

class ItemForSale
     public itemName
     public price
     public discount

     …

endclass

The supermarket has previously had issues with discounts being set as values above 50.

Explain how encapsulation could be applied to the ItemForSale class to stop this problem from occurring.

You are not expected to write any code in your answer to this question.





[3]
 

  (b). Some items in the supermarket are only available through home delivery. These items are the same as ItemsForSale with the
following exceptions:
 
• the supermarket also stores the location of the stock
• the percentage discount allowed is up to 75 rather than the standard 50.

Explain how inheritance can be used to implement the above requirements.









[4]
 

© OCR 2022. You may photocopy this Page 20 of 23 Created in ExamBuilder


page.
14(a). 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:
 

The method new is used to denote the constructor for each class.

State the purpose of the constructor.



[1]
 

  (b). The classes office and house inherit from building.

Describe what is meant by inheritance with reference to these classes.





[2]
 

  (c). 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 ………………………………………………………………………

© OCR 2022. You may photocopy this Page 21 of 23 Created in ExamBuilder


page.
    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
 
[5]
 

  (d). 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.

















[6]
 

  (e). 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().



© OCR 2022. You may photocopy this Page 22 of 23 Created in ExamBuilder


page.





[4]
 

  (f). 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.








[4]
 

END OF QUESTION PAPER

© OCR 2022. You may photocopy this Page 23 of 23 Created in ExamBuilder


page.

You might also like