CSC508 - Pass Year 2022
CSC508 - Pass Year 2022
INSTRUCTIONS TO CANDIDATES
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.
4. No discussion and do not share your answers with other students. Any copying of or
plagiarized answers will be awarded 0 mark.
public ArrayList()
// method definition
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:
ii. calculate and display the average compounds for speeding offences in the array
list summonList.
(5 marks)
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)
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
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)
𝐴+2∗𝐵
𝐹= +8∗𝐸
𝐶+𝐷
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
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 (!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
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:
class TreeNode{
TreeNodeleft;
Hotel data;
TreeNoderight;
public classBSTHotel{
TreeNode root;
public BSTHotel();
public void countRoom(String t);
}
Based on the information in the following table for the occupied room:
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
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)
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)
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)
10 10
B
A C
30
5
5 F 5
6
E D
25
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)