DOC PREVIEW
UMD CMSC 131 - Final Exam Practice Answers

This preview shows page 1-2-3 out of 8 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1 Final Exam Practice Answers 1) Implement method public static Cat[][] upsideDownBackwards(Cat[][] source){ int numRows = source.length; Cat[][] temp = new Cat[numRows][]; int currRow = numRows-1; for (int row = 0; row < numRows; row++, currRow--){ int numCols = source[row].length; temp[currRow] = new Cat[numCols]; int currCol = numCols-1; for (int col = 0; col < numCols; col++, currCol--){ temp[currRow][currCol] = new Cat(source[row][col]); } } return temp; } 2) Determine the output a) f("Hello"); A C D G b) f(""); B C D G c) f(null); C E G 3) Define a switch switch (value){ case 3: str = "x"; break; case -6: str = "w"; break; case 0 : case 1: case 2: case 4: str = "A"; break; default: str = "B"; break; }2 4) Give results: a. Mammal p = (Mammal) c; Works Fine b. Dog q = (Dog) a; Compiles, but Throws an Exception c. Mammal r = c; Works Fine d. Dog s = a; Will NOT compile e. Dog t = (Dog) x; Works Fine f. Dog u = x; Will NOT compile g. For this part, assume that the Dog class has an instance method called “bark”, which is not implemented in the Mammal class. x.bark(); Will NOT compile h. Suppose you add an instance method to the Mammal class called grow() and that you override that method in the Dog class. Consider the code below. Which grow method will be called? x.grow(); CIRCLE ONE: Mammal / (Dog) 5) Indicate what is on the stack and what is on the heap after each of the following statements (draw a picture): Integer x = new Integer(5); // x is on the stack pointing at an integer object that contains a 5 int a = 7; // a is put on the stack with the value 7 on the stack Integer[] b = {new Integer(1), new Integer(5)}; // b is put on the stack with a reference to a 2 element array on the heap // the first element of that array is a reference to another space on the heap //that is an Integer that contains a 1 // the second element of that array is a reference to another space on the heap // that is an Integer that contains a 5 Integer[] c = b; // c is put on the stack – it is a reference pointing to the same space on the heap // as where b is pointing to Integer[] d = new Integer[b.length]; // d is put on the stack pointing to a 2 element array (distinct from b's) // the elements of the array are null for (int i = 0; i < b.length; i++) { d[i] = b[i]; } // the elements of the array pointed to by d now point to the elements of the // array pointed to by b // they are distinct arrays but share the values int[] e = {8, 9}; // e comes on to the stack pointing to a two element array on the heap // the 8 and 9 are directly in that array (not pointed to by it) 6) Assume you have the class MyClass as defined below. public class MyClass{ private Obj1[][] list1; private ArrayList<Obj2> list2; a) Write a copy constructor for the MyClass. public MyClass(MyClass other){ list1 = new Obj1[other.list1.length][]; for (int row = 0; row < other.list1.length; row++){ list1[row] = new Obj1[other.list1[row].length]; for (int col = 0; col < other.list1[row].length; col++){ list1[row][col] = new Obj1(other.list1[row][col]); } } list2 = new ArrayList<Obj2>(); for (Obj2 temp: other.list2){ list2.add(new Obj2(temp)); } }3 b) Write a equals for the MyClass (two MyClass objects are considered equal if the contents of the array is exactly the same and in the order and the objects of the arrayLists are exactly the same and in the same order). public boolean equals(Object other){ if (other == null ){ return false; } else if (other.getClass() != getClass()){ return false; } else{ MyClass local = (MyClass) other; if (list1.length != other.list1.length || list2.size() != other.list2.size()){ return false; } else { for (int row = 0; row < list1.length; row++){ if (list1[row].length != other.list1.row.length){ return false; } for (int col = 0; col < list1[row].length; col++){ if (!(list1[row][col].equals(other.list1[row][col]))){ return false; } } } for (int cur = 0; cur < list2.size(); cur++){ if (!(list2.get(cur).equals(other.list2.get(cur))){ return false; } } } return true; } c) Write a method which returns the largest int (based on the individual getValues in the array (list1) (call it largestInArray). Return a -1 if there is nothing in the array. public int largestInArray(){ if (list1.length == 0){ return -1; } int largestSoFar = -1; for (int row =0; row < list1.length; row++){ for (int col = 0; col < list1[row].length; col++){ if (list1[row][col].getValue() > largestSoFar){ largestSoFar = list1[row][col].getValue(); } } } return largestSoFar; } d) Write a method which returns the largest int (based on the individual getValues) in the ArrayList (list2) (call it largestInList). Return a -1 if there is nothing in the ArrayList. public int largestInList(){ if (list2.size() == 0){4 return -1; } int largestSoFar = -1; for (Obj2 temp: list2){ if (temp.getValue() > largestSoFar){ largestSoFar = temp.getValue(); } } return largestSoFar; } e) Write a method that uses the two previous methods to return a single integer which is the largest of the whole class. Returns a -1 if and only if they are both empty. public int largestWhole(){ return (largestInArray() > largestInList() ? largestInArray():largestInList()); } f) Write a sum method that returns the sum of all getValues (in both data members) public int sumAll(){ int sum = 0; for (int row = 0; row < list1.length; row++){ for (int col = 0; col < list1[row].length; col++){ sum += list1[row][col].getValue(); } } for (Obj2 temp: list2){ sum+= temp.getValue(); } return sum; } g) Write a toString method that returns the first value (the [0][0] element) of the array followed by the first item in the ArrayList. If either is empty (there are no objects inside) return the string "empty". public String toString(){ if (list2.size() == 0){ return "empty"; } if (list1.length == 0){ return "empty"; } boolean allEmpty = true; for (Obj1[] row:list1){ if (row.length != 0){


View Full Document

UMD CMSC 131 - Final Exam Practice Answers

Documents in this Course
Set #3

Set #3

7 pages

Exam #1

Exam #1

6 pages

Exam #1

Exam #1

6 pages

Notes

Notes

124 pages

Notes

Notes

124 pages

Load more
Download Final Exam Practice Answers
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Final Exam Practice Answers and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Final Exam Practice Answers 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?