Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

CONFIDENTIAL CS/AUG 2022/CSC248

UNIVERSITI TEKNOLOGI MARA


FINAL EXAMINATION

COURSE : FUNDAMENTALS OF DATA STRUCTURES


COURSE CODE : CSC248
EXAMINATION : AUGUST 2022
TIME : 3 HOURS

INSTRUCTIONS TO CANDIDATES

1. This question paper consists of three (3) questions.

2. Answer ALL questions in the Answer Booklet. Start each answer on a new page.

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

4. Please check to make sure that this examination pack consists of :

i. the Question Paper


ii. an Answer Booklet – provided by the Faculty

5. Answer ALL questions in English.

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


This examination paper consists of 9 printed pages

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 2 CS/AUG 2022/CSC248

Answer ALL questions.

QUESTION 1

class Queue {
public Queue () {...}
public enqueue (Object obj) {...}
public Object dequeue () {...}
public boolean isEmpty(){...}
public String toString () {...}
}

public class testQ {


public static void main (String[] args) {
Queue q1 = new Queue();
Queue q2 = new Queue();
Queue q3 = new Queue();

for (int n=0; n<5; n++)


q1.enqueue(n);

//Line 1

int xy = 0;
while(!q1.isEmpty()){
Object obj=q1.dequeue();
xy = xy + (int) obj;
if (xy % 2 == 0)
q2.enqueue(xy);
q3.enqueue(xy);
}

//Line 2
}
}

a) Draw diagrams of q1, q2 and q3 to represent the content in these data structures at Line
1 and Line 2. Please label the front and the rear of the data structures.
(5 marks)

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 3 CS/AUG 2022/CSC248

b) Given the following Queue and ZakatUiTMRecepient2021 ADTs:


class Queue {
public Queue () {...}
public enqueue (Object obj) {...}
public Object dequeue () {...}
public boolean isEmpty(){...}
public String toString () {...}
}

public class ZakatUiTMRecepient2021 {


private String studentName;
private String studentID;
private char category; //1-Asnaf, 2-Poor
private String ICNumber; //e.g 880411065521
private double amount; //amount received by the
//recepient

//normal constructor
//accessors: getName(), getID(), getCategory(),
//getIcNo(), getAmount();
//printer: toString();
}

Assume that many ZakatUiTMRecepient2021 objects have been created and added to
the ZakatUiTM queue.

i) Display the total amount of UiTM zakat distribution in the year 2021. Store the original
data in a temporary queue.
(5 marks)

ii) Display the number of Zakat UiTM recipients. Store the original data in the temporary
queue.
(5 marks)

c) Display the number of Zakat UiTM recipients according to the category. For each
category, display the highest and lowest amount received by the recipient.
(10 marks)

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 4 CS/AUG 2022/CSC248

QUESTION 2

a
)a) Given the following:

i) Convert the expressions to prefix and postfix expression:

1) ( A + B ) $ ( C + D ) – E

2) A – ( B + C ) * D + E / F

(5 marks)
ii) Given an infix expression as:

Z=(M*E–L)+(A–K/U)

1) Convert infix into postfix expression.

(1 mark)

2) Evaluate the postfix expression from (1) using stack if

M=4, E=3, L=2, A=7, K=10, U=5

(5 marks)

iii) Given a newStack diagram as follow. Write a program fragment to display the
output S A T U from the stack.

S
A
T
U
N

newStack

(4 marks)

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 5 CS/AUG 2022/CSC248

b) Given the following Vaccine and Stack ADT.

public class Vaccine {


private String vacNo; //vaccine number e.g. 001, etc
private double vacPrice; //vaccine price
private double vacYear; //vaccine produced in 2021, etc
private String vacType; //Pfizer,Sinovac,AstraZeneca,
//Novavax
//constructors
//setters
//getters
//printer
}

public class Stack {


public void push( Object elem);
public Object pop();
public boolean isEmpty();
}

Assume that all data have been inserted into a stack called vacStack.

i. Remove vaccine Pfizer which produced in year 2021 and insert into pfStack,
otherwise, insert into tempStack. Then, return back from tempStack into
vacStack.

(5 marks)

ii. Find and display the average price for vaccine Sinovac. All data in the original stack
should remain.

(5 marks)

iii. Search and replace a new price for vaccine AstraZeneca which its vaccine number
is 007 to RM 1999.00. Then, display the information. All data in the original stack
should remain.

(5 marks)

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 6 CS/AUG 2022/CSC248

c) What is the output when the following program is executed?

public class StackApp


{
public static void main(String[] args)
{
Stack theStack = new Stack();
Stack tempStack = new Stack();

for(int i = 0; i < 8; i++)


{
theStack.push(i);
}

theStack.pop();
tempStack.push(theStack.peek());

theStack.pop();
tempStack.push(theStack.peek());

theStack.pop();
tempStack.push(theStack.peek());

System.out.println("Data in theStack : ");


while (!theStack.isEmpty())
{ Object data = theStack.pop();
System.out.println(data);
}

System.out.println("Data in tempStack : ");


while (!tempStack.isEmpty())
{ data = tempStack.pop();
System.out.println(data);
}
}
}
(10 marks)

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 7 CS/AUG 2022/CSC248

QUESTION 3

a) Given an arithmetic expression as follows:

(P - Q / R) * (S + T * P $ T)

i) Draw the expression tree of the given arithmetic expression.


(5 marks)

ii) Traverse the tree in (i) using PREORDER and POSTORDER traversal.
(5 marks)

b) Given the definition of Disease, TreeNode and BSTDisease ADTs ;

class Disease {
private String diseaseName;
private double percentDeath;
private int year;

/*** Definition of the other methods including


normal constructor, mutators, accessors, processors, and
toString() printer
***/
}

class TreeNode {
Disease data;
TreeNode left, right;
/*** Definition of the other methods ***/
}

class BSTDisease {
TreeNode a;
public BSTDisease(){…} //constructor
public void printAll() {…}
public void calTotByYear(int) {…}
public void printBySpesific(){…}
public double calByDisease(String)
/*** Definition of the other methods ***/
}

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 8 CS/AUG 2022/CSC248

The following table shows the data for five principal on causes of medically certified death
in Malaysia from 2018 until 2020.

(Source from https://www.dosm.gov.my)

Diseases Death Percentage (%) Year


Pneumonia 11.4 2020
Ischaemic heart diseases 17.0 2020
Transport accidents 2.9 2020
Cerebrovascular diseases 8.3 2020
Malignant neoplasm of trachea, bronchus & lung 2.5 2020
Pneumonia 12.2 2019
Ischaemic heart diseases 15.0 2019
Transport accidents 3.8 2019
Cerebrovascular diseases 8.0 2019
Malignant neoplasm of trachea, bronchus & lung 2.4 2019
Pneumonia 11.8 2018
Ischaemic heart diseases 15.6 2018
Transport accidents 3.7 2018
Cerebrovascular diseases 7.8 2018
Malignant neoplasm of trachea, bronchus & lung 2.4 2018

Draw a Binary Search Tree and write the definition for display methods as below:

i. Draw a Binary Search Tree based on the death percentage.

(4 marks)

ii. Method printAll() and its recursive method to display the details of disease in
descending order according to death percentage.

(5 marks)

iii. Method printBySpesific() and its recursive method to display the details of
disease with death percentage more than 10%.

(6 marks)

c) Write the definition for methods that are passed values year and disease through its
parameter as below:

i. Method calTotByYear(int)and its recursive method to calculate and display the


total percentage of death based on the year that is passed through its parameter.

(5 marks)

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 9 CS/AUG 2022/CSC248

ii. Method calByDisease(String) and its recursive method to calculate and


return the total percentage of death based on the disease that is passed through
its parameter.

(5 marks)

END OF QUESTION PAPER

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL

You might also like