0% found this document useful (0 votes)
114 views7 pages

CSC508 - Pass Year 2022

This document contains instructions for a data structures final assessment consisting of 5 questions. Question 1 involves removing an element from an ArrayList, calculating average compounds from a Summon ArrayList, copying summons to a LinkedList, and displaying summon details from the LinkedList. Question 2 involves defining a recursive function, tracing its output, identifying its base case, and evaluating an expression by showing the stack contents. Question 3 involves defining a postorder tree traversal, constructing an AVL tree from a list of values showing balance factors, and identifying the field to build a binary search tree from a hotel booking table.

Uploaded by

Noor Hidayah
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)
114 views7 pages

CSC508 - Pass Year 2022

This document contains instructions for a data structures final assessment consisting of 5 questions. Question 1 involves removing an element from an ArrayList, calculating average compounds from a Summon ArrayList, copying summons to a LinkedList, and displaying summon details from the LinkedList. Question 2 involves defining a recursive function, tracing its output, identifying its base case, and evaluating an expression by showing the stack contents. Question 3 involves defining a postorder tree traversal, constructing an AVL tree from a list of values showing balance factors, and identifying the field to build a binary search tree from a hotel booking table.

Uploaded by

Noor Hidayah
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/ 7

CONFIDENTIAL 1 CS/FEB 2022/CSC508

UNIVERSITI TEKNOLOGI MARA


FINAL ASSESSMENT

COURSE : DATA STRUCTURES


COURSE CODE : CSC508
EXAMINATION : FEBRUARY 2022
TIME : 3 HOURS

INSTRUCTIONS TO CANDIDATES

1. This question paper consists of five (5) questions.

2. Answer ALL questions in your answer sheet. Start each answer on a new page. Write your
name, id and group on the answer sheet.

3. Please scan and save your answers as pdf file.

4. No discussion and do not share your answers with other students. Any copying of or
plagiarized answers will be awarded 0 mark.

5. Answer ALL questions in English.

DO NOT TURN THIS PAGE UNTIL YOU ARE TOLD TO DO SO


This examination paper consists of 7 printed pages
© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL
CONFIDENTIAL 2 CS/FEB 2022/CSC508

QUESTION 1 (20 MARKS)

Given the following ADT‟s:

public class ArrayList


{
// declaration of other methods and data

public ArrayList()
// method definition

public void addFirst(Object elem)


// method definition

public Object get(int index)


// method definition

public Object set(int index, Object elem)


// method definition

public Object remove(int index)


// method definition

public int indexOf(Object elem)


// method definition

public int size()


// method definition
}

public class LinkedList


{
// declaration of other methods and data

public void insertAtFront(Object elem)


// method definition

public void insertAtBack(Object elem)


// method definition

public Object getFirst()


// method definition

public Object getLast()


// method definition

public Object removeFirst()


// method definition

public Object removeLast()


// method definition

public Object getNext()


// method definition
}

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 3 CS/FEB 2022/CSC508

public class Summon


{
private String plateNo; // Selangor – starts with „B‟, Melaka – „M”, etc.
private int summonNo;
private String date;
private double compound;
private String offence; // example: “speeding”, “running a red light”, etc.

public Summon(String,int,String,double,String){..}
public String getPlate();
public int getSummonNo();
public String getDate();
public double getCompound();
public String getOffence();
public String toString();
}

a) Assume FIFTY (50) summon data has been inserted in an ArrayList object named
summonList. Write a Java program segment to:

i. remove the summon at index 5.


(1 mark)

ii. calculate and display the average compounds for speeding offences in the array
list summonList.
(5 marks)

iii. declare a LinkedList object named speedList.


(1 mark)

iv. copy the summons with speeding offences, from summonList to speedList.
(3 marks)

v. display the details of the summons in the LinkedList named speedList, for
all vehicles registered at Selangor.
(4 marks)

b) Compare the performance of deleting an element in the the array list and deleting an
element in the linked list. Determine which one is faster. Briefly explain your reason.
(4 marks)

c) List TWO (2) disadvantages of a circular linked list.


(2 marks)

QUESTION 2 (20 MARKS)

a) Given the following recursive definition for f(n) where n is a non-negative integer.

f (n) = 0 , if n = 0

n + f (n - 1) , if n > 0

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 4 CS/FEB 2022/CSC508

i. Write the definition of the recursive function f(n) in Java.


(4 marks)

ii. Trace the output for function f(6). Show all steps taken.
(4 marks)

iii. Count the number of times the method has to be called for function f(6).
(1 mark)

iv. Identify the base case for the above recursive method.
(1 mark)

b) Evaluate expression F by showing the content of the stack

𝐴+2∗𝐵
𝐹= +8∗𝐸
𝐶+𝐷

if the following values are given :

A = 10, B= 5, C = 3, D = 7, E = 4
(5 marks)

c) The following diagram show the linked list myLL and stack myStack respectively:

myLL
26 54 68 95

head

myStack top null

If the following code fragment is executed, draw the diagram of myLL and myStack
at places marked with ***. What is the output ?

current = head;

while (current != NULL)


{
myStack.push(current.info);
current = current.link;
}
***

while (!myStack.isEmpty())
{
current.info = myStack.top();
myStack.pop();
myLL.addFront(current.info);
System.out.print(current.info + “ “);
}
***
(5 marks)
© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL
CONFIDENTIAL 5 CS/FEB 2022/CSC508

QUESTION 3 (20 MARKS)

a) There are three different traversals of a binary tree which are inorder, preorder and
postorder traversals. Write a recursive definition of method named
postordertraversal() to traverse the binary tree in post order traversal.
(2 marks)

b) Construct an AVL tree by using the following list of values and show the balance
factor after each insertion:

340 202 398 553 705 350 140


(8 marks)

c) Given the following Hotel, TreeNode and BSTHotel ADTs:

public class Hotel


{
private String roomType;
private int roomNo;
private double ratePerNight;

public Hotel(String t, int n, double r);


public String getRoomType();
public int getRoomNo();
public double getRatePerNight();
}

class TreeNode{
TreeNodeleft;
Hotel data;
TreeNoderight;

// definition for other methods


}

public classBSTHotel{
TreeNode root;

//definition of other data and methods

public BSTHotel();
public void countRoom(String t);
}

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 6 CS/FEB 2022/CSC508

Based on the information in the following table for the occupied room:

Room Type Room Number Rate per Night (RM)


Suite 1210 350
Suite 1010 350
Deluxe 1050 250
Deluxe 1250 250
Deluxe 1310 250
Superior 1020 300
Superior 1230 300
Standard 1420 150
Standard 1070 150
Standard 1280 150

i. What field to be used in order to build binary search tree? Why?


(2 marks)

ii. Based on the above table, the room number information consists the first two digits
about room level and the last two digitsis the exact room number. Write the definition
of method countRoom() and its recursive method to count the number of occupied
room for the tenth floor.
(8 marks)
Mar 2017

QUESTION 4 (20 MARKS)

a) From the given list below, illustrate the execution of each pass of the following sorting
algorithms to sort the list in ascending order.

24 12 65 87 97 34 5 77 41 110

i. Merge sort
(5 marks)
ii. Selection Sort
(5 marks)

b) Given the following items:

80 89 160 247 322 404

Fill in the contents of the hash tables after inserting the items. To insert the item k, use
the hash function, H(k) = k % 8 and resolve collisions with linear probing.
(6 marks)

c) Given the following list:

15 30 45 60 75 90 100 150

Using the linear search algorithm, how many comparisons are required to find
© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL
CONFIDENTIAL 7 CS/FEB 2022/CSC508

whether the following items are in the list? Determine whether it is best case, average
case or worst case.

i. 15
(2 marks)

ii. 200
(2 marks)

QUESTION 5 (20 MARKS)

Given the following graph:

10 10
B

A C
30
5
5 F 5
6

E D
25

a) Draw the weighted adjacency matrix from the graph.


(6 marks)

b) Traverse the graph starting from vertex A using:


i. Breadth First Search
(3 marks)

ii. Depth First Search


(3 marks)

c) Using Djikstra Algorithm, find the shortest path from node A to all other vertices for the
graph shown in a). Show all the working steps.
(8 marks)

END OF QUESTION PAPER

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL

You might also like