OOP Exam Style Questions
-
A taxi firm is investigating replacing its drivers with self-driving cars.
-
Explain why the self-driving system will use a real-time operating system.
- Self-driving systems need to process input as it is generated, so that they can optimise their route and avoid obstacles; due to their real-time processing of data, they fit the definition of a real-time system.
-
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 forObstacle
is shown below.public class Obstacle private moving // bool private distance //real number in metres private direction // int between 1 and 360 (degrees) public procedure new(givenMoving, givenDistance, givenDirection) moving = givenMoving distance = givenDistance direction = givenDirection endprocedure public procedure updateDistance(givenDistance) distance = givenDistance endprocedure endclass
- Write a line of code to create an object called
bollard
of typeObstacle
which is not moving and is 7.8 metres away in a direction of 8 degrees.bollard = new Obstacle(false, 7.8, 8)
- Describe an example of encapsulation in the class definition code above.
distance
is not modifiable directly, only through the class' ownupdateDistance
method
- Describe the advantages of using encapsulation.
- Encapsulation helps prevent a class from being modified in ways which could cause the program to crash or otherwise misbehave
- Write a line of code to create an object called
-
The self-driving program recognises people as a special type of obstacle and the class
Person
should inherit the methods and attributes ofObstacle
. People are treated like other obstacles except:- When the
updateDistance
method is called, if the person is more than 2 metres away but is 5 metres (or less) away, the methodControls.beepHorn()
is called. - When the person is 2 metres away (or closer), the method
Controls.applyBrakes()
is called as well asControls.beepHorn()
Complete the class
Person
.class Person inherits Obstacle public procedure updateDistance(givenDistance) if givenDistance <= 5 then Controls.beepHorn() endif if givenDistance <= 2 then Controls.applyBrakes() endif distance = givenDistance endprocedure endclass
- When the
-
Give one advantage and one disadvantage to the customers of the taxi using self-driving cars rather than drivers.
Advantage
- The customer will likely have a more efficient route, and therefore a faster journey
Disadvantage
- The customer may have issues where the car goes to the wrong place, and with no driver to talk to would be unable to correct the destination efficiently
-
-
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:
class Lizard private speed private mass private size public procedure new(givenSpeed, givenMass, givenSize) speed = givenSpeed mass = givenMass size = givenSize endprocedure public function breakBlock(brick) if speed * mass > brick.getStrength() then speed = ((speed * mass) - brick.getStrength()) / mass return true else return false endif endfunction ... endclass
- Lizard is a class. Describe what is meant by a class.
- A class is a data structure with methods and attributes used to model a real-world object.
- Identify an attribute in the Lizard class.
speed
-
- Describe what is meant by the term inheritance.
- Inheritance is the process by which a class derives methods and attributes from its superclasses; all methods and attributes from the superclasses are copied into the class, generally with precedence given to the first class in the superclasses; i.e. if two classes define the same method, the first in the inheritance list will give its method to the class.
- Explain one way the game's developers might use inheritance for Livid Lizards.
- Lizards with special powers could be derived from
Lizard
, inheriting its attributes and methods but being able to override methods for which a special behaviour is wanted.
- Lizards with special powers could be derived from
- Describe what is meant by the term inheritance.
- Lizard is a class. Describe what is meant by a class.