Vous êtes sur la page 1sur 7

CONFIDENTIAL NOV2008/SET A/COS329

Diploma in Computer Science

FINAL EXAMINATION

COURSE : DATA STRUCTURES


COURSE CODE : COS329
DATE : 18 NOVEMBER 2008
TIME : 2.00 PM – 5.00 PM
DURATION : 3 HOURS

INSTRUCTIONS TO CANDIDATES:

1. This examination paper consists of 5 questions (100 marks).

2. Answer ALL questions in the Answer Booklet provided.

3. Do not bring any material into the examination hall unless permission is given by
the invigilator.

4. Please write your answer using a ball-point pen.

IC NO. : _______________________________
ID. NO. : _______________________________
LECTURER : JUNAIDAH SAID
SECTION : 2/4

Do not open the question paper until you are told to do so


The question paper consists of 8 pages including the cover page
CONFIDENTIAL 2 NOV2008/SET A/COS329

Write all answers in the provided Answer Booklet.

QUESTION 1

a. State the differences between ArrayList and LinkedList.


(4 marks)

b. The following diagram is the logical representation of an ArrayList. Given the


original size of the number list is 10.

ArrayList number = new ArrayList(10);

6 2 4 7 9 3 8 1 5 6 Logical representation

i) What will happen if the original number list is inserted with 2 more
numbers?

ii) Given two numbers are 21 and 34. Draw the new logical representation
of list number after these two numbers as been inserted in the list.
(4 marks)

c. Given the following Java application NumberList:

import java.util.*;
import javax.swing.*;

public class NumberList{


public static void main(String args[]){
final int MAX = 6;
String numStr;
int num;
ArrayList numList = new ArrayList();
for (int j=0; j<MAX; j++){
numStr = JOptionPane.showInputDialog
("Enter an integer");
numList.add(0,numStr);}
ArrayList LIST1 = new ArrayList();
ArrayList LIST2 = new ArrayList();
while (!numList.isEmpty()){
numStr = (String) numList.remove(0);
num = Integer.parseInt(numStr);
if (num % 2 == 0)
LIST2.add(numStr);
else
LIST1.add(numStr);
}
System.out.println("List1 : " + LIST1
+ "\nList2: " + LIST2);
}
}

CONTINUE
CONFIDENTIAL 3 NOV2008/SET A/COS329

a. What is the output of the above application if the inputs for the program are
27, 82, 73, 26, 19 and 21.
(4 marks)

b. Write the Java code to:

i. Store 15 numbers ranging from –300 to 300 to an ArrayList named intList.

(4 marks)

ii. Count and display all positive and negative numbers in intList.
(5 marks)

d. Show the intermediate results of sorting the following numbers into


ascending order using the Insertion Sort algorithm.
(8 marks)

83 79 92 12 78 52 38 51

Total: ( 30 marks)

TURN OVER
CONFIDENTIAL 4 NOV2008/SET A/COS329

QUESTION 2

a. State the differences between single linked list and doubly linked list.
(4 marks)

b. Given the following Athlete and LinkedList ADTs.

public class Athlete


{
private String name // name of athlete
private double distance[] // 3 distances achieved

public Athlete(String, double[])


//normal constructor

public double getBestAttempt()


// return the best distance among 3 attempts of distances

public double getAverage()


//return average distance for each athlete

public String toString()


// return the string representation of data

// definition of other methods

public class LinkedList


{
public insert (Object elem)
// insert specified element into the list

public Object getFirst()


// return the first data in the list

public Object getNext()


// return the next data in the list

// definition of other methods


}

i. Write Java codes for a method getAverage() from Athlete class above
which will return average distance for each athlete
(4 marks)

CONTINUE
CONFIDENTIAL 5 NOV2008/SET A/COS329

ii. Write in a full Java application named TestAtheletOlympic.java


to solve the following problems:

Input 7 athletes that participate in the long jump event in Olympic Games
into the LinkedList.
(6 marks)

Find and display the winner's information of that event. One athlete will have
three attempts. The winner will be the one with the longest jump among all the
athletes.
(6 marks)

Display the average distance of all the jumps obtained by the athletes.
(5 marks)

(Total: 21 marks)

QUESTION 3

a. Explain the characteristics that must be considered when designing a recursive


method.
(4 marks)

b. Why is recursion "more expensive" than iteration (looping)?


(2 marks)

c. Draw a trace of complete execution of the following method power(3, 4).

int power(int n, int a) //LINE 1


{if (a = = 0) //LINE 2
return 1; //LINE 3
else //LINE 4
return n * power(n, a-1); //LINE 5
(5 marks)
}

d. What happens during execution of the above method for power(2, 3) if the
statement in //LINE 5 is changed to:

return n * power(n, a);

(2 marks)

(Total: 13 marks)

TURN OVER
CONFIDENTIAL 6 NOV2008/SET A/COS329

QUESTION 4

a. State TWO (2) differences between a stack and a queue by using (draw) a model
of each structure.
(6 marks)

b. Using a stack diagram/table, convert the following expression into a postfix


expression.

i) A+B*(C-D)/(E-F)
(5 marks)

ii) A*(B+C / (D-E))


(5 marks)

(Total: 21 marks)

QUESTION 5
a. Discuss TWO (2) advantages of storing data in a Binary Search Tree compared
to storing in an unsorted LinkedLlist.
(4 marks)

b. How many nodes are there in a full binary tree of height 4?


(1 mark)

c. Construct a strictly binary tree (SBT) with 11 nodes.


(3 marks)

d. Given a binary tree:

B C

D F
E

G
H
I

CONTINUE
CONFIDENTIAL 7 NOV2008/SET A/COS329

The following questions are referred to the given Binary Tree:

i. What are the descendants of B?


(2 marks)

ii. What is the height of the right sub-tree?


(1 mark)

iii. What is the output if the elements are written out using the pre-order traversal of
the above tree?
(2 marks)
e.
i. Build an expression tree for the following postfix expression:

cd*ab+-db/%

(5 marks)

ii. Evaluate the above expression if a = 3, b = 2, c = 4 and d = 5.


(5 marks)

f. Construct a binary search tree (BST) from the following set of data according the
order given:

45 27 31 70 65 84 12 55 38 47
(5 marks)

(Total: 28 marks)

END OF PAPER

TURN OVER