TEST CSC248 Dis2021 A

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

CONFIDENTIAL CS/ DEC 2021/ CSC248

UNIVERSITI TEKNOLOGI MARA


TEST

COURSE : FUNDAMENTALS OF DATA STRUCTURES


COURSE CODE : CSC248
DATE : DECEMBER 2021
TIME : 3 HOURS

INSTRUCTIONS TO CANDIDATES

1. This question paper consists of THREE (3) 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. Discuss/share/disseminate the questions and answers amongst your


classmates/coursemates are strictly prohibited. If you are found to be guilty or committed one
of the actions, your marks will be deducted and you will be penalised.

Name
Student ID
Phone No.
Group
Lecturer’s Name
Campus/Branch

QUESTION FULL MARK STUDENT’S MARK

Part B 60

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


This Question Paper consists of 12 printed pages
© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL
CONFIDENTIAL 2 CS/ DEC 2021 / CSC248

Answer ALL questions.

QUESTION 1 (20 marks)

a) For each of the following problems, choose the data structure to solve the problem and
explain why your answer works better than other options:

i) Given runners’ names in 1st, 2nd,..., nth places, the announcer needs to announce
the awards in reversed order, which means the first place is announced last.
(2.5 marks)
Answer:

ADT/Data Structure: Stack


Explanation: Last in first out (Removal and insertion at the same point (top))

ii) Trucks drive onto a toll bridge from one end and exit from the other end of the bridge.
iii)
(2.5 marks)

Answer:

ADT/Database structure: Queue


Explanation: First in first out and removal at one point (front) and insertion at the
other point (back).

b) Briefly explain what happens when insertion is performed in ArrayList and LinkedList.
(5 marks)
Answer:

Array List works on Array and when we add an element in middle of the list, Array
List need to update the index of all subsequent elements. It also shifts the element
currently at that position (if any) and any subsequent elements to the right in order
to give a place to a new element.

Linked List works on doubly linked list algorithm. Insertions can be made without
moving any elements; only the links are altered (adjust the address of the previous
and next elements).

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 3 CS/ DEC 2021 / CSC248

c) According to the following program segment answer the following questions.

ArrayList <String> animals = new ArrayList<String>();

animals.add("CAT");
animals.add("FISH");
animals.add("DOG");
animals.add("ELEPHANT");
animals.add("MOUSE");
animals.add("HORSE");

#aaa
for(int i =0;i<animals.size();i++){
for(int j=0; j<animals.size()-1; j++){
if(animals.get(j).compareTo(animals.get(j+1))<0)//***
{
String value = animals.get(j);
animals.set(j, animals.get(j+1));
animals.set((j+1), value);
}
}
}

for(int i =0;i<animals.size();i++)
System.out.println(animals.get(i));

i) Trace the output of the program.


(3marks)
Answer:

MOUSE
HORSE
FISH
ELEPHANT
DOG
CAT

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 4 CS/ DEC 2021 / CSC248

ii) Identify and explain the algorithm used after the line statement //#####C in the
program above.
(3 marks)
Answer:

Bubble Sort Algorithm ; sorting algorithm that works by repeatedly swapping


the adjacent elements if they are in wrong order.

iii) Explain the purpose of the following statement in the program:

if(animals.get(j).compareTo(animals.get(j+1))<0)//***
(2 marks)
Answer:

Comparing two element in the list weather data in index j is smaller than
element j+1.

iv) Rewrite the code in (iii) if you want to display the elements in different order.
(2 marks)
Answer:

if(animals.get(j).compareTo(animals.get(j+1))>0)

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 5 CS/ DEC 2021 / CSC248

QUESTION 2 (20 Marks)

a) What is the output for this program segment?

ArrayList<Integer> numList = new ArrayList<Integer>();

for (int i=5; i <= 15; i++)


numList.add(i + 3);

System.out.println(numList.get(3) * 2);

for (int i=1; i <= numList.size(); i++)

if (numList.get(i) % 2 == 0)
numList.remove(i);

System.out.println(numList);

ANSWER

22 -- 2m

[8, 9, 11, 13, 15, 17] -- 3m

b) Given the following Bicycle and ArrayList ADTs:

public class Bicycle


{
private String brand; //Riverside, Polygon, Xiaomi
private String type; // mountain, road, city, folding
private double price;
private char material;// A-aluminium, B-steel, C-carbon fiber

//normal constructor
//Accessor methods:
//getBrand(),getType(),getPrice(),getMaterial()
//printer method: toString()
}
© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL
CONFIDENTIAL 6 CS/ DEC 2021 / CSC248

public class ArrayList


{
public ArrayList() {…}
public void add (Bicycle data){…}
public int size() {…}
public Bicycle get (int index) {…}
public Bicycle remove (int index) {…}
//definition of other methods
}

Assume that 50 objects of Bicycle have been inserted into an ArrayList named bicycleAL.
Write a program segment for each of the following:

i) Count and display the number of steel folding bike in bicycleAL.

int count = 0; 0.5m

0.5m
for (i=0; i< bicycleAL.size(); i++)
{ 1m
Bicycle b = (Bicycle) bicycleAL.get(i);
1m
if (b.getType().equalsIgnoreCase("folding")) 1m
if (b.getMaterial()== ‘B’ ) 0.5m
count++;
}
System.out.println("The number of steel folding bike " 0.5m
+count);

(5 marks)

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 7 CS/ DEC 2021 / CSC248

ii) Instantiate 3 ArrayList object namely SteelAL, AluminiumAL and CarbonAL.


Remove all aluminium bicycle from bicycleAL into aluminiumAL, all steel bicycle
into steelAL, and all carbon fiber bicycle into carbonAL.

(5.5 marks)
Answer
ArrayList aluminiumAL = new ArrayList(); 0.5m

ArrayList steelAL = new ArrayList(); 0.5m


ArrayList carbonAL = new ArrayList(); 0.5m

for (i=0; i< bicycleAL.size(); i++) 0.5m


{
Bicycle b1 = (Bicycle) bicycleAL.remove(i); 1m

if (b1.getMaterial() == ‘A’)
aluminiumAL.add(b1); 0.5m

else if (b1.getMaterial() == ‘B’)


steelAL.add(b1); 0.5m

else if (b1.getMaterial() == ‘C’)


carbonAL.add(b1); 0.5m

i--;
} 1m

iii) Calculate and display the total price of Polygon bike in steelAL which price is
more than RM3000.

(4.5 marks)
Answer
double total = 0.0; 0.5m

0.5m
for (i=0; i< steelAL.size(); i++)
{ 1m
Bicycle b2 = (Bicycle) steelAL.get(i);
0.5m
if (b2.getBrand().equalsIgnoreCase(“Polygon”))
0.5m
if (b2.getPrice() > 3000) 1m
total = total + b2.getPrice();
} 0.5m
System.out.println("The total price is " + total);

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 8 CS/ DEC 2021 / CSC248

QUESTION 3 (20 Marks)

a) Given the following program segment:

public static void main(String arg[])


{
int num;
LinkedList list = new LinkedList();
Scanner Scan = new Scanner (System.in);

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


{
num = scan.nextInt();
list.insertAtBack(num);
}

int value = 13;


Object data = list.getFirst();

while (data!= null)


{
System.out.println("Number is :"+value);
value = Integer.parselnt (data.toString()) ;
value = value/2;

if (value < 12)


list.insertAtFront(value);
Object data = list.getNext();
}
Assume
} the input for the above program are:

60, 11, 40, 33, 20, 55

i) Draw the diagram of list after the program segment is executed.


(2.5 marks)
Answer:

10 5 60 11 40 33 20 55

// 0.25M for each correct node


first // 0.5M for correct labelling and sequence

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 9 CS/ DEC 2021 / CSC248

ii) Write the output of the program.


(1.5 marks)
Answer:

Number is 13
Number is 30
Number is 5 //0.25M each
Number is 20
Number is 16
Number is 10

b) The following are the ADTs of House and LinkedList.

public class House


{
private String type; //ex: Terrace, SemiD, Bungalow
private String area; //ex: Kemaman, Dungun, etc
private int numUnit; //the number of unit built
private double price;

//constructor
//mutator : setType(String), setArea(String), setNumUnit(int),
setPrice(double)
//retriever : getType(), getArea(), getNumUnit(), getPrice()
//processor
//printer
}

public class LinkedList


{
public LinkedList() {}
public void insertAtBack(Object) { … }
public Object getFirst() { … }
public Object getNext() { … }
public boolean isEmpty() { … }
}

Berjaya Enterprise is offering discount to their customers. The discount rates are shown in the
table below:

House Price Discount


< RM150000 3%
RM150000 – RM300000 5%
> RM300000 10%

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 10 CS/ DEC 2021 / CSC248

i) Write the program segment to input 50 House objects and store them into a
LinkedList object named houseLL.
(3.5 marks)
Answer:

for(int j=0; j< 50; j++) //0.5M


{
String type = scan.nextLine(); //1M
String area = scan.nextLine();
int unit = Integer.parseInt(scan.nextLine();
double price =
Double.parseDouble(scan.nextLine());

House H = new House(type,area,unit,price);


//1M
houseLL.insertAtBack(H); //1M
}

ii) Get all semi-D and terrace houses from houseLL and store them into LinkedList
objects named semiDLL and terraceLL respectively. Otherwise, store them into
another LinkedList object named otherLL.
(5 marks)
Answer:

House hs = (House)houseLL.getFirst(); //0.75M


while(hs != null) //0.5M
{

if(hs.getType().equals(“semiD”)) //1M
semiDLL.insertAtBAck(hs);
else if (hs.getType().equals (“terrace”)) //1M
terraceLL.insertAtBAck(hs);
else //1M
otherLL.insertAtBAck(hs);

hs = (House)houseLL.getNext(); //0.75M

}
© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL
CONFIDENTIAL 11 CS/ DEC 2021 / CSC248

iii) Calculate and display the price after discount for each house in terraceLL.
(7.5 marks)
Answer:

House hs = (House)terraceLL.getFirst(); //0.75M


double price = 0.0; //0.25M
while(hs != null) //0.5M
{
if(hs.getPrice()<150000) //1.5M
price = hs.getPrice() * 0.97;
else if(h.getPrice()<300000) //1.5M
price = h.getPrice() * 0.95;
else //1.25M
price = h.getPrice() * 0.9;

System.out.println(hs.toString()); //1M
System.out.println(“Price after discount: RM” +
price);

hs = (House)terraceLL.getFirst(); //0.75M
}

END OF ANSWER SCHEME

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL

You might also like