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

159.201 Algorithms and Data Structures - Massey - Exam - S3 2009 - 0903

159.201 Algorithms and Data Structures- Massey- Exam Massey University, NZ, 159.201, 159201

Uploaded by

Don
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)
19 views5 pages

159.201 Algorithms and Data Structures - Massey - Exam - S3 2009 - 0903

159.201 Algorithms and Data Structures- Massey- Exam Massey University, NZ, 159.201, 159201

Uploaded by

Don
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

0903/159.

201
MAN
Extramural only

MASSEY UNIVERSITY
MANAWATU CAMPUS
EXAMINATION FOR
159.201 ALGORITHMS & DATA STRUCTURES

Summer School – February 2010

Time allowed: THREE (3) hours

When asked to write “code” you must use the Java 2 Programming Language as
used during the course.
Minor syntax errors such as a missing ‘;’ will not be penalised.

This examination consists of THREE (3) Sections – A, B and C

Answer ALL questions in each of Sections A, B and C

Marks are distributed as follows:

SECTION A - 20 marks
Answer ALL FOUR (4) questions in this section.

SECTION B - 15 marks
Answer ALL FOUR (4) questions in this section.

SECTION C - 20 marks
Answer BOTH questions in this section.

This examination contributes 55% to the overall assessment for the paper.

CALCULATORS ARE NOT PERMITTED

THIS IS A CLOSED BOOK EXAMINATION

Page 1 of 5
0903/159.201
MAN
Extramural only

SECTION A

This section contains FOUR (4) questions.


Answer ALL FOUR (4) questions. There are 20 marks in total.

Question 1

Provide the missing Javadocs for the following method, which returns the maximum of
two values.

public int max(int a, int b) {


if (a > b) return a;
else return b;
}
[5 marks]

Question 2

For each of the following concepts, briefly describe them, and give one (1) example of
their use in Java software.

(a) Encapsulation
(b) Accessor
(c) Mutator
(d) Method overriding
(e) Method overloading [5 marks]

Section A continued over…

Page 2 of 5
0903/159.201
MAN
Extramural only

Section A continued…

Question 3

Consider the following Java interface, which allows objects to represent instances of
people:

public interface Person {

public String setName(String name);

public void getName();

public void setAge(long age);

public int getAge();

public StringBuffer toString();

(a) There are four (4) problems with this interface. Identify them. [2 marks]

(b) Provide a completed version of this interface that resolves these problems.
[2 marks]

Question 4

(a) What does the hashCode() method do in Java? [2 marks]

(b) What kind of contract exists between equals() and hashCode()? [2 marks]

(c) Give one (1) example of what may occur if this contract is breached. [2 marks]

Page 3 of 5
0903/159.201
MAN
Extramural only

SECTION B

This section contains FOUR (4) questions.


Answer ALL FOUR (4) questions. There are 15 marks in total.

Question 5

Draw an expression tree (binary tree) of the following algebraic expression


(a+b/c)*(d-e)/g
[3 marks]

Question 6

Complete the following implementation of a binary tree

public int getNumberOfNodes ()


{

} // end getNumberOfNodes

[3 marks]

Question 7

Draw three balanced search trees [(a) AVL; (b) 2-3; (c) 2-4] obtained by adding 60, 50,
20, 80, 90, 70, 55, 10, 40, and 35. [6 marks]

Question 8

Provide an algorithm for finding the second largest node in a binary search tree.
[3 marks]

Page 4 of 5
0903/159.201
MAN
Extramural only

SECTION C

This section contains TWO (2) questions.


Answer BOTH questions. There are 20 marks in total.

Question 9

While on a night tramp, 4 people reach a bridge. To see well enough to cross the bridge
you have to use a torch, and being poorly equipped you only have one torch between
the 4 of you. The bridge is only strong enough to allow 2 people to use it at the same
time, and when 2 people cross together they go at the speed of the slowest one.
Write an algorithm that gets all 4 people across the bridge in the shortest time.

Demonstrate it on these two examples:

(a) The crossing times for the 4 people are 1 minute, 1 minute, 3 minutes, 5 minutes.
(b) The crossing times for the 4 people are 1 minute, 4 minutes, 4 minutes, 5 minutes.

Comment on how well your algorithm generalises to the case of n people. [10 marks]

Question 10

(a) Give an example of a loop invariant and why it is useful. [3 marks]

(b) Chocolate bars come with grooves between the pieces so that you can break them
up. How many breaks do you have to make in order to separate the bar into all the
individual pieces? You should answer this question by identifying a loop invariant
that consider the number of breaks you have made and the number of pieces that
the chocolate bar is in. [3 marks]

(c) Write down a loop invariant for sorting a list of numbers and use it to construct the
sort algorithm that is produced. Name the algorithm, and demonstrate that the
loop invariant is satisfied by the algorithm. [4 marks]

++++++++

Page 5 of 5

You might also like