Family Name: Other Names: Signature: Student Number: This PAPER is NOT to be retained by the STUDENT The University Of New South Wales COMP2911 Sample Exam Computing 3 - Design 20 May 2012 Time allowed: 2.5 hrs Total number of questions: 14 Total number of marks: 75 You must hand in this entire exam paper, your information sheet, and ALL the answer booklets. Otherwise you will get zero marks for the exam and a possible charge of academic misconduct. Ensure that you fill in all of the details on the front of the info sheet, this pink paper, and the 3 answer booklets, and then SIGN everything. Mark one booklet PART B, one PART C, and one PART D. Only answer PART D if your answer is likely to score more than half marks. Do not use red pen or pencil in your answers. You can use pencil for your working which is not marked. Unless advised otherwise you may not use any language features or library functions not covered in class. Code which does not comply with the course style guide or which is overly complex or unclear will be penalised. You will be provide with (an edited) copy of the course Textbook. There are two marks for following the examination instructions. Questions (and sub-questions) are not worth equal marks. Calculators may not be used. Answer all questions. Examiner’s Use Only: Followed Instructions? Part A Part B Part C Part D Signed OK Total Part A: Short Answer Questions Answer these questions in the spaces provided on this pink question paper. DO NOT answer these questions in an answer booklet! Your working is not marked in Part A. Write your answers clearly. Keep your answers neat and very brief. Messy or long answers will not be marked. For multiple-choice questions, circle one answer only. Question 1 (3 marks) The following alternatives list expressions for the worst-case time complexity of various algorithms. Circle the alternative with the worst complexity. [A] [B] [C] [D] [E] [F] n4 4n + 50n 105 .log(n) n3 + n2 + n + 1 5n + 2n(n + 1) + 1010 0.01n3 + 2n2 + 1000000 Question 2 (3 marks) In what way is an ADT more than just a list of available operations? Question 3 (4 marks) State one place the Abstract Factory pattern was used in the project. Give two good reasons why it was used there. Where: Reason 1: Reason 2: Question 4 (4 marks) A class has an array attribute. What are 3 advantages of making it private rather than public? Advantage 1: Advantage 2: Advantage 3: 1 Question 5 (4 marks) What is the problem with this code? (If you find more than one state the most serious one.) // check the first number on line is the correct source assert (Integer.parseInt(st.nextToken()) == source); // read in the destination assert (st.hasMoreTokens()); int destination = Integer.parseInt(st.nextToken()); Problem: Question 6 (3 marks) In Java, why must the main method be static? Question 7 (5 marks) In the space below give a brief example clearly demonstrating that the use of inheritance in java can violate abstraction. 2 Part B Answer this part in your Part B answer booklet. Start each question on a new page. Make your answers as clear and easy to understand as possible. Provide brief comments in your code where necessary. Confusing or illegible solutions will lose marks. If you do not wish your answer for a question to be marked, clearly record 1 mark for that question on the front of your Part B answer booklet. If you do this your answer for that question will not be marked. Question 8 (6 marks) (i) Describe the Decorator design pattern. (ii) Explain, with a brief example, in what circumstances you would use this pattern instead of inheritance, and when it would be more appropriate to use inheritance. Question 9 (6 marks) Implement a java method public static int binarySearch(int[] sorted, int key); to perform binary search on a sorted array of ints. You may also write helper methods if you wish. The int value returned is to be the position in the array of key. A precondition of the method is that key occurs exactly once in the array. We want you to write your own code in this question, so do not make use of any of the binarySearch() methods provided in java libraries like Collections or Arrays. Your code should comply with the 2911 style guide. Question 10 (5 marks) In the above question you did not have to consider the case when the key was not in the array. Of course a binary search algorithm normally does need to consider this case. We could do this by returning some special flag value, such as -1 (which can never be a legal position) to indicate that the search has failed to find the key, but in this question we consider a different approach. Suppose the type signature of method binarySeach() is altered to be: public static SearchResult binarySearch(int[] sorted, int key); Design the java class SearchResult which the binary search returns. SearchResult should allow the function to return information about both whether or not the search was sucessful, as well as the position of the key when the search was successful. (i) Briefly state the design decisions you made when designing this class. (ii) Give a java implementation of the class. Note that you do NOT need to implement the modified binarySearch() method in your answer. 3 Part C Answer this part in your Part C answer booklet. Start each question on a new page. Make your answers as clear and easy to understand as possible. Provide type definitions and brief comments where necessary. Confusing or illegible solutions will lose marks. If you do not wish your answer for a question to be marked, clearly record 1 mark for that question on the front of your Part C answer booklet. If you do this your answer for that question will not be marked. Question 11 (6 marks) (i) State the total number of ways you can place 5 queens on a 5 × 5 chessboard so that none are attacked by any other. (ii) List all the ways this can be done. For each solution you find draw a 5x5 grid and clearly mark the location of each queen on it using Xs. (iii) State what type of algorithm you used to find the answer. Note: don’t give the algorithm just state what type of algorithm it was (eg Divide and Conquer, Dynamic Programming, etc). Only your answer is marked in this question, not your working. Your answer must be clear and brief. Cross out or otherwise clearly indicate which is your working and which is your answer. Question 12 (8 marks) List a set of integer coin denominations for some fictional country in which the problem of finding the minimum number of coins to make a certain amount of change cannot be solved in a greedy fashion. The denominations must include 1c coins. Give an example which clearly demonstrates that the greedy method will not work. Explain what it is about the general problem which makes it unsuitable for greedy. Explain why some sets of denominations WILL permit the coin change problem to be solved in a greedy fashion, and give an example clearly supporting your explanation. Use plausible and realistic denominations in your examples. e.g. It is not realistic for a country to have only 1c coins. Question 13 (8 marks) Write a java dynamic programming algorithm to compute the minimum number of snaps needed to break a block of chocolate into singe squares (you can only snap one connected piece at a time). State the dynamic programming recurrence your algorithm uses. 4 Part D: Challenge Question Answer this part in your Part D answer booklet. Partial solutions for this question (i.e. attempts worth less than 50%) will score no marks in this part. If you do not wish your answer for the question to be marked, record 1 mark for the question on the front of your Part D answer booklet. If you do this your answer will not be marked. Your solution must be clear, elegant and easy to understand. In this part confusing or difficult to understand solutions will score no marks. Question 14 (8 marks) (i) Describe in no more than two paragraphs an efficient algorithm to produce a randomly generated hamiltonian cycle in a complete graph with n nodes. Each possible cycle has to be equally likely to be generated. Suppose we need to generate billions of such cycles for graphs of about 1000 nodes and execution time is critical so we want the algorithm to be fast. (ii) Write an implementation of your algorithm in java. Take no more than one page. 5