Download CSC 227 Practice Test Solutions and more Exams Computer Science in PDF only on Docsity! C Sc 227 Practice Test 2 Fall 05 Section Leader ________ Your Name __________________________ 1. Write the output generated when this code executes using the TwoDArrayClass below (5pts) TwoDArray h = new TwoDArray(4); System.out.println(h.toString()); public class TwoDArray { private int lastRow, lastCol; private int[][] m; public TwoDArray(int size) { lastRow = size; lastCol = size; m = new int [lastRow][lastCol]; for(int row = 0; row < lastRow; row++) { for(int col = 0; col < lastCol; col++) m[row][col] = (row+1) * (col+1); } } public String toString() { String result = ""; for (int row = 0; row < lastRow; row++) { for (int col = 0; col < lastCol; col++) result += " " + m[row][col]; result += "\n"; } return result; } // Complete putSumInDiagonal 2. To the TwoDArray class, add method putSumInDiagonal that places the sum of each row on the diagonal with all other elements set to 0. It should work for any initial values set in the constructor (even if initial values are set to random integers). The sample below indicates different outputs. (10pts) before after 1 2 3 4 10 0 0 0 2 3 4 5 0 14 0 0 3 4 5 6 0 0 18 0 4 5 6 7 0 0 0 22 1 Output 3. Determine the tightest upper bound runtimes of the following loops. Express your answer in the Big-O notation we have been using in class (assume the initialization int sum = 0;). (12pts) a. ____________ for (int j = n; j >= 0; j--) sum++; b. ____________ for (int j = 1; j <= n; j = j + 2) sum++; c. ____________ for(int j = 1; j <= 2*n; j++) sum++; for(int k = 1; k <= n; k = 2 * k) sum++; d. ____________ for( int j = 1; j <= n; j++ ) for( int k = 1; k <= n; k++ ) sum++; for(int l = 1; l <= n; l++ ) sum++; e. _____________ int j = 1; sum++; f. _____________ for(int j = 1; j <= 1000; j++) sum++; 4. Write the code that would generate the linked structure shown below. Assume you have access to the Node<E> class with its instance variables data and next. (5pts) temp "A" "B" temp2 3 5. Draw a picture of the linked structure built from this code (use the picture above as a guideline) (5pts) Node<E> first = new Node<E>("one"); first.next = new Node<E>("two"); first.next.data = "three"; first.next.next = first; 6. Write COMPILES in the box if code would compile, otherwise write ERRROR (8pts) ArrayList stringList = new ArrayList(); String str = "first"; stringList.add(str); String from = stringList.get(0); ArrayList<String> stringList = new ArrayList<String>(); String str = "first"; stringList.add (str); String from = stringList.get(0); ArrayList stringList = new ArrayList(); stringList.add ("abc"); stringList.add (new Integer(1)); String from = stringList.get(0); Integer anInt = stringList.get(1); ArrayList<String> stringList = new ArrayList<String>(); stringList.add ("abc"); stringList.add (new Integer(1)); String from = stringList.get(0); Integer anInt = stringList.get(1); 7. Write the output for each code fragment (6pts) String inputField = "123point4"; double input = 0.0; try { input = Double.parseDouble(inputField); System.out.println("Good"); } catch (RuntimeException rte) { System.out.println("Bad"); } System.out.println("Ugly"); String inputField = "123.4"; double input = 0.0; try { input = Double.parseDouble(inputField); System.out.println("Good"); } catch (RuntimeException rte) { System.out.println("Bad"); } System.out.println("Ugly"); 2