0% found this document useful (0 votes)
20 views8 pages

Exam 2017 Questions

The document is an exam paper for the Programming Paradigms module at the University of Nottingham, covering topics in Haskell, Java, and object-oriented programming. It includes questions on types, recursion, sorting algorithms, interfaces, lambda expressions, and class diagrams. Candidates are required to answer all four questions within a time limit of two hours and thirty minutes.

Uploaded by

nnteja1
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)
20 views8 pages

Exam 2017 Questions

The document is an exam paper for the Programming Paradigms module at the University of Nottingham, covering topics in Haskell, Java, and object-oriented programming. It includes questions on types, recursion, sorting algorithms, interfaces, lambda expressions, and class diagrams. Candidates are required to answer all four questions within a time limit of two hours and thirty minutes.

Uploaded by

nnteja1
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/ 8

lOMoARcPSD|25652308

Exam 2017, questions

Programming Paradigms (University of Nottingham)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by Kyra Zayn ([email protected])
lOMoARcPSD|25652308

G51PGP-E1

The University of Nottingham


SCHOOL OF COMPUTER SCIENCE

A LEVEL 1 MODULE, SPRING SEMESTER 2016-2017

PROGRAMMING PARADIGMS

Time allowed: TWO hours THIRTY minutes

Candidates may complete the front cover of their answer book


and sign their desk card but must NOT write anything else
until the start of the examination period is announced.

Answer ALL FOUR QUESTIONS

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.

No electronic devices capable of storing and retrieving text,


including electronic dictionaries, may be used.

DO NOT turn your examination paper over until instructed to do so

ADDITIONAL MATERIAL: Haskell standard prelude

G51PGP-E1 Turn Over

Downloaded by Kyra Zayn ([email protected])


lOMoARcPSD|25652308

2 G51PGP-E1

1. Haskell/FP: Types and recursion

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 ? = ?

e4 :: [a] -> [a] -> [a]


e4 xs ys = ?

e5 :: Int -> (Int -> Int)


e5 = \x -> ?

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)

sum :: [Int] -> Int

length :: [a] -> Int

reverse :: [a] -> [a]

map :: (a -> b) -> [a] -> [b]

replicate :: Int -> a -> [a]

d) Using the definition

fac 0 = 1
fac n = n * fac (n-1)

show how the expression fac 3 is evaluated. (5 marks)

G51PGP-E1 Turn Over

Downloaded by Kyra Zayn ([email protected])


lOMoARcPSD|25652308

3 G51PGP-E1

2. Haskell/FP: Recursion and sorting

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)

insert :: Int -> [Int] -> [Int]


insert x [] = ?
insert x (y:ys) = if x <= y then
?
else
?

b) Show how your definition evaluates insert 3 [1,2,4,5]. (3 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)

e) Using list comprehensions, define functions

smaller :: Int -> [Int] -> [Int]

larger :: Int -> [Int] -> [Int]

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)

qsort :: [Int] -> [Int]


qsort [] = ?
qsort (x:xs) = qsort ? ++ ? ++ qsort ?

g) Draw a tree diagram to explain how qsort [3,2,4,1,5] evaluates. There is no


need to show how each application of smaller and larger are evaluated, and
you may assume that qsort [x] = [x] for any number x. (4 marks)

G51PGP-E1 Turn Over

Downloaded by Kyra Zayn ([email protected])


lOMoARcPSD|25652308

4 G51PGP-E1

3. Java/OO: Interfaces and Lambda Expressions

Consider the following MyVector class:


public class MyVector
{
protected Integer[] internalArray;

public MyVector( int initialSize )


{
internalArray = new Integer[initialSize];
}

public void resize( int newSize )


{
if ( newSize != internalArray.length )
{
Integer[] newArray = new Integer[newSize];
for (int i=0; i < Math.min(newSize,internalArray.length); i++)
newArray[i] = internalArray[i];
internalArray = newArray;
}
}

public Integer get( int index )


{
if ( index < internalArray.length )
return internalArray[index];
return null; // Unknown
}

public static void main(String[] args)


{
MyVector vec = new MyVector(5);

for ( int i = 0 ; i < 20 ; i++ )


vec.set(i, i*i ); // This calls set() – see part a

// vec could be used here, e.g. in parts e and g

for ( int i = 0 ; i < 20 ; i++ )


System.out.println( "Element" + i + "=" + vec.get(i) );
}
}

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)

G51PGP-E1 Turn Over

Downloaded by Kyra Zayn ([email protected])


lOMoARcPSD|25652308

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)

d) Provide the implementation of a new method in the MyVector class, called


applyToAll(), which will take a single parameter (of type Operation, from part
b) and will call the apply() method of the supplied Operation for every element
in the array, setting the new value of the element to be the value returned by
apply(). (5 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)

g) Provide an example of the use of a Lambda expression in Java by giving a


single line of code which when executed will halve the values of all items in the
MyVector vec. (You may use the answers to parts a-d of this question.)
(3 marks)

G51PGP-E1 Turn Over

Downloaded by Kyra Zayn ([email protected])


lOMoARcPSD|25652308

6 G51PGP-E1

4. Java/OO: Understanding code, sub-classing and class diagrams

Consider the Java code for the InfTree class on the next page which implements
an infinite tree.

a) What is the output if the following code is executed? (9 marks)

InfTree myTree = new InfTree(1);


System.out.println( "Head = " + myTree.val() );
System.out.println( "l() = " + myTree.l().val() );
System.out.println( "r() = " + myTree.r().val() );

InfTree current = myTree;


for ( int i = 0 ; i < 6 ; i++ )
{
System.out.println("Left branches, level " + i + "=" + current.val());
current = current.l(); // Go to next left sub-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)

G51PGP-E1 Turn Over

Downloaded by Kyra Zayn ([email protected])


lOMoARcPSD|25652308

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;

public InfTree( int value )


{
parent = this; // Root is its own parent
this.value = value;
}

public int val() // get value in tree node


{
return value;
}

public InfTree l() // get left subtree


{
// generate next node if it does not exist
if ( left == null )
genLeft();
return left;
}

public void genLeft()


{
left = new InfTree( val() +1 );
left.parent = this;
}

public InfTree r() // get right subtree


{
// generate next node if it does not exist
if ( right == null )
genRight();
return right;
}

public void genRight()


{
right = new InfTree( val() + p().val() );
right.parent = this;
}

public InfTree p() // get parent tree


{
return parent;
}
}

G51PGP-E1 End

Downloaded by Kyra Zayn ([email protected])

You might also like