You are on page 1of 12
CONFIDENTIAL, CSIJAN 2018/CSC248 Y SS UNIVERSITI TEKNOLOGI MARA FINAL EXAMINATION COURSE FUNDAMENTALS OF DATA STRUCTURES COURSE CODE : CSC248 EXAMINATION JANUARY 2018 TIME 3 HOURS INSTRUCTIONS TO CANDIDATES 1 This question paper consists of two (2) parts PART A (10 Questions) PART B (7 Questions) 2 ‘Answer ALL questions from two (2) parts 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 invigitator. 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 12 printed pages (© Hak Cipta Universit Teknologi MARA CONFIDENTIAL CONFIDENTIAL, 2 CSIJAN 2018/CSC248 PART A (20 MARKS) 1. The term Data Structure refers to and interrelationship between them. ‘A. programming language statements B. coding standards C. organization of data elements D. none of the above 2. Which of these methods of the ArrayList class is used to obtain the current number of elements in an ArrayList object? A size() B. capacity () C. length() D. index() 3. Which of the following is not a type of linked list? A. Hybrid Linked List B. Singly Linked List C. Doubly Linked List D. Circular Linked List 4. If the elements “A”, “B", “C” and “D" are placed in a queue and are deleted one at a time, in what order will they be removed? A. ACBD B. DCBA Cc. DCAB D. ABCD Choose the suitable data structure to check whether an expression contains balanced parentheses or not? A. Stack B. Queue Cc. Arraylist D. Tree In data structures, trees are a collection of connected nodes. ‘A. topological B. linear C. _ hierarchical D. ‘sequential (© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL CONFIDENTIAL 3 CSIJAN 2018/CSC248 7. Ina binary search tree, which type of tree traversal algorithm will process the data in sorted order? A. Pre-order B. In-order C. Post-order D. None 8. — Which method is used to access the data at the top of the stack? A peek() B. retrieve() Cc. pop() D. access() 9. When implementing programs that use data structures, a programmer should think about A. How to organize the data B. The methods that process the data structure C. The problems that will use the data structure D. Allof the above 10. Which of the following statements are TRUE about linked list? |. Every node in a linked list has two components. Il, The address of the first node is stored in a reference head. Ill. In doubly linked list, every node contains the address of the next node except for the first node. IV. Method adaFirst() adds the item in the linked list based on specific index. All B. UII Cc. UII D. LUI (© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL CONFIDENTIAL, 4 PART B (80 MARKS) QUESTION 1 Given the following application program: CSIJAN 2018/CSC248 import java.util.*; public class Showoutput { public static void main(String all) { ArrayList inputName = new ArrayList (); ArrayList insertedValue = new ArrayList (); Scanner sc = new Scanner (System. in); //#a insert name into inputName inputName.add(3,"7") ; for(int is0;icinputName.size();i++) { int num = name.length() * 3; insertedValue.add (num) ; // *eeeereeees Question 1 (a) ttteeetereesene (//e0seee0ee+s Question 1 (b) tetereeeeeaeeeees. String name = (String) inputName.get (i) ; a) — Write a program segment at line marked with #a to insert 5 names given by the user as shown in the given example and store in inputname: Enter name : Farid Enter name : Ali Enter name : Fatimah Enter name : Sulaiman Enter name : Aisyah (3 marks) b) Draw the ArrayList objects inputName and insertedvalue after the program has been executed. (© Hak Cipta Universiti Teknologi MARA ( marks) CONFIDENTIAL, CONFIDENTIAL 5 CSIJAN 2018/CSC248 QUESTION 2 Courier VS Deliveries operates delivery services for the northern region. The company asks you to write a program that uses the sequential list data structure to manage the packages to be delivered. Given the following Delivery and ArrayList ADTs: Delivery ADT: public class Delivery{ private String custName private String idNum private char destination // L - Local, X - Long Distance private double weight // in kilogram public Delivery (string, String, char, double) {...} public String getCustName () {. public String getIdNum() {..} public char getDestination() {..} public double getWeight () {..} public double calculate () {..} public void display {..} } ArrayList ADT: public class Delivery{ public ArrayList () public int size() public void add (Object elem) public Object get (int index) public Object remove(int index) Assume that all data has been inserted into an arraylist named Al1DeliveryList, write Java program segments to: a) _ Display the information of all items delivered to local destinations. (3 marks) b) Transfer the delivery information of overweight items to another arraylist named OverWeightList, where overweight items have weight is more than 50kg. (5 marks) (© Hak Cipta Universit Teknologi MARA, CONFIDENTIAL CONFIDENTIAL, 6 CSIJAN 2018/CSC248 QUESTION 3 A linked list with the following ADTs are used to store XYMovie information. public class xyMovie{ private String movieTitle; private double price; private String modePayment ; //cash or creditcard //getter: getCode() ,getTitle() ,getPrice() ,getModePayment () , //getMember () public class LinkedList { private Node first, last, current; public Linkedbist (); public void insertatBack (Object elem); public Object removeFromSecond () ; public Object getFirst (); public Object getNext (); public boolean isEmpty (); Write a Java program segment a) Create a method removeFromSecond() that removes the second element in a linked list and returns the removed object. Return null if the list is empty or there is only one data. (5 marks) b) Assume that all data has been inserted into moviesLL, use method removeFromSecond () to transfer all data paid by credit card into CreditcardLL, otherwise transfer into CashLL. (5 marks) (© Hak Cipta Universit Teknologi MARA CONFIDENTIAL, CONFIDENTIAL 7 CSIJAN 2018/680248 QUESTION 4 The following is the ADTs of Lecturer and LinkedList. public class Lecturer { private String lecturerName; private String facultyName; private String phoneNumber; / {constructors //setters /Igetters //printer } public class LinkedList { public LinkedList (); public void insertatBack (Object elem); public void insertatFront (Object elem); public Object getFirst (); public Object getNext (); } Write program segments to do the following: a) _ Insert TWENTY (20) records of lecturers into a linked list named lecturerLL. (3 marks) b) List the names and phone numbers of lecturers from Faculty of Computer & Mathematical Sciences. (3 marks) (© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL, CONFIDENTIAL 8 CSIJAN 2018/CSC248 QUESTION 5 Given the ADTs of Lorry and Queue classes: public class Lorry { String regNo; // lorry registration number String dateReg; // date registered with JPJ, format ddmmmyyyy String owner; // name of the owner String stateReg; //which state the vehicle is registered eg. Pahang, Melaka etc public Lorry(string, String, String, String); public String getRegNo(); public String getDateReg(); public String getowner(); public String getstate(); } public class Queue { public Queue () ; public void enqueue (Object) ; public Object dequeue (); public Boolean isEmpty (); // definition for other methods } Given that a queue named lorryQueue has been inserted with 30 lorries’ information. Write Java program segments to: a) find and display how many lorries were registered in the year 2016. (Note: In order to avoid data loss, use a temporary queue to store lorry information.) (4 marks) b) from the temporary queue, move all lorries that were registered in Pahang into a stack named phgStack and other lorries into the original queue. (4 marks) (© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL CONFIDENTIAL, 9 CSIJAN 2018/CSC248 QUESTION 6 Famous BookShop is a bookstore that is strongly positioned as a trilingual one-stop bookstore catering for everyone from all walks of life. It sells a wide variety of fiction, non- fiction and general interest books in English, Chinese and Malay languages, as well as school textbooks and revision books. A pile of shelved books is arranged for the same title of the same category to help customers quickly and easily find the specific book they came for. The detail of the books has been kept using stack named bookStack. The following are the ADTs of Book and Stack. public class Book { private String serialNum; //example: A2027, B2516, C2927 //(Bekid, B= adult, C= teenager) private String title; private double price; private string author; private string publisher; // definition of basic methods given here } public class stack{ public void push (Object) ; public Object pop(); public boolean isEmpty (); a) Write the program segment to extract the kid books and adult books from bookstack. Copy into another stack named kidStack for kid books and adultStack for adult books. The type of book can be recognized by its serial number where the serial number that starts with A are kid books and the serial number starting with B are adult books. The rest of the books are to remain in the original stack. (Hint: use a temporary stack) (5 marks) b) The initial diagram for bookstack is given as follows. Draw the diagrams of bookStack, kidStack and adultstack after the process in Question 6 (a). Bottom Top bookstack (3 marks) (© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL CONFIDENTIAL 10 CSIJAN 2018/CSC248 ©) Given the following expression: F°GSH#I-(W+K)/L i) Convert the expression to postfix and prefix notations. (3 marks) ii) Evaluate the above expression (a) using stack configuration if F= 5, G = 2, H= 2, 157, J=8, K=6,L =2, Showall the works. (4 marks) QUESTION 7 a) Given an infix arithmetic expression as follows: H*(1+J)$(K-L+M*N) i) Draw the expression tree. (3 marks) ii) Traverse the tree using preorder and postorder traversal. (4 marks) iii) _ Whats the depth and the type of the tree produced in Question 7 (a) i) (2 marks) (© Hak Cipta Universiti Teknologi MARA, CONFIDENTIAL, CONFIDENTIAL, 1" CSIJAN 2018/CSC248 b) Given the TreeNode, BSTEmployee and Employee ADTs: public class Employee { private String 1D; private String name; private String statu: private String position; private double salary; public Employee () {..} public Employee (string a String b,String c,String 4, double e) private String get1D () {.. private String getName (){..}; private String getstatus () private String getPosition () {. private double getSalary (){..}; } public class TreeNode { TreeNode left; Employee data; TreeNode right; //definition for other methods } public class BSTEmployee { TreeNode root; public BSTEmployee(); public double calcTotsalary (){~); public void calcAveSalary() {..} public void countMStatus(String status) {..}; //definition for all methods (© Hak Cipta Universit Teknologi MARA CONFIDENTIAL CONFIDENTIAL, 12 CSIJAN 2018/CSC248 The following table contains the information of TechNoArt Sdn. Bhd employees: Marital ‘Salary 1D Name Position Status (RM) 13124 | Nik Mohd Fathi Nik Mat Marketing Officer | Single 4494 27421 | NoorShuhada Ibrahim Operating Officer Married 6627 21564 | Arumugam Senthivel | Clerk | Married 2099 16145 | Siti Nuraidi Abu Bakar Manager Single ‘9059 26121 | Ahmad Husni Zamri Financial Officer Single ~ 6964 24737 | Ahmad Shafiq Abdul Razak | Clerk Married 1629 i) Based on the information in the table, draw a Binary Search Tree (BST) according to employee salary. (3 marks) ii) Write the definition of method countmstatus (String status) and its recursive method to count and display the number of employees with the marital status given by the parameter. (6 marks) Write the definition of method calcavesalary() and necessary recursive method{s) to calculate and display the average salary of all employees. (7 marks) END OF QUESTION PAPER (© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL

You might also like