You are on page 1of 13
CONFIDENTIAL, i CSIJUN 2019/¢8C248 UNIVERSITI TEKNOLOGI MARA FINAL EXAMINATION FUNDAMENTALS OF DATA STRUCTURES COURSE CODE : CSC248 EXAMINATION : JUNE 2019 TIME : 3HOURS INSTRUCTIONS TO CANDIDATES 1 This question paper consists of two (2) parts: PART A (10 Questions) PART B (7 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 ji) 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 13 printed pages (© Hak Cipta Universit Teknologl MARA CONFIDENTIAL CONFIDENTIAL, 2 CSIJUN 2019/8248 PART A 1. The sort usually performs fewer exchanges than the sort ‘A. bubble, insertion B. bubble, linear Cc. insertion, bubble D. insertion, binary 2. Choose the CORRECT statement in Java that will insert value "101010" into a list named theList as its 100" element. A. thebist.add (99, 101010); B. theList.add (100, 101010); ©. thebist.insert (101, 101010); D. thebist.set (100, 101010); 3. Given the following Diagram 1 Head Diagram 1 Diagram 1 shows a linked list. A. circular B. doubly C. reversed D. singly 4. Which of the following is FALSE about queue? ‘A. Aqueue stores element in a First-In-First-Out (FIFO) order. B. In a queue, element can be inserted or deleted at/from both ends but not in the middle. C. A queue uses the implementation of linked list, in which a new element will be inserted at the back of the list. D. Ina queue, we remove the newest added element. (© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL, CONFIDENTIAL, 3 CSIJUN 2019/¢8C248 5. Choose the CORRECT description about stack data structure, A B. cl D. The element that is added first will be the first to be removed. Top of the stack always contain the recently added element. Stack is a non-linear data structure. Accessing the element without removing it from a stack is called pop operation. 6. — Which statement is FALSE about a binary tree? A. B. c! D. Anode in a binary tree has at most two children. Annon-root node has exactly one parent. A non-empty binary tree has exactly one root node. A subtree can have more than 2 children. 7. What is common in inorder, preorder and postorder tree traversals? A B. c. D. Root is visited after the left subtree. Left subtree is always visited before right subtree. The left and right subtrees can be traversed interchangeably. Root is always the last node to be visited in a tree. 8. The disadvantage of binary search algorithm over linear search algorithm is pvom> complexity efficiency simplicity speed 9. Choose a statement that is TRUE regarding sequential list. A. A sequential list is a data structure that supports searching, sorting, inserting and deleting data efficiently. A sequential lst is a circular list, which can begin at any node and follow the list in either direction until you return to the original node. A sequential list is an array that stores data in a contiguous memory location A sequential list can be perceived as a special type of list where insertions and deletions take place only at the top of the list. © Hak Cipta Universiti Teknolog! MARA CONFIDENTIAL CONFIDENTIAL, 4 CSIJUN 2019/68C248 10. (© Hak Cipta Universiti Teknologi MARA Based on the method definition given below, what is the most appropriate name to represent the name XXXX? public void XXxx (¥YYY) ( Node VW = new Node (vY¥Y); if (isEmpty ()) t first = vw; last = vw; } else ( last next last = Www; y insertatBack insertatFront removeFromBack removeFromFront pop> (Total 20 marks) CONFIDENTIAL, CONFIDENTIAL 5 CSIJUN 2019/¢8C248 PARTB QUESTION 1 Given the following Teacher and ArrayList ADTs: public class Teacher ¢ private String teacherName; private String schoolName; //i.e. SK Sri Indera, Sk Ampang private char gender; Jim or £ private int age; //age in year //normal constructor //accessors: getName(), getSchool(), getGender(), getAage() //printer: toString() d public class ArrayList ( public void add(Teacher obj) { ... } public Teacher get(int index) { ... } public int size() ( ... } d - ‘Assuming that 100 data have been inserted into a sequential list named myTeachers: a) Display the 55" record stored in myTeachers. (2 marks) b) Search and display the name and age of the oldest male teacher at SK Sri Indera. (6 marks) (© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL, CONFIDENTIAL 6 CSIJUN 2019/¢8C248 QUESTION 2 Given the following Homestay and ArrayList ADTs: public class Homestay { private String namé private int no_rooms; private double distance_fromuitm; //in kilometer (km) private double rate; //normal constructor //accessors: getName(), getNoRoom(), getDistance(), getRate() //printer: toString() ) public class Arraybist € public void add (Homestay obj) (...) public Homestay get (int index) {. public int size () (...} public void set (int index, Homestay obj) {...) y Assuming that 100 data have been inserted into a sequential list named homestayList1: a) Replace the current data stored at index 22 with a new object that has the following details: Name of the homestay : Fyna Motel No. of rooms available : 68 Distance from UITM (km): 7.98 Rate per night 120.90 (Hint: Create a new object with the given values, then replace the data in index 22 with the new object) nae marks) b) Find the homestay with distance more than 5 km from UiTM and copy its details to another sequential list named homestayList2. (4 marks) ©) Based on the answer in (b), write statements that will display the number of homestays with distance more than 5 km from UiTM. (2 marks) (© Hak Cipta Universiti Teknologi MARA, CONFIDENTIAL, CONFIDENTIAL, 7 CSIJUN 2019/¢8C248 QUESTION 3 Given the following Magazine and LinkedList ADTs: public class Magazine ( y private String title; //name of the magazine private String category; //i.e. music, fashion, humor private String publisher; //publisher name private double price; private int yearPublish; //normal constructor /Jaccessors: getTitle(), getCategory(), getPublisher(), uw getPrice(), getYear() /fprinter: toString() public class Linkedbist ( } private Node first, last, current; public void insertAtBack(Magazine obj) { ...} public Megazine getFirst() ( ... } public Magazine getNext() ( ... } Assuming that 500 magazines have been stored in a linked list named magazineLL: a) b) Count and display the number of books published by “Ju Publisher’. (3 marks) Write code segments that perform the following actions: + Search and display the titles and prices of all books in "fashion" category. * Calculate and display the total price of all books found in (i) (4 marks) Write the method definition called getLast() for class LinkedList. This method will get the last node in a linked list. (3 marks) (© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL CONFIDENTIAL 8 CSIJUN 2019/CSC248 QUESTION 4 Given the following Cinema and Linkedbist ADTs: public class Cinema ¢ private String cinCode; = //cinema code, i.e. Halll, Hall2 private String movieTitle; private double price; private String modePaymen| private boolean membershi| /eash or credit card //true: member, false: non-member //normal constructor //accesors: getCode(), getTitle(), getPrice(), getMode(), uw getMembership () //printer: toString() ) public class Linkedbist { public Node first, last, current; public void insertAtBack(Cinema obj) { ...} public Cinema getFirst() ( ... } public Cinema getNext() ( ... } a) Insert 50 Cinema objects into a linked list named Cinema. (4 marks) b) Search and display the details of a movie entitled “Enchanted. (2 marks) © Hak Cipta Universiti Teknologi MARA. CONFIDENTIAL, CONFIDENTIAL 9 CSIJUN 2019/C8C248 QUESTION 5 Given the following program: public class TestQ ( public static void main (String{] args) € Queue list1 Queue list2 Queue list3 new Queue(); new Queue (); new Queue(); for(int i=5;i<16;i++) ( if (185120) List1.enqueue(i*6) ; else List2. enqueue (i); ) Queue list4 = new Queue(); int x; while (!list1.iskmpty()) { x = (Integer) list1.dequeue() ; if (xe8==0) List3 enqueue (x); else ist4. enqueue (x) ; ) SN while (!list4.ismmpty()) { List1. enqueue (List4.dequeue()); y a) Draw the diagram of 1ist1, 1ist2 and 1ist3 after the program has been executed Please indicate the position of front and rear for all three lists. (6 marks) b) In Queue concept, briefly explain what happens to 1ist4 afer the line statement LRA? (2 marks) (© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL, CONFIDENTIAL, 10 CSIJUN 2019/¢SC248 QUESTION 6 a) Consider the following program segment. Draw the diagrams of stack1 and stack2 at the line of //daw and identify the output. import java.util.Stack; public class MyStack { public static void main (String{] args) | ( Stack stackl Stack stack2 char [] list new Stack(); new Stack(); {'e', bY, ta, te! for(int i =| ( i <6; i++) stackl.push(list{i]); y Object ob: for(int i = ¢ pi<3;it+) obj = stackl.pop(); stack2.push(obj) + ) //araw while(!stackl.iskmpty()) ce system. out .printIn(stackl.pop()) ; } b) Convert the following expressions into postfix notations: i (A*B+C-D)+E/F i, H+(J/K*U*M*N ©) Convert the following expressions into prefix notations: i. (D-E/F)*(G/H-J) i, U-V-W+#X/(¥*Z) (© Hak Cipta Universit Teknolog! MARA (5 marks) (3 marks) (3 marks) CONFIDENTIAL, CONFIDENTIAL, " CSIJUN 2019/CSC248 d) Evaluate the following equation by using stack configuration and show all the steps involved in solving the equation. 9+(24+3*2)/(4/2) (4 marks) QUESTION 7 a) Given the following arithmetic expression: (R#S)/(T#UV)*W Draw the expression tree of the given arithmetic expression. (4 marks) b) Traverse the following tree using inorder, preorder and postorder traversal. (Ge) ae, (6 marks) (© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL, CONFIDENTIAL, 12 CSIJUN 2019/¢SC248 °) Given the ADTs of Kindergarten, TreeNode and BSTKindergarten: public class Kindergarten { private String name; private char gender; private int age; private String className; private String teacher; //normal constructor /*accessors: getName(), getGender(), getAge(), getClassName, getTeacher()*/ //printer: toString() y public class TreeNode € Kindergarten data; TreeNode left, right; //definition of other methods } public class BSTKindergarten ( private TreeNode root; private BSTKindergarten () {..} public void countGender (char gender) ( ...) public void displayChildren (String classname) (..) //definition of other methods ) ‘Samples of the children data in the kindergarten are listed in the following table: No. ___Name Gender | Age | Class Name | Teacher Name 1__| Saidatul Afiyah Zakaria F 4 [4Green Teacher Aini 2 | Amirul Zaquan Alias M 5_|5Red Teacher Maziah 3 | Qaira Qistina Mahzan F 5 |5Red Teacher Maziah 4 | Mimi izzati Sulaiman F 5 | 5Green Teacher Laili 5___| Chan Chuah Yip F 5__|5Red Teacher Maziah 6 | Malik Muhammad M_ | 4 |4Red | Teacher Zaini Write the definition of countGender (char) method and its recursive method to count and display the number of children that is specified through parameter according to the gender in the kindergarten. (6 marks) Write the definition of displayChildren(String) method to display the details of children from the class name that is specified through parameter according to the children’s names in ascending order. (6 marks) (© Hak Cipta Universiti Teknolog! MARA, CONFIDENTIAL, CONFIDENTIAL, B CSIJUN 2019/68C248 li, Assume that 20 objects of kindergarten class have been inserted into a tree named kinderTree, Inmain() method, write Java statements to call the methods to display: + the number of male students in the kindergarten "the children’s detail that will be sorted according to the their names in ascending order for a specific class. (3 marks) (Total : 80 marks) END OF QUESTION PAPER (© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL

You might also like