Primary Examination, Semester 1, 2019
Object Oriented Programming
COMP SCI 1102, 1102UAC
Official Reading Time: 10 mins
Writing Time: 120 mins
Total Duration: 130 mins
Questions Time Marks
Answer all 9 questions 120 mins 120 marks
120 Total
Instructions for Candidates
• This is a closed-book examination.
• Begin each answer on a new page.
• Examination material must not be removed from the examination room.
Materials
• Calculator without network capability permitted.
• Paper dictionaries and translation dictionaries permitted.
DO NOT COMMENCE WRITING UNTIL INSTRUCTED TO DO SO
COMP SCI 1102, 1102UAC Page 2 of 13
Primary Examination, Semester 1, 2019
Computer Architecture, Memory and Data Representation
Question 1
(a) The basic layout of memory can be divided into four main areas. What are
they and what are they used for?
[6 marks]
(b) Give a short description to explain each of the following statements, including
any terms used.
i. We sometimes have to defragment the heap.
[2 marks]
ii. Local variables within a function do not need to be explicitly deleted when
you return from that function.
[2 marks]
(c) What is the decimal number 126 in binary notation? Note: the most signifi-
cant bit should be on the left hand side. Show your working.
[2 marks]
(d) What is the decimal value of the hexadecimal number 0xFE? Show your working.
[3 marks]
[Total for Question 1: 15 marks]
PLEASE SEE NEXT PAGE
COMP SCI 1102, 1102UAC Page 3 of 13
Primary Examination, Semester 1, 2019
Pointers
Question 2
(a) Provide a single line of C++ code that will declare a variable that is a pointer
to an int value, where the variable is named i1.
[2 marks]
(b) You have array exam19 declared as
int *exam19[6];
Give the single line of C++ code that will assign the int pointer stored in
variable i1 (from above) to the last element of the array exam19.
[4 marks]
(c) Write a for loop that initialises the contents of each element of the array
exam19 in the previous question to −1.
[2 marks]
(d) Write a C++ expression that will calculate the address of the third element of
the array exam19.
[2 marks]
(e) Given the following declarations, write code to exchange the addresses stored
in x and y. You may add any additional declarations you wish.
int *x = new int(3);
int *y = new int(4);
[5 marks]
[Total for Question 2: 15 marks]
PLEASE SEE NEXT PAGE
COMP SCI 1102, 1102UAC Page 4 of 13
Primary Examination, Semester 1, 2019
Arrays
Question 3
(a) Using a diagram, show how array values are stored in memory.
[2 marks]
(b) You pass an array to a function as a parameter. What could happen if you do
not pass in the length of the array as well?
[2 marks]
(c) Write the code to declare and initialise an integer array with the values 3, 9,
12, 20.
[2 marks]
(d) Write a function with the signature:
int* initarr(int size1)
That allocates a new array of ints with size1 elements and initialises every
element of an array to the square of its index value. The function should then
return a pointer to the new array.
As an example of how the initialisation should work, the contents of a new
array with four elements would be 0, 1, 4, 9.
[4 marks]
[Total for Question 3: 10 marks]
PLEASE SEE NEXT PAGE
COMP SCI 1102, 1102UAC Page 5 of 13
Primary Examination, Semester 1, 2019
Object Oriented Programming
Question 4
(a) In the declaration below, identify the class and the object
Dog classyDog;
[2 marks]
(b) Why do we provide accessor methods rather than just making the member
variables public? Which OOP property do accessors support?
[3 marks]
(c) Objects capture state and behaviour. Explain briefly how objects provide this.
[4 marks]
(d) Explain how a static class variable differs from a member variable.
[2 marks]
(e) Explain when each of the access modifiers (public, private, protected) should
be used.
[3 marks]
(f) You have been asked to produce an initial design for a library software catalogue.
The library contains a wide range of books. Patrons borrow books
from the library and have three weeks to return them. Returned books are
held in a storage room until they are returned to the shelves by staff. Produce
a diagram showing the fundamental state and behaviour of the objects in this
system. Only show those elements that are essential to understanding the
system.
[6 marks]
[Total for Question 4: 20 marks]
PLEASE SEE NEXT PAGE
COMP SCI 1102, 1102UAC Page 6 of 13
Primary Examination, Semester 1, 2019
Classes
Question 5
(a) This piece of code is from a class called Exams. The section marked ... represents
code that has been removed for this example.
Exams::Exams() { ... }
What is the name of this kind of method and what is it used for?
[2 marks]
(b) Write a destructor for the class Panda, which displays “Panda object deleted”
when the destructor is invoked.
[2 marks]
(c) Write a class for an object called cup that represents a coffee cup. cup should
have a function fill() which will set the cup’s state to full and a function
drain() that sets it back to empty. Write the class declaration and the implementation
for the cup class.
[6 marks]
[Total for Question 5: 10 marks]
PLEASE SEE NEXT PAGE
COMP SCI 1102, 1102UAC Page 7 of 13
Primary Examination, Semester 1, 2019
Inheritance and Abstract Classes
Question 6
Consider the following code defining a hierarchy of Vehicle and Cars:
// class definition of Vehicle
class Vehicle{
public:
Vehicle(string id, int dist);
virtual void travel(int amount)=0;
virtual void print_details();
protected:
string id;
int dist_travelled;
};
// constructor taking id and distance
Vehicle::Vehicle(string id, int dist){
this->id=id;
dist_travelled=dist;
}
void Vehicle::print_details(){
cout << "vehicle id: " << id << " dist travelled: " <<
dist_travelled << endl;
}
// class definition of Car
class Car : public Vehicle{
public:
Car(string id, int dist,string model);
virtual void travel(int amount)=0;
virtual void print_details();
protected:
string model;
};
Car::Car(string id, int dist, string model):Vehicle(id,dist){
this->model=model;
}
void Car::print_details(){
Vehicle::print_details();
cout << " model name: " << model << endl;
}
PLEASE SEE NEXT PAGE
COMP SCI 1102, 1102UAC Page 8 of 13
Primary Examination, Semester 1, 2019
Now, answer the following questions:
(a) The implementation of the constructor for Car is:
Car::Car(string id, int dist, string model):Vehicle(id,dist){
this->model=model;
}
Explain precisely what the code: :Vehicle(id,dist) above does.
[2 marks]
(b) The implementation of the print details method for Car is:
void Car::print_details(){
Vehicle::print_details();
cout << " model name: " << model << endl;
}
Explain what the code: Vehicle::print_details(); above does.
[1 mark]
(c) The driver code:
// make a car and make it travel
Car *my_car = new Car("123BRAD",30000,"Velox");
my_car->print_details();
my_car->travel(200);
my_car->print_details();
Has an errors in it. Explain precisely what is wrong with the code above.
[2 marks]
PLEASE SEE NEXT PAGE
COMP SCI 1102, 1102UAC Page 9 of 13
Primary Examination, Semester 1, 2019
(d) Consider the following class definition, which describes a sub-class of Car
representing electric cars:
// class definition of an electric car
class ECar: public Car{
public:
ECar(string id, int dist,string model,int range);
virtual void travel(int amount);
virtual void print_details();
virtual void fill();
protected:
int range; // range of car in kms
int remaining; // remaining range in kms
};
Answer the following questions
i. Write a C++ implementation of the constructor of ECar. Make sure you
call the constructor of the Car class as appropriate and initialise all the
relevant variables to sensible values.
[4 marks]
ii. Write a C++ implementation of the print details method of ECar. This
method should make use of the print details method of the super-class
as appropriate. In addition to the same details as printed by the Car
classes print details method, your method should also print the range
and the remaining range of the car in a well-formatted way.
[4 marks]
iii. Write a C++ implementation of the travel method of ECar. This method
updates remaining and dist travelled according to the amount parameter.
Note that, if the remaining range of the car is less than amount then
you will only be able to travel for the remaining range – make sure your
code implements this logic.
[6 marks]
[Total for Question 6: 19 marks]
PLEASE SEE NEXT PAGE
COMP SCI 1102, 1102UAC Page 10 of 13
Primary Examination, Semester 1, 2019
Polymorphism
Question 7
Consider the following C++ code defining an Animal class and a Lion sub-class of
Animal:
class Animal{
public:
Animal(string name);
virtual void speak();
protected:
string name;
};
//constructor for Animal
Animal::Animal(string name){
this->name=name;
}
void Animal::speak(){
cout << name << " makes default sound" << endl;
}
// class for lion
class Lion: public Animal{
public:
Lion(string name, string sound);
virtual void speak();
protected:
string sound;
};
Lion::Lion(string name, string sound):Animal(name){
this->sound=sound;
}
void Lion::speak(){
cout << name << " the lion says " << sound << endl;
}
PLEASE SEE NEXT PAGE
COMP SCI 1102, 1102UAC Page 11 of 13
Primary Examination, Semester 1, 2019
Now, given the definition above, answer the following questions:
(a) What is the output of the of the following driver code?
Lion *leo = new Lion("Leo","Ger");
Lion *bella = new Lion("Bella","Roar");
Animal* zoo[2];
zoo[0]=leo;
zoo[1]=bella;
zoo[0]->speak();
zoo[1]->speak();
[4 marks]
(b) What would the output of the driver code from part a) above be if the speak
function were not declared virtual in the Animal class?
[4 marks]
(c) Consider the following driver code for the Animal and Lion classes at the
beginning of this question:
// declare a lion and make it speak
Lion leo("Leo","Ger");
leo.speak();
// make an animal and speak
Animal animal1("generic animal 1");
animal1.speak();
// assign leo to animal1 and speak
animal1=leo;
animal1.speak();
When this code is run it produces the following output:
Leo the lion says Ger
generic animal 1 makes default sound
Leo makes default sound
With the aid of diagrams, explain how the driver code produces the last line
of output above.
[4 marks]
[Total for Question 7: 12 marks]
PLEASE SEE NEXT PAGE
COMP SCI 1102, 1102UAC Page 12 of 13
Primary Examination, Semester 1, 2019
Standard Template Library
Question 8
(a) Briefly describe two advantages of using the Standard Template Library.
[2 marks]
(b) Briefly describe what is wrong with the following code using the vector template
from the Standard Template Library.
vector myInts;
myInts.push_back(13);
myInts.push_back(14);
myInts.push_back(15);
myInts[100]=25;
cout << myInts.back() << endl;
cout << myInts[1000] << endl;
[2 marks]
(c) Briefly describe two advantages of vectors over arrays.
[2 marks]
(d) Briefly describe the difference between a set and a map in the Standard Template
Library.
[2 marks]
[Total for Question 8: 8 marks]
PLEASE SEE NEXT PAGE
COMP SCI 1102, 1102UAC Page 13 of 13
Primary Examination, Semester 1, 2019
Complexity and Problem Solving Strategy
Question 9
(a) We have calculated a function of the runtime (measured in terms of basic
instructions executed) of an algorithm for an input of size n to be:
f(n) = 1000 + n + 3n
2 + 2n
3
Demonstrate that this algorithm has a big-O runtime complexity of O(n
3
).
[3 marks]
(b) Give an example of when the use of a Brute Force algorithm might be preferred
over other algorithms.
[2 marks]
(c) You have an unsorted array of 1000 numbers and you want to remove the duplicate
numbers from the array. Precisely describe two alternative algorithmic
approaches to doing this – one of which is, in terms of time complexity, more
efficient than the other.
[6 marks]
[Total for Question 9: 11 marks]
END OF EXAMINATION PAPER