VERSION 00000001 COMPSCI 105 THE UNIVERSITY OF AUCKLAND FIRST SEMESTER, 2010 Campus: City Computer Science TEST Principles of Computer Science (Time Allowed: 50 MINUTES) Note: The use of calculators is NOT permitted. Compare the exam version number on the Teleform sheet supplied with the version number above. If they do not match, ask the supervisor for a new sheet. Enter your name and student ID on the Teleform sheet. Your name should be entered left aligned. If you name is longer than the number of boxes provided, truncate it. Answer Section all Multiple-choice questions on the Teleform answer sheet provided. Answer Section B in the space provided in this booklet. Attempt all questions. Use a dark pencil to mark your answers in the multiple choice answer boxes on the Teleform sheet. Check that the question number on the sheet corresponds to the question number in this question/answer book. If you spoil your sheet, ask the supervisor for a replacement. Write your answers in the space provided in the short answer section. Write as clearly as possible. The space provided will generally be sufficient but is not necessarily an indication of the expected length. Extra space is provided at the end of this exam book. An appendix with a simplified API is included on the last page. You may detach this appendix. Surname: First Name(s): Student ID: Login Name(UPI): MARKERS ONLY Section Marks Out Of A 25 B 25 Total 50
VERSION 00000001-2 - COMPSCI 105 Given the nested if-else structure below, answer the following TWO questions. (Question 1 and Question 2) if (a > 0) if (b < 0) x = x + 5; else if (a > 5) x = x + 4; else x = x + 3; else x = x + 2; Question 1 [1 mark] If x is currently 0, a = 5 and b = 5, what will x become after the above statement is executed? (a) 4 (b) 2 (c) 3 (d) 5 (e) 0 Question 2 [1 mark] If x is currently 0, a = 0 and b = -5, what will x become after the above statement is executed? (a) 5 (b) 3 (c) 2 (d) 4 (e) 0 Question 3 [2 marks] If x is an int where x = 1, what will x be after the following loop terminates? while (x < 50) x *= 2; (a) 2 (b) 64 (c) 100 (d) 50 (e) This is an infinite loop. Question 4 [2 marks] Given that s is a String, what does the following loop do? for (int j = s.length( ); j > 0; j--) System.out.print(s.charAt(j-1)); (a) It prints s out backwards after skipping the last character (b) It prints s out forwards (c) It yields a run-time error because there is no character at s.charat(j-1) for j = 0 (d) It prints s out backwards but does not print the 0th character (e) It prints s out backwards
VERSION 00000001-3 - COMPSCI 105 Question 5 [1 mark] Given the following: double[][] things ={ {1.2, 9.0,{9.2, 0.5, 0.0,{7.3, 7.9, 1.2, 3.9 ; What is the value of things[2].length? (a) 0 (b) 3 (c) 9 (d) 4 (e) 2 Question 6 [2 marks] What is the output of the following code fragment? ArrayList<Integer> list = new ArrayList <Integer> (); for (int i=0; i<4; i++) list.add(i,i); System.out.println(list); (a) [3, 2, 1, 0] (b) Run-time Error (c) [1, 2, 3, 4] (d) [0, 1, 2, 3, 4] (e) [0, 1, 2, 3] Question 7 [2 marks] Which of the following loops would adequately add 1 to each element stored in the array values? (a) for (int j=1; j<values.length; j++) values[j]++; (b) for (int j=1; j<values.length-1; j++) values[j]++; (c) for (int j=0; j<=values.length; j++) values[j]++; (d) for (int j=0; j<values.length-1; j++) values[j]++; (e) for (int j=0; j<values.length; j++) values[j]++; Question 8 [1 mark] To declare a three-dimensional int array called threed, which of the following would you use? (a) int[ ][ ][ ] threed; (b) int [ [ [ ] ] ] threed; (c) int[ ] threed[3]; (d) int[,, ] threed; (e) int[3] threed; Question 9 [2 marks] What will be the value for x[1] after the following code is executed? int[ ] x = {22, 33, 44; arrayprocess(x);... public void arrayprocess(int[ ] a) { for(int k = 0; k < a.length; k++) a[k] = a[k] + 5; (a) 27 (b) 38 (c) 44 (d) 22 (e) 33
VERSION 00000001-4 - COMPSCI 105 Use the code below to answer the following TWO questions. (Question 10 and Question 11) Note that the catch statements in the code are not implemented, but you will not need those details. Assume filename is a String, x is an int, a is a double array and i is an int. try { BufferedReader infile; infile = new BufferedReader(new FileReader(filename)); // i1 int x = Integer.parseInt(infile.readLine( )); // i2 a[++i] = (double) (1 / x); // i3 catch (FileNotFoundException ex) {... // e1 catch (NumberFormatException ex) {... // e2 catch (ArithmeticException ex) {... // e3 catch (ArrayIndexOutOfBoundsException ex) {... // e4 catch (IOException ex) {... // e5 Question 10 [2 marks] An exception raised by the instruction in i1 would be caught by the exception(s). (a) either FileNotFoundException or ArrayIndexOutOfBoundsException (b) either FileNotFoundException or IOException (c) ArithmeticException only (d) NumberFormatException only (e) ArrayIndexOutOfBoundsException only Question 11 [2 marks] An exception raised by the instruction in i2 would be caught by the exception(s). (a) FileNotFoundException only (b) either NumberFormatException or IOException (c) ArrayIndexOutOfBoundsException only (d) either FileNotFoundException or IOException (e) IOException only Question 12 [1 mark] If int[ ] x = new int[15]; and the statement x[-1] = 0; is executed, then which of the following Exceptions is thrown? (a) ArithmeticException (b) NullPointException (c) ArrayIndexOutOfBoundsException (d) NegativeArraySizeException (e) NumberFormatException
VERSION 00000001-5 - COMPSCI 105 Question 13 [2 marks] What is the big-o complexity of the following code fragment in terms of n, the size of the array? public static void swap(int[] a, int index1, int index2) { int temp = a[index1]; a[index1] = a[index2]; a[index2] = temp; (a) O(3) (b) O(n) (c) O(1) (d) O(n 2 ) (e) O(log n) Question 14 [2 marks] What is the big-o complexity of the following code fragment in terms of n? x = 2 * n; y = n - 4; total = 0; while (x > 0) { if ((x % 2) == 0) total = total + y; else total = total - x; x = x - 1; (a) O(1) (b) O(n 3 ) (c) O(n 2 ) (d) O(n) (e) O(log n) Question 15 [2 marks] What is the big-o complexity of the following code fragment in terms of n? int total = 0; for (int i=0; i<100; i++) total += n; (a) O(n 3 ) (b) O(n 2 ) (c) O(log n) (d) O(1) (e) O(n)
VERSION 00000001-6 - COMPSCI 105 SECTION B Answer all questions in this section in the space provided. If you run out of space then please use the Overflow Sheet and indicate in the allotted space that you have used the Overflow Sheet. ( An appendix with a simplified API is included on the last page. You may detach this appendix.) Question 16 [8 marks] (a) An int two-dimensional array stores the following values. 1 2 3 4 5 6 7 8 9 10 11 12 Write a code fragment to create and initialize the above two-dimensional array. (2 marks) (b) Complete the following method that takes an array of Strings as a parameter, and returns a single String that is the concatenation of all Strings in the array using a for-each loop. public String arrayconcatenatorbyforeachloop(string[] str){ String allelements = ""; return allelements; (3 marks) (c) Complete the following method that takes an ArrayList as a parameter, and returns a single String that is the concatenation of all Strings in the ArrayList using a simple for-loop. public String arraylistconcatenatorbyforloop (ArrayList<String> a){ String allelements = ""; return allelements; (3 marks)
VERSION 00000001-7 - COMPSCI 105 Question 17 [8 marks] (a) Complete the following method to use a Scanner object that opens a file named autos.txt and displays each line to the screen. public void readfromscanner() throws IOException { Scanner s = null; try { catch (Exception e) { System.out.println(e); finally { if (s!= null) s.close(); (4 marks) (b) Complete the following method to use a BufferedReader object that opens a file named autos.txt and displays each line to the screen. public void readfrombufferedreader() throws IOException { BufferedReader b = null; String line; try { catch (Exception e) { System.out.println(e); finally { if (b!= null) b.close(); (4 marks)
VERSION 00000001-8 - COMPSCI 105 Question 18 [3 marks] Show how the following array would change as a SELECTION SORT algorithm is used to sort the array into ascending numerical order. {25, 9, 12, 38, 11, 6 Original Array 25 9 12 38 11 6 1 st step 25 9 12 6 11 38 2 nd step... 6 9 11 12 25 38 (3 marks)
VERSION 00000001-9 - COMPSCI 105 Question 19 Given the following method factorial(): public static double factorial (double n) { if (n == 0) { return 1; else { return n * factorial(n-1); [6 marks] What is the value returned when the integer 3 is the argument to the factorial method? Perform a box trace of the call to the factorial method. Clearly indicate each subsequent recursive call. factorial(3) = (6 marks)
VERSION 00000001-10 - COMPSCI 105 Overflow pages please number answers carefully
VERSION 00000001-11 - COMPSCI 105 Overflow pages please number answers carefully
VERSION 00000001-12 - COMPSCI 105 Overflow pages please number answers carefully APPENDIX FOLLOWS
APPENDIX - 13 - COMPSCI 105 java.lang class Integer boolean equals(object obj) class String boolean equals(object anobject) char charat(int index) APPENDIX java.io class BufferedReader BufferedReader(Reader in) void close() int read() String readline() class PrintStream void print(string s) PrintStream printf(string format, Object... args) void println(string x) class FileReader FileReader(String filename) class File File(String pathname) java.util class Scanner Scanner(InputStream source) Scanner(File source) void close() boolean hasnext() boolean hasnextint() boolean hasnextline() String next() int nextint() String nextline() class ArrayList ArrayList() boolean add(e o) void add(int index, E element) boolean contains(object elem) E get(int index) boolean isempty() E remove(int index) E set(int index, E element) int size() void clear() for (initialization; termination; increment) { statement(s) for (type var : arr) { statement(s) while (expression) { statement(s) do { statement(s) while (expression);