Exam 2017 Questions
Exam 2017 Questions
G51PGP-E1
PROGRAMMING PARADIGMS
Dictionaries are not allowed with one exception. Those whose first language
is not English may use a standard translation dictionary to translate between
that language and English provided that neither language is the subject of this
examination. Subject specific translation dictionaries are not permitted.
2 G51PGP-E1
a) Fill in the missing parts ? in the following definitions to make them type correct.
It does not matter what they do as long as they are type correct: (5 marks)
e1 :: ?
e1 = [True,False,True]
e2 :: ?
e2 = (1,2,3)
e3 :: a -> (a,a,a)
e3 ? = ?
b) Write down the most general types for the following functions: (5 marks)
one x = [x]
two x = (x,x)
three xs = xs ++ xs ++ xs
first x y = x
inc x = x+1
c) Define the following library functions using recursion. Each is defined in the
attached standard prelude, but not directly using recursion. (10 marks)
fac 0 = 1
fac n = n * fac (n-1)
3 G51PGP-E1
a) Complete the missing parts ? in the following definition for a recursive function
that inserts an integer into the correct position in a sorted list. For example,
insert 3 [1,2,4,5] should return [1,2,3,4,5]. (5 marks)
c) Using insert, define a recursive function isort :: [Int] -> [Int] that
implements the insertion sort algorithm, in which the empty list is already
sorted, and any non-empty list is sorted by inserting the head of the list into
the result of recursively sorting the tail of the list. (4 marks)
d) Show how your definition evaluates isort [3,2,1]. There is no need to show
how each application of the function insert is evaluated. (3 marks)
that return the numbers in a list that are strictly smaller and larger than a
given number, e.g. smaller 3 [2,4,1,5] should return [2,1]. (2 marks)
f) Using smaller and larger, complete the missing parts ? in the following
definition for a recursive function that implements the quicksort algorithm. You
may assume that the argument list contains no duplicates. (4 marks)
4 G51PGP-E1
a) Provide the implementation for a method set() which will take two parameters,
an (int) index and an Integer value, which will set the array element at the
specified index to the specified value. Implement your method so that it will
automatically grow the array if an attempt is made to set a value beyond the
end of the array (please note the resize() method). A correct set() method
would ensure that the main() method above would output the first 20 square
numbers. (5 marks)
5 G51PGP-E1
b) Implement an interface called Operation, with one method called apply, which
will take a single parameter value (of type int) and return a value of type int.
(3 marks)
c) Create a class called OpDouble which implements the interface Operation, and
has an apply() method which returns a value which is double the value of the
parameter provided. (3 marks)
e) Provide a single line of code which will call the function applyToAll() from part
d) using a new object of your OpDouble type, to double the value of all elements
in the array. (2 marks)
f) Explain what Lambda expressions are in Java and why these are useful.
(4 marks)
6 G51PGP-E1
Consider the Java code for the InfTree class on the next page which implements
an infinite tree.
current = myTree;
for ( int i = 0 ; i < 6 ; i++ )
{
System.out.println("Right branches, level " + i + "=" + current.val());
current = current.r(); // Go to next right sub-tree
}
b) Provide the code for a sub-class of the InfTree class which will change the
behaviour so that each left tree node multiplies the value of the parent by 2
rather than adding 1. Explain clearly why you need a constructor for this class.
You may find it useful to consider the factory design pattern. (8 marks)
c) Draw a simplified class diagram showing the InfTree class and your new class,
with all relationships, attributes and operations. Provide a key which clearly
shows the notation which you used in your diagram. (8 marks)
7 G51PGP-E1
public class InfTree
{
// Assume that tree node contains the left sub-tree
protected InfTree left;
// Assume that tree node contains the right sub-tree
protected InfTree right;
// Assume that tree node uses its parent tree
protected InfTree parent;
// Each tree node has an integer value
protected int value;
G51PGP-E1 End