You are on page 1of 7

CONFIDENTIAL 1 CS/OCT 2019/CSC508

UNIVERSITI TEKNOLOGI MARA


TEST ONE – ANSWER SCHEME

ANSWER SCHEME

INSTRUCTIONS TO CANDIDATES

1. This question paper consists of six (6) 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 5 printed pages l

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 2 CS/OCT 2019/CSC508

QUESTION 1 (10 marks)

a) What is the different between singly, doubly and circular linked list?
(6 marks)

Singly – node has data and next parts


Doubly – node has prev, data and next parts
Circular – Last node not null.

b) Give TWO (2) applications of stack.


(2 marks)
Any suitable applications
1. Function Call Management
2. Expression Evaluation
3. Browser History

c) Give TWO (2) examples of recursive applications.


1) Fibonaci (2 marks)
2) BST operations

QUESTION 2 (19 marks)


a) The following are the ADTs of Printer, Node, LinkedList and Queue.

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 3 CS/OCT 2019/CSC508

public class Printer{


String brand, type;
double price;
int ppm;
public Printer(String brand, String type, double price,
int ppm){…}
public String getType(){…} //e.g. Laser, Deskjet
public String getBrand(){…}//e.g. Brother, Canon
public double getPrice(){…}
public int getPpm(){…}
public String toString(){…}
}

public class Node{


Object data;
Node next;
}
public class LinkedList{
public LinkedList(){…}
public void insertAtBack(Object data){…}
public Object getFirst(){…}
public Object getNext(){…}
public boolean isEmpty(){…}
}
public class Queue{
public Queue(){…}
public void enqueue(Object data){…}
public Object dequeue(){…}
public boolean isEmpty(){…}
}
public class Stack{
public Stack(){…}
public void push(Object data){…}
public Object pop(){…}
public boolean isEmpty(){…}

Assume TEN (10) printer object have been inserted into a Queue named PrinterQueue.

i) Write a program segment that will get the data from PrinterQueue based on their
brand and store into TWO (2) different LinkedList named CanonList and
BrotherList
(5 marks)

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 4 CS/OCT 2019/CSC508

LinkedList CanonList =new LinkedList();


LinkedList BrotherList = new LinkedList();
while(!PrinterQueue.isEmpty())
{

Printer p=PrinterQueue.dequeue();
if (p.getBrand().equals.IgnoreCase(“Canon”))
CanonList.insertAtBack(p);
else
BrotherList.insertAtBack(p);

ii) Calculate and display the total price of the printer from CanonList.
(4 marks)
float totalprice=0.0;
Object obj=CanonList.getFirst();
while (obj != null)
{
Printer p=(Printer)obj;
totalprice += p.getPrice();
obj=CanonList.getNext();
}

System.out.println(“Total price is ” + totalprice);

iii) Store all the laser type printer from BrotherList with the price of more than
RM1000 into a stack named expensivePrinter.
(4 marks)

Stack expensivePrinter = new Stack();


Object obj= BrotherList.getFirst();

while (obj != null)


{
Printer p=(Printer)obj;
If(p.getType().equalIgnoreCase(“Laser”)
{
if( p.getPrice()>1000.00)
expensivePrinter.push(p);
obj=CanonList.getNext();
}
}

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 5 CS/OCT 2019/CSC508

iv) Write the method public void enqueue(Object data){…}. Hints: The queue
extends the LinkedList class.
(3 marks)

public void enqueue(Object data)


{
insertAtBack(data);
}

v) Write the method public Object pop(){…}. Hints: The stack extends the
LinkedList class.
(3 marks)
public Object pop()
{
return removeAtFront();
}

QUESTION 3 (6 marks)

a) Convert the following infix expression to postfix expression.


A * (B + C) * D = ABC+*D*

(3 marks)

b) Evaluate the following postfix expression.


2 8 9 + * 1 + = 35

(3 marks)

QUESTION 4 ( 15 marks)

a) The recursive definition below is the formula to find n-th power of ten:
Func(n) = 1 if n = 0

2Func(n-1) +Func(n-1) if n > 0

i) Write a recursive method for the above recursive function Func(n)


(5 marks)

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 6 CS/OCT 2019/CSC508

public static int Func(int n)…………………1.5m


{
if (n==0) return 1;……………………1m
else………………………………0.5m
return 2*Func(n-1) + Func(n-1)……………………..2

ii) Trace the function if n = 2. Show your calculations.

Func(2) = 2 * Func(1) + Func(1)


=2 * (2 * func(0) + func(0) ) + ( 2 * func(0) + func(0) )
=2 * 3 + 3
=9

(4 marks)

iii) Write a recursive method for the following:

Calc(3) = 3*2 + 2*2 + 1*2 + 0*2=11


Calc(4) = 4*2 + 3*2 + 2*2 + 1*2 + 0*2=19

(6 marks)
public static int Calc(int n)…………………….2

{
if (n==0) return 0;…………………………..1.5

else…………….0.5

return n*2 + fact(n-1);……………………..2

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 7 CS/OCT 2019/CSC508

END OF QUESTION PAPER

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL

You might also like