Exam code: 9618
1/1150Still learning
Know0
Define class.
A class is a blueprint used to create objects, defining a structure for attributes (data) and methods (behaviour).

Join for free to unlock a full flashcard set, track what you know,
and turn revision into real progress.
Define object.
An object is an instance of a class with its own state and behaviour.
Define attribute.
An attribute is a variable defined in a class, also called a class variable.
Was this flashcard helpful?
Define class.
A class is a blueprint used to create objects, defining a structure for attributes (data) and methods (behaviour).
Define object.
An object is an instance of a class with its own state and behaviour.
Define attribute.
An attribute is a variable defined in a class, also called a class variable.
Define instance variable.
An instance variable is an attribute stored inside an individual object.
Define method.
A method is a function defined within a class that describes an object's behaviour.
Define instantiation.
Instantiation is the process of creating an object from a class.
Define identifier.
An identifier is the name used to refer to an object.
A class defines a structure for , which hold the data, and methods, which define the behaviour.
A class defines a structure for attributes, which hold the data, and methods, which define the behaviour.
What is the difference between a prebuilt class and a custom class?
A prebuilt class is provided by the language, such as String or Scanner in Java, while a custom class is defined by the programmer, such as Animal or Student.
Give two examples of prebuilt Java classes.
Two prebuilt Java classes are String and Date, and Random and Scanner are also prebuilt.
True or False?
Every object created from the same class must hold the same attribute values.
False.
Each object created from a class has its own unique values for the attributes, so two Student objects can hold different names and dates of birth.
State three attributes a Student class might contain.
A Student class might contain the attributes name, dateOfBirth and gender.
Define constructor.
A constructor is the method used to create the objects of a class, setting the initial values of the attributes.
A Cars class is written for a car sales program. State the five attributes it needs.
The five attributes are the manufacturer, model, price, mileage and whether the car is preowned.
Which data type would the PreOwned attribute of a Cars class use, and why?
It would use a Boolean, because a car either is or is not preowned.
Name two methods that would be useful for a car object.
Accelerate, to make the car go faster, and honkHorn, to sound the horn.
When a class is written so that its attributes cannot be accessed from outside it, those attributes are declared as .
When a class is written so that its attributes cannot be accessed from outside it, those attributes are declared as private.
A Vehicle class has the attributes ID, MaxSpeed, CurrentSpeed, IncreaseAmount and HorizontalPosition. Which of these should the constructor set to 0 rather than take as a parameter?
CurrentSpeed and HorizontalPosition are set to 0, because a vehicle starts stationary at the origin, while ID, MaxSpeed and IncreaseAmount are passed in as parameters.
True or False?
In Python, attribute declarations for a class are written as comments when answering an exam question.
True.
Python has no separate attribute declaration, so the exam requires the declarations to be included as comments, for example #self. MaxSpeed integer.
What is the purpose of the IncreaseAmount attribute in the Vehicle class?
IncreaseAmount is the value added to the current speed each time the vehicle increases its speed.
Define object.
An object is a representation of a real-world entity, such as a teacher, an aeroplane or a mobile phone.
What is the difference between a class and an object?
A class is a blueprint that describes the properties and behaviours of objects, while an object is a specific instance created from that blueprint, with its own unique values for the properties.
Define constructor.
A constructor is a special method within a class that is automatically called when an object of that class is created.
What do constructors typically define?
Constructors typically define the initial values of the instance variables.
As well as setting initial values, a constructor performs any necessary to prepare the object for use.
As well as setting initial values, a constructor performs any necessary setup to prepare the object for use.
Give three examples of real-world entities that could be modelled as objects.
A teacher, an aeroplane and a mobile phone could all be modelled as objects.
True or False?
Two objects created from the same class always share one set of property values.
False.
Each object is a separate instance with its own unique values for the properties.
In the Person class example, name the four attributes.
The four attributes are firstName, surname, dateOfBirth and hobbies.
In Java, an object is created from a class using the keyword.
In Java, an object is created from a class using the new keyword.
How many objects are instantiated in the Person example, and what are their identifiers?
Two objects are instantiated, with the identifiers person1 and person2.
What values are passed in when person1 is created?
person1 is created with "Bob", "Jones", "06/10/1981" and "E Sports".
In Python, which method acts as the constructor for a class?
The init method acts as the constructor in Python.
True or False?
In the Java Person example, all four attributes are declared as private.
True.
Each attribute is declared with the private access modifier, so it cannot be accessed directly from outside the class.
In Python, how does the constructor assign a parameter to an attribute of the object?
It uses the self keyword, for example self.firstName = firstName.
Define method.
A method is a function associated with an object or class that defines the behaviour and actions the object can perform.
What are the two main types of method?
The two main types of method are a function and a procedure.
What is the difference between a function and a procedure?
A function performs a task and returns a value, while a procedure performs a task but does not return a value.
True or False?
A procedure returns a value to the code that called it.
False.
A procedure performs a task but does not return a value, which is what distinguishes it from a function.
Because they are associated with objects rather than the class itself, these methods are called methods.
Because they are associated with objects rather than the class itself, these methods are called instance methods.
What notation does an object use to call a method from its class?
It uses dot notation, in the form Objectname.Methodname.
An object jumboJet has been created from the aircraft class. Write the statement that calls its take off method.
The statement is jumboJet.Takeoff().
Name the four methods of the aircraft class.
The four methods are take off, land, bank left and bank right.
Name the four attributes held by each object of the aircraft class.
Each object holds a manufacturer, a model, a passenger capacity and a maximum speed.
Why is a class a good way to model aircraft, given that aircraft differ so much in design?
Although the designs differ, every aircraft requires the same methods, such as take off and landing, so one class can define that shared behaviour.
Define public method.
A public method is accessible and can be invoked by any code within the same class or from any external class.
Define private method.
A private method is only accessible within the same class and cannot be invoked by external code or other classes.
True or False?
A private method can be invoked by code in a different class.
False.
A private method can only be called from within its own class.
Why must changes to public methods be made carefully?
Changes to public methods may affect other parts of the codebase, so they should be carefully designed, documented and kept backward compatible wherever possible.
Why is a private method easier to modify or refactor than a public one?
A private method has a localised impact, because it is only used internally within its own class.
When would you make a method public?
When you want to provide other parts of the program with access to a particular functionality or behaviour of the object or class.
When would you make a method private?
When it holds internal implementation details that should not be accessed or used by external code.
In Python, a method whose name begins with a single leading is treated as private by convention.
In Python, a method whose name begins with a single leading underscore is treated as private by convention.
Does Python enforce private methods in the way Java does?
No. Python has no explicit access modifier, so a method marked as private can still be accessed from outside the class, but other developers should treat it as private and avoid doing so.
What does the statement jumboJet = new aircraft("Boeing", "747", 416, 547) do?
It creates an object called jumboJet from the aircraft class, giving it a manufacturer of Boeing, a model of 747, a capacity of 416 and a speed of 547.
Define attribute.
An attribute is a data member, or property, associated with an object or a class.
What do the attributes of a class define?
The attributes define the state of an object.
True or False?
Every instance of a class must hold the same values for its attributes.
False.
Attributes can hold different values for different instances of the same class.
What data types can an attribute be?
An attribute can be any data type, including integers, strings, Booleans or even other objects.
As well as a data type, attributes can be given different rights.
As well as a data type, attributes can be given different access rights.
A Car class has an attribute called manufacturer with private access and a String data type. What do those two things mean?
Private access means it can only be accessed within the Car class, and the String data type means the value it holds must be text.
An object of the Car class is instantiated with the value "Ford". Which attribute holds that value?
The manufacturer attribute holds the value "Ford".
How many attributes does a class usually have?
In most cases a class has many different attributes, so a single-attribute class is unusual.
Name the five attributes of the Java Person class.
The five attributes are name, age, gender, occupation and isMarried.
In the Java Person class, which attribute is declared as a boolean, and which as an int?
isMarried is declared as a boolean and age is declared as an int.
In Python, attributes are defined using the keyword, followed by the attribute name and its initial value.
In Python, attributes are defined using the self keyword, followed by the attribute name and its initial value.
True or False?
In the Java Person class, the attributes are declared as private.
True.
All five attributes are declared with the private access modifier.
Define inheritance.
Inheritance allows a class to inherit the properties and behaviours, that is the attributes and methods, of another class.
How does inheritance promote code reuse?
A derived class can inherit and use the existing code from the base class, which avoids duplicating code and improves organisation and maintainability.
Inheritance establishes an relationship between the base class and the derived class.
Inheritance establishes an IS-A relationship between the base class and the derived class.
A Car class is derived from a Vehicle class. State the IS-A relationship between them.
A Car is a Vehicle, so the Car class inherits the properties and behaviours associated with being a vehicle.
Give two other names for the base class.
The base class is also known as the parent class or the superclass.
Give two other names for the derived class.
The derived class is also known as the child class or the subclass.
What is the role of the base class in an inheritance hierarchy?
The base class is the blueprint or template from which the derived class inherits, defining the common properties and behaviours shared among multiple derived classes.
True or False?
A derived class can only use what it inherits, and cannot add anything of its own.
False.
A derived class inherits the attributes and methods of the base class, but it can also add additional attributes and methods of its own.
A Car class inherits from Vehicle. Name two attributes the Car class adds for itself.
The Car class adds IsInsured, which records whether the car is insured, and EngineCapacity, which records the size of the engine.
Name three methods a Vehicle base class might define.
TurnEngineOn(), TurnEngineOff() and SteerLeft() are all methods a Vehicle base class might define.
Define the super keyword.
The super keyword is used within a subclass to refer to the superclass and access its methods, attributes or constructors.
In Java, a derived class inherits from a base class using the keyword .
In Java, a derived class inherits from a base class using the keyword extends.
In Python, how does a class show that it inherits from another class?
The parent class name is written in brackets after the child class name, for example class Car(Vehicle):.
Why does the constructor of a derived class call the constructor of its base class?
So that the attributes belonging to the base class are initialised, before the derived class sets its own additional attributes.
A Helicopter class inherits from Vehicle and adds VerticalPosition, VerticalChange and MaxHeight. Which of these does the constructor set to 0?
VerticalPosition is initialised to 0, while VerticalChange and MaxHeight are set from the parameters passed in.
The Helicopter constructor takes five parameters. Which three are passed straight up to the Vehicle constructor?
The ID, the maximum speed and the increase amount are passed up to the Vehicle constructor, because they belong to the parent class.
True or False?
The attributes a derived class declares for itself should be private.
True.
The Helicopter class declares VerticalPosition, VerticalChange and MaxHeight as private, in the same way the parent class declares its own attributes.
Define encapsulation.
Encapsulation is the practice of grouping data (attributes) and methods (functions) together within a class.
How does encapsulation keep data secure?
It controls access to the data using access modifiers such as public and private, so the data cannot be accidentally modified or misused.
State two benefits of encapsulation other than security.
It organises code by keeping related data and methods together, and it promotes code reusability, so the same class can be used in different parts of a program without rewriting it.
Encapsulation uses a concept called , which reduces complexity by hiding the implementation details of the object.
Encapsulation uses a concept called abstraction, which reduces complexity by hiding the implementation details of the object.
Why does hiding the internal details of a class help other programmers?
They can use the methods and classes without having to understand how they have been constructed internally.
True or False?
External code can access a private variable of a class directly.
False.
Private variables are only accessible within the class itself, and external code must interact with the class through its public methods.
Define get method.
A get method is used to retrieve the value of an object's private attribute, and is also known as a getter or accessor method.
Define set method.
A set method is used to set the value of an object's private instance variable, and is also known as a mutator method.
What is the main purpose of a get method?
To provide controlled access to the internal state of an object without allowing direct modification.
Why is extra code often written inside a set method?
To enforce conditions or validations before the attribute is updated, so that access is controlled.
A NumberUpdater class has a private attribute oldnumber and a set method that rejects negative values. What happens if the method is passed -3?
oldnumber is left unchanged, because the method only updates the attribute when the new value is not less than zero.
Because attributes are declared private, external code must use the public methods to read their values.
Because attributes are declared private, external code must use the public get methods to read their values.
How many parameters does a get method take, and what does it do with the attribute?
A get method takes no parameters and returns the attribute without overwriting it.
The Vehicle class stores CurrentSpeed privately. Write, in words, what GetCurrentSpeed() must do.
It must return the value held in the CurrentSpeed attribute, taking no parameters and making no change to it.
True or False?
A set method returns the value of the attribute to the code that called it.
False.
A set method updates the attribute, while it is the get method that returns its value.
Define polymorphism.
Polymorphism allows objects to take on different forms or behaviours, so different objects can share the same method name but work in different ways.
State two benefits of using polymorphism.
Polymorphism makes code more flexible and more reusable, which also makes it easier to maintain.
How does polymorphism make a program more versatile?
Objects can be treated as belonging to a common group even when they belong to different classes, so the code adapts more easily to change.
The keyword is used to provide a new implementation for a method that is already defined in the parent class.
The override keyword is used to provide a new implementation for a method that is already defined in the parent class.
A Motorcycle class and a Car class both inherit from Vehicle and neither redefines startEngines(). What is output when either object calls it?
Both output "Engines Started!", because they inherit the same method from the base class.
A Motorcycle object calls displayInfo(), which is defined in both Vehicle and Motorcycle. What is output, and why?
"I am a Motorcycle!" is output, because the version in the object's own class overrides the Vehicle class method.
What does the declaration Vehicle vehicle1 = new Car() allow a programmer to do?
It allows an array of type Vehicle to store both Car and Motorcycle objects, rather than keeping them in separate data structures.
vehicle1 is declared as type Vehicle but created as a Car. What does vehicle1.displayInfo() output?
It still outputs "I am a Car!", because the version that runs depends on the object actually created, not the declared type.
Define run-time polymorphism.
Run-time polymorphism happens when a subclass overrides a method defined in a superclass, and the correct version is chosen at run-time rather than compile-time.
In the Animal, Dog and Cat example, what does the speak() method in the Animal superclass contain?
It is empty, acting only as a placeholder for the subclasses to override.
myDog and myCat are passed in turn to make_sound(). What is output each time?
make_sound(myDog) outputs Woof and make_sound(myCat) outputs Meow.
True or False?
A separate version of make_sound() has to be written for each type of animal.
False.
One make_sound() accepts an object of type Animal and calls the correct version of speak() for whichever subclass was passed to it.
In run-time polymorphism the method call is resolved based on the actual of the object.
In run-time polymorphism the method call is resolved based on the actual type of the object.
In Java, what marks a method as overriding one in its superclass?
The @Override annotation is written above the method header.
The Helicopter class overrides IncreaseSpeed() from Vehicle. State the three things the overriding method must do.
It adds the vertical change to the vertical position, adds IncreaseAmount to the current speed, and adds the updated current speed to the horizontal position.
What two limits must the Helicopter version of IncreaseSpeed() enforce?
The vertical position cannot exceed the maximum height, and the current speed cannot exceed the maximum speed.
True or False?
The Helicopter version of IncreaseSpeed() takes no parameters.
True.
An overriding method keeps the same header as the method it overrides, and IncreaseSpeed() has no parameters.
By signing up you agree to our Terms and Privacy Policy