CONFIDENTIAL CS/FEB 2022/CSC508
UNIVERSITI TEKNOLOGI MARA
TAKE-HOME FINAL ASSESSMENT
COURSE : DATA STRUCTURES
COURSE CODE : CSC508
DATE : FEB 2022
TIME : 3 HOURS
INSTRUCTIONS TO CANDIDATES
1. This question paper consists of FIVE (5) Structure Questions)
2. Answer ALL questions in English. Start each answer on a new page. You are given THREE
(3) hours to answer these questions, therefore spend them wisely.
3. This is a take-home test. Therefore, to discuss/share/disseminate the questions and
answers amongst your classmates/coursemates are strictly prohibited. If you are found to
be guilty or have committed one of the actions, your marks will be deducted and you will be
penalised.
Name MUHAMAD NAJMIN BIN IBERAHIM
Matric Number 2021178761
Phone Number 0134371549
Group M3C2534B
Campus/Branch M3
Lecturer’s Name ANIS AMILAH BINTI SHARI
DO NOT TURN THIS PAGE UNTIL YOU ARE TOLD TO DO SO
This question booklet consists of 7 printed pages
1
CONFIDENTIAL CS/FEB 2022/CSC508
QUESTION 1 (20 marks)
The job of Faculty Coordinator is to administer the programmes and the students in
the faculty. The following are the ADTs of Coordinator and LinkedList.
public class Coordinator
{
String coordinatorName;
String programName;
String facultyName;
int totalStudents;
String phoneNumber;
// normal constructors
// mutators and accessors
// toString() printer
}
public class LinkedList {
public LinkedList() //default constructor
public void insertAtBack(Object elem) //insert at back
public Object getFirst();
public Object getNext();
public Object removeFromFront() //remove specified element
/*** Definition of the other methods ***/
}
Write a Java application program segment to do the following tasks:
2
CONFIDENTIAL CS/FEB 2022/CSC508
a) We have created an object of LinkedList named coordinatorLL. And, we
have inserted TWENTY (20) coordinator objects from user and insert into
coordinatorLL. Write a program segment to display a list of the coordinator
name, program and phone number from Faculty of Mechanical Engineering.
(3 marks)
System.out.println(“List of Coordinators”);
while (int num=0; num< coordinatorLL.size(); num++) {
if(coordinatorLL.get(num).getFacultyName().equalsIgnoreCase(“Faculty of
Mechanical Engineering”)){
System.out.println (“Coordinator Name: ” +
(coordinatorLL.get(num).getCoordinatorName());
System.out.println (“Coordinator Program: ” + (coordinatorLL.get(num).
getProgramName());
System.out.println (“Coordinator Phone Number: ” +
(coordinatorLL.get(num). getPhoneNumber());
System.out.println(“------------------------------------”)
}
}
3
CONFIDENTIAL CS/FEB 2022/CSC508
b) Remove all programs offered by Faculty of Civil Engineering from
coordinatorLL and move to a new Linked List called civilEngineeringLL.
(NOTE: because you only have one method to remove object from LinkedList,
you might need a temporary LinkedList to store objects from the original
LinkedList)
(7 marks)
LinkedList civilEngineeringLL= new LinkedList();
LinkedList tempList= new LinkedList();
while (int i=0; num< coordinatorLL.size(); i++){
if(coordinatorLL.get(num).getFacultyName().equalsIgnoreCase(“Faculty of
Civil Engineering”){
civilEngineeringLL.addFirst(coordinatorLL.get(i));
}
else{
tempList.addFirst(coordinatorLL.get(i));
}
}
coordinatorLL.clear();
while (int x=0; num< tempList.size(); x++){
coordinatorLL.addFirst(tempList.get(i));
}
Given ADTs of Product and Queue classes:
public class Product{
private String type; //shortSleeve, shortPants
private String color; //Pink, Blue
private char size; //S, M, L
private double price;
public Product();
public void setData(String, String, char, double)
public String getType();
public String getColor();
public char getSize();
public double getPrice();
}
public class Queue{
4
CONFIDENTIAL CS/FEB 2022/CSC508
public Queue();
public void enqueue(Object);
public Object dequeue();
public boolean isEmpty();
//definition of other methods
}
c) Given a Queue object named prodQueue has been created and inserted with
customer details. Write java program segments to perform the tasks below.
Copy all products from the prodQueue and store them into two (2) Queue named
shortSleeveQ and shortPantsQ according to the product type. The original
elements are remained in the prodQueue using a temporary Queue object.
Calculate and display the total price of all products from each Queue.
(10 marks)
Queue shortSleeveQ = new Queue();
Queue shortPantsQ = new Queue();
Queue tempQ = new Queue();
Object data;
Product p = null;
Double shortSleeveTotal = 0;
Double shortPantsTotal = 0;
Double prodTotal = 0;
while(!prodQueue.isEmpty()){
data = prodQueue.dequeue();
p = (Product) data;
if(p.getType().equalsIgnoreCase(“shortSleeve”)){
shortSleeveTotal += p.getPrice();
shortSleeveQ.enqueue(data);
}
else if(p.getType().equalsIgnoreCase(“shortPants”)){
shortPantsTotal += p.getPrice();
shortPantsQ.enqueue(data);
}
else{
prodTotal += p.getPrice();
tempQ.enqueue(data);
}
5
CONFIDENTIAL CS/FEB 2022/CSC508
while(!tempQ.isEmpty()){
data = tempQ.dequeue ();
prodQueue.enqueue(data);
}
System.out.println(“Total price of short sleeve t-shirt: RM” + shortSleeveTotal);
System.out.println(“Total price of short pants: RM” + shortPantsTotal);
System.out.println(“Total price of products that are not short sleeve t-shirt and
short pants: RM” + prodTotal);
QUESTION 2 (20 marks)
a) Given a POSTFIX expression as follows,
AB/CD/*E F G + + -
Evaluate the above expression using a STACK. If the values of the variables
are
A = 22, B = 2, C = 40, D = 5, E =3, F = 6, G = 4
Show the diagram of the stack during the evaluation process.
(8 marks)
22 2 11 40 5 8 88 3 6 4 4 10 81
22 11 40 11 88 3 6 6 91
11 88 3 91
88
A B / C D / * E F G + + -
b) Trace and write the output of the following program if the positive integer
number 825 is entered. You also have to answer what is the function of this program.
public class Mystery
{
public static void main (String [] args)
{
Stack st = new Stack();
System.out.println(“Please type a positive integer: “);
int no = input.nextInt();
while (no > 0)
{ st.push(no % 8);
no = no/8;
6
CONFIDENTIAL CS/FEB 2022/CSC508
}
System.out.print(“The mystery output is “);
while (!st.empty())
{ x = st.pop()
System.out.print(x);
}
}
}
(4 MARKS)
NO ST
825 1
103 7
12 4
1 1
Please type a positive integer: 825
The mystery output is 1471
c) Given the following method 'foo', determined the returned value of foo(22,8). You
MUST show the tracing of this recursive function.
public static int foo(int a, int b)
{
if( a < b ) {
return b;
}
int diff = a – b;
return foo( diff, b-1) + diff;
}
(8 marks)
foo(22, 8) = foo(14, 7) + 14
foo(14, 7) = foo(7, 6) + 7
foo(7, 6) = foo(1, 5) + 1
foo(1, 5) = 5
foo(7, 6) = 5 + 1 = 6
foo(14, 7) = 6 + 7 = 13
foo(22, 8) = 13 + 14 = 27
7
CONFIDENTIAL CS/FEB 2022/CSC508
QUESTION 3 (20 marks)
Given the following binary tree of alphabets:
a) Write the method definition of Binary Search Tree (BST) traversal that will produce
the output traversal of the word “M E T A M O R P H O S I S”.
(4 Marks)
b) Traverse the tree using PREORDER traversal.
(6 Marks)
c) Construct an AVL Tree by inserting this primary keys. Show step by step of the
insertion starting with an empty tree:
FY76, DD87, EU88, RD77, QA32, BL56, AA12
(10 Marks)
8
CONFIDENTIAL CS/FEB 2022/CSC508
9
CONFIDENTIAL CS/FEB 2022/CSC508
10
CONFIDENTIAL CS/FEB 2022/CSC508
QUESTION 4 (20 marks)
a) Given the array A = [38, 27, 43, 3, 9, 82, 10], apply the following SORT function
to sort this array. You are required to show the contents of the array at each
iteration.
void sort(int arr[])
{
int n = arr.length;
for (int i=1; i<n; ++i)
{
int key = arr[i];
int j = i-1;
while (j>=0 && arr[j] > key)
{
arr[j+1] = arr[j];
j = j-1;
}
arr[j+1] = key;
}
(5 marks)
b) Now, Sort the above array using Merge sort algorithm.
(5 marks)
c) Consider the following values: 66, 48, 73, 90, 122, 140, 169, 143, 177, 285. Store
the values into a hash table with 14 positions where entries indexed from 0
through 13, use the formula : key % 14 (tablesize) as the hash function and linear
probing as the method of collision resolution.
(10 marks)
11
CONFIDENTIAL CS/FEB 2022/CSC508
12
CONFIDENTIAL CS/FEB 2022/CSC508
13
CONFIDENTIAL CS/FEB 2022/CSC508
QUESTION 5 (20 marks)
a. During this pandemic area, movement from one place to another place is
restricted. Therefore, delivery services company need to find the shortest path
from one point to another point. From the following graph, find the shortest route
from the company headquarters (1) to each branch offices.
(10 marks)
b. The following graph is the route of an airplane to all cities. In order to help your
CEO to help reducing the operation cost, transform the graph into a minimum
spanning tree (Kruskal algorithm) and calculate the minimum cost for this airline
to operate their route in this pandemic era.
14
CONFIDENTIAL CS/FEB 2022/CSC508
(10 marks)
15
CONFIDENTIAL CS/FEB 2022/CSC508
16
CONFIDENTIAL CS/FEB 2022/CSC508
17
CONFIDENTIAL CS/FEB 2022/CSC508
~End of Question Script~
18