Docsity

Excerpts from Computer Science Exam Preparation Material - Prof. Jandelyn Dawn Plane, Exams of Computer Science

Toggle sidebar
bg1
1
Final Exam Practice Quiz โ€“ Will be discussed Monday, May 11 in lab section โ€“ answers will be posted at that time
1) Implement method
public static Cat[][] upsideDown(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] = newCat(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 0 :
case 1:
case 2:
case 4:
str = "A";
break;
default:
str = "B";
pf3
pf4
pf5
pf8

Partial preview of the text

Download Excerpts from Computer Science Exam Preparation Material - Prof. Jandelyn Dawn Plane and more Exams Computer Science in PDF only on Docsity!

Final Exam Practice Quiz โ€“ Will be discussed Monday, May 11 in lab section โ€“ answers will be posted at that time

  1. Implement method public static Cat[][] upsideDown(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] = newCat(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

  1. Define a switch

switch (value){ case 3: str = "x"; break; case 0 : case 1: case 2: case 4: str = "A"; break; default: str = "B";

break; }

  1. 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)

  2. 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)

  3. Assume you have the class MyClass as defined below. public class MyClass{ private Obj1[][] list1; private ArrayList 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]); } }

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){ 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){ allEmpty = false; } } if (allEmpty){ return "empty"; } if (list1[0].length == 0){

return "empty"; } return (list1[0][0] + " & " + list2.get(0)); } h) Write a method that returns a Stack of all objects on the array (order does not matter) but all items present in the array must be on a Stack that is returned as the return value of the method. public Stack stackIt(){ Stack temp = new Stack(); for (int row = 0; row < other.list1.length; row++){ for (int col = 0; col < other.list1[row].length; col++){ temp.push(new Obj1(list1[row][col]); } } return temp;

i) Write a method that uses a for loop to increaseValue of all of the objects in list1. Rewrite using for-each.

public void increaseArrFirstWay(){ for (int row = 0; row < other.list1.length; row++){ for (int col = 0; col < other.list1[row].length; col++){ lsit1[row][col].increaseValue(5); } } } public void increaseArrSecondWay(){ for (Obj1[] row : list1){ for (Obj1 temp: row){ temp.increaseValue(5); } } } j) Write a method that uses a for loop to increaseValue of all of the objects in list2. Rewrite with for/each. pubic void increaseListFirstWay(){ for (int i = 0; i < list2.size(); i++){ list2.get(i).increaseValue(5); } } public void increaseListSecondWay(){ for (Obj2 temp: list2){ temp.increaseValue(5); } }

  1. Answer each of the following: a) 2 raised to the 6th b) CPU = Central Processing Unit, RAM = Random Access Memory, API =Application Programmers Interface, GUI= Graphical User Interface, IDE = Integrated Development Environment, JVM = Java Virtual Machine c) Explain the purpose of each of the following: junit testing, the debugger, eclipse, JVM, memory: stack&heap
  • look these up or come see me โ€“ too much to type here d) Explain the purpose of each of the following key words: public, private, final, finally, void, static, this, super โ€“ look these up or come see me โ€“ too much to type here

String temp = name + "(";

temp += weightInt / 100;

temp += ".";

temp += weightInt % 100;

temp += ")";

return temp;

  1. Implement the method

public static void specialPrint() { for (int i = 1; i <= 100; i++){ if (i %2 == 1 && i > 50){ System.out.println("oddbig"); } else if (i %2 == 1){ System.out.println("odd"); } else if (i > 50){ System.out.println("big"); } else{ System.out.println(i); }

} }

10) Consider the class Fruit and interface hasSeeds , below.

public class Fruit { private String fruitType; public Fruit(String fruitType) { this.fruitType = fruitType; } public Fruit(Fruit x) { fruitType = x.fruitType; } public String toString(){ return (โ€œโ€+fruitType+โ€โ€); } } public interface hasSeeds { public int getNumSeeds(); } class Apple extends Fruit implements hasSeeds { private int numSeeds; public Apple(int s) { super(โ€œAppleโ€); numSeeds = s; } public Apple() { this(13); } public Apple(Apple a) { super(a); numSeeds = a.numSeeds;

public boolean equals(Object x) { if (x == null) { return false; } if (x.getClass() != getClass()) return false; Apple a = (Apple)x; return numSeeds == a.numSeeds; } public String toString() { return super.toString() + โ€œ with โ€œ + numseeds + โ€œ seedsโ€; } public int getNumSeeds() { return numSeeds; } }