0% found this document useful (0 votes)
96 views5 pages

Time: 09.00-13.00: ECS658U Further Object Oriented Programming Test: 18 November 2020

This document provides instructions for a test on further object oriented programming. It consists of 10 questions worth a total of 100 marks. Students are asked to write their answers in a single file with clear labeling of each answer, and submit a PDF version of the file. The questions cover topics like encapsulation, object references, inheritance, polymorphism, and data structures. Students are asked to write methods and explain concepts, but not run any code as part of the test.

Uploaded by

Harshil Chordia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views5 pages

Time: 09.00-13.00: ECS658U Further Object Oriented Programming Test: 18 November 2020

This document provides instructions for a test on further object oriented programming. It consists of 10 questions worth a total of 100 marks. Students are asked to write their answers in a single file with clear labeling of each answer, and submit a PDF version of the file. The questions cover topics like encapsulation, object references, inheritance, polymorphism, and data structures. Students are asked to write methods and explain concepts, but not run any code as part of the test.

Uploaded by

Harshil Chordia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

ECS658U Further Object Oriented Programming

Test: 18 November 2020


Time: 09.00-13.00
There are 10 questions: answer every question.
For this year, how you should answer the questions is to make one file with all your answers
on it. Make it clear which question each answer you have written is an answer for. Make a
pdf version of your answer file, and upload it.
Note, where questions contain code you are meant just to write the answers by hand, not
compile or run any code, as you would have to if this was a test or exam in the normal form,
done sitting at table with paper to write your answer on.

Each question is worth 10 marks. Total 100 marks.

Question 1)

a) A basic aspect of good quality Object Oriented Programming is keeping a clear distinction
between WHAT an object does and HOW it does it. Explain what this means and why it
is important.
[6 marks]

b) Explain what is needed in Java to keep a clear distinction between WHAT an object does
and HOW it does it.
[4 marks]

Question 2)

a) A basic aspect of Object Oriented Programming that is fundamental to both Java and
Python is the concept of a variable referring to an object. Explain what this means, and
illustrate that by showing what happens when an assignment obj1=obj2 takes place,
where both obj1 and obj2 are variables of an object type.
[6 marks]

b) One way in which Java differs from Python is that Java has variables of a primitive type,
while in Python all variables are of an object type. Explain what this means.
[4 marks]

Question 3)

a) Another important aspect of Object Oriented Programming is inheritance. Explain what


this means both in terms of how classes that define objects are written, and how variables
in Java have types that indicate what objects they can refer to.
[5 marks]

b) Inheritance leads to a concept called dynamic binding. Explain what this means in terms
of how code works, and also why it can assist in the production of good quality code.
Explain also why precaution may need to be taken due to the way it works, with the
Liskov Substitution Principle being a general advice on this precaution.
[5 marks]
Question 4)

For question 4) and question 5) suppose we have a Java class Alpha. The class Alpha is
defined as having a natural order, that is, it implements Comparable<Alpha> and has a
method with header
public int compareTo(Alpha alf)
in it. Also, it has methods with the following headers:
public Alpha join(Alpha alf) throws JoinException
public boolean testWith(Date d)
Suppose also we have a class Beta and a class Gamma, both of which are declared as
extends Alpha.

a) Write a static method which takes a Date object, a List of objects of type Alpha or
one of its subclasses, and an object of the same type as the content type of the List.
referred to by a parameter called tester. What it must do is return the lowest indexed
object in the List where calling testWith on it with the Date argument returns true
and also that object is greater than the one referred to by tester in terms of natural
order. If there is no such object, it should instead return instead what tester refers to.
[6 marks]

b) Write a static method that takes an Alpha object and a List of objects of type
Alpha or one of its subclasses. It must return the result of calling join on the Alpha
object with its argument the lowest indexed object of the List where doing that does not
throw a JointException. If there is no object that can be returned, then the method
should return the Alpha object itself.
[4 marks]

Question 5)
a) Write a method to go in class Beta which overrides the join method as follows. If the
argument to join is not of type Beta, then it should just return the Beta object that it
was called on. Otherwise, it should use the join code it inherits to join the Beta object
argument to the Beta object it is called on and return the result.
[6 marks]

b) Suppose the equals method has been overridden in Alpha, and also overridden again in
Gamma. Explain what this means when a call a1.equals(a2) is made, where a1 and
a2 are both variables declared as type Alpha.
[2 marks]

c) Explain briefly why if there was a method in class Beta with header
public equals(Beta b)
that would not be a correct away for Beta to override equals.
[2 marks]
Question 6)
Assume there is an interface Player given by:
interface Player {
Move choose(Position pos);
int cost(Move m);
Position play(Move m);
}

Write a class TwoPlayer which implements Player using the composite design pattern
to treat two Player objects as a single Player object. The two Players are provided by
the constructor for TwoPlayer. The way it should work is that both Players choose a
Move for a Position, and the Move with the lowest cost is chosen. Different Players
have different costs for the same Move, the method cost should return the lowest cost. One
Player may be asked to play the Move chosen by the other Player if the other Player
had a higher cost for it.
[10 marks]

Question 7)
Assume we also have the interface Game given by:
interface Game {
Set<Position> positions();
boolean make(Move m);
Move randomMove();
Move takeFrom(Position p);
int cost(Position p);
Position tryPlay(Player player, Move m)
}

Write a class which called GamePlayer which implements Player by taking a Game as
the argument to its constructor and storing it.
Then the way the method choose works is that if pos is in the set given by
positions(), it returns the result of giving it as the argument to takeFrom called on the
Game object. Otherwise it returns the result of calling randomMove() on the Game object.
The way the method play works is to call tryPlay on the Game object, with the Player
object that play was called on as its first argument, and the argument to the play method
as its second argument, and it returns what that returns.
The way the method cost works is that if using its argument as the argument to make on
the Game object returns true, it returns the cost of the Position given by using its
argument as the argument to the method play. The cost of the Position is given by
giving as the argument to the method cost called on the Game object. Otherwise, if using
the argument to cost as the argument to make returns false, the method should return 100.
[10 marks]
Question 8)
The type LispList<T> is completely defined by the following methods:
public T head() returns the first element of the list it is called on.
public LispList<T> tail() returns a new list consisting of all the
elements of the list it is called on except the first, in the same order they were in
that list.
public LispList<T> cons(T item) returns a new list consisting of
item as its first element followed by all the elements from the list it is called
on in the same order they occurred in that list.
public boolean isEmpty() returns true if the list it is called on is empty
(contains 0 elements), and false otherwise.
public static LispList<T> empty() returns an object representing an
empty list.

a) Write a static method cheapest that takes a LispList<Player> and a Move where
Player and Move are as given for Question 6. The method must return the Player from
the list which has the lowest cost for the Move as given by calling cost on the Player with
the Move object as its argument. The method must use recursion and not iteration, and it
must not use any other data structure apart from LispList.
[4 marks]

b) Write a static method between which performs the following operation. It takes a
LispList of any base type, and two objects of that base type. It returns a LispList,
containing all the elements in the LispList argument between the first occurrence of the
first object and the next occurrence of the second object in the same order. So, for example
between(list,10,20) where list refers to [5,7,10,15,30,42,20,12,9]
should return [15,30,42]. The method must use recursion and not iteration, and it must
not use any other data structure apart from LispList.
This question does not say what to do if the first object does not occur or the second object
does not occur after it. So when you have written your code, write an explanation in English
of what your code would do under those circumstances.
[6 marks]

Question 9)
a) The type LispList<T> as given in Question 8) is an example of an immutable data type.
Explain what is meant by that, and why it can be a good aspect of a data type.
[5 marks]
b) Explain how the Object Pool Design Pattern works, and why one reason for using it is that
it makes sense to use it for dealing with requests for immutable objects
[5 marks]
Question 10)
Write a brief explanation of each of the following issues: [2 marks for each part]
a) Why Java provides two related classes: HashSet<E> and TreeSet<E>
b) How the classes HashMap<K,V> and TreeMap<K,V> work, and how they are related
to the classes HashSet<E> and TreeSet<E>.
c) How an object of type Comparator<T> works, and why it can be used as an
argument for a constructor of TreeSet<E> or TreeMap<K,V>.
d) Question 5) asks about overriding equals in the class Alpha. Explain why doing
that could cause an affect to how a collection of type HashSet<Alpha> works,
and how a collection of type TreeSet<Alpha> would work.
e) The effect of overriding equals in Alpha only has the effect on a
HashSet<Alpha> if hashCode() is also overridden so that if
a1.equals(a2) is true, a1.hashCode() and a2.hashCode() will
return the same value. Explain what this means.

You might also like