Sie sind auf Seite 1von 4

Final Exam Practice Will be discussed in the last lab sections answers will be distributed at that time 1) Implement

t the method upsideDownBackwards, with the prototype listed below. This method returns a deep copy of the source array, but with the rows in upside-down order and the columns are in reverse order. (So the first row becomes the last row; the second row becomes the next-to-last row, etc. For the elements of an individual row, the last becomes the first and the first becomes the last, etc.) The source array might be a ragged array and any array could be size 0 but none will be null. (Remember, ragged means that the number of columns may not be the same for every row.) You may assume that there is a Cat copy constructor available. public static Cat[][] upsideDownBackwards(Cat[][] source){ 2) Determine the output for the following program assuming the line in the middle of main()'s try block is replaced with each of the three options only one of the options shown in the comment (f("Hello"), f(""), f(null) is used in each run, then replace with a different one and then run again, etc.:
private static void f(String a) { try { int x = 7 / a.length(); System.out.println("A"); } catch (ArithmeticException e) { System.out.println("B"); } finally { System.out.println("C"); } } public static void main(String[] args) { try { /*OPTIONS(Select one of these statements for this line): */ // f("Hello"); f(""); f(null); System.out.println("D"); } catch (NullPointerException e) { System.out.println("E"); } catch (ArithmeticException e) { System.out.println("F"); } System.out.println("G"); }

3) Consider the following code fragment. Assume that the variable named value is of type int and str of type String. if (value > -2) { Define a switch statement so that will have a str = "A"; result that is equivalent in final result to the } else if (value < 0){ code fragment to the right. You may not write str = "B"; any statements before or after the switch } else{ statement. Your switch statement should not str = "C"; have any unnecessary redundancy. You may } not have any if/else statements or any if (value > 4) { ternary ?: operators in your switch str = "B"; statement. } else if (value == 3){ str = "x"; } else if (value == 6){ str = "m"; | else if (value == -6){ str = "w"; } 1

4) Assume that there is a class called Mammal which has been extended to a subclass called Dog, and that the following statements have been executed. For each write "works fine" or "will not compile" or "compiles but throws and exception" Mammal a = new Mammal(); Dog c = new Dog(); Mammal x = new Dog(); For each code fragment below, answer each with: Works Fine, Compiles but Throws an Exception, Won't compile a. Mammal p = (Mammal) c; b. Dog q = (Dog) a; c. Mammal r = c; d. Dog s = a; e. Dog t = (Dog) x; f. Dog u = x; 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(); 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); int a = 7; Integer[] b = {new Integer(1), new Integer(5)}; Integer[] c = b; Integer[] d = new Integer[b.length]; for (int i = 0; i < b.length; i++) { d[i] = b[i]; } int[] e = {8, 9};

6) Assume you have the class MyClass as defined below. Assume every type of class mentioned here is mutable. Write all methods mentioned. You may assume Obj1 and Obj2 classes have good copy constructors, good toString methods, good equals methods, and each has a getValue method that returns a positive int, and each has a increaseValue method that increases the value of the item by the int amount passed as the parameter. public class MyClass{ private Obj1[][] list1; private ArrayList<Obj2> list2; a) Write a copy constructor for the MyClass. 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). 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. 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. 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. f) Write a sum method that returns the sum of all getValues (in both data members) 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". 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. i) Write a method that uses a for loop to increaseValue of all of the objects in list1. Rewrite with for/each. j) Write a method that uses a for loop to increaseValue of all of the objects in list2. Rewrite with for/each. 2

7) Answer each of the following: a) How many distinct sequences of 0s and 1s could be represented using all of a series of 6 bits? b) What do the following acronyms stand for: CPU, RAM, API, GUI, IDE, JVM c) Explain the purpose of each of the following: junit testing, the debugger, eclipse, JVM, memory: stack&heap d) Explain the purpose of each of the following key words: public, private, final, finally, void, static, this, super e) What do you call the situation where two different variables refer to the same object? f) What do you call the type of variable that can at different moments in time reference different types of objects? g) Which of the following are valid Java identifiers that could be used to name methods or variables? (Circle all the valid ones.) 24Temp _9_2 break While dog@99 h) List the following operators in order of precedence. (Put the operator with highest precedence at the top; lowest at the bottom.) > (greater than) List the operators here: _____ / (division) _____ -- (decrement) _____ - (subtraction) _____ = (assignment) _____ && (logical AND) _____ i) In Java, a class can directly extend more than one class simultaneously. TRUE / FALSE j) In Java, a class can directly implement more than one interface simultaneously. TRUE / FALSE k) Write a statement that declares a variable that could be used to refer to an ArrayList that will contain String references. l) Write statements to put all of the names of your immediate family into that arrayList. You can write a
separate statement for each of the members in your family.

m) What class is at the top of every inheritance diagram in Java? (The one class that all other classes extend either directly or indirectly.) n) You can access a static variable in a class even if no objects of that class have been instantiated. TRUE/FALSE o) You can access an instance variable in a class even if no objects of that class have been instantiated. T/F p) In which kind of methods can you use the Java keyword this? (and where can't you use it?) q) Answer the following about selecting methods a. In some languages, when you override a method the compiler decides whether the base-class version or the derived-class version will run. What kind of binding is this? b. In other languages, the decision of which version of the overridden method to call is made at runtime, depending on the class of the object on the heap. What kind of binding is this? c. What kind of binding does Java use? r) If you need to continuously re-size a string, what class should you use instead of the String class?

8) Implement the method below. Assume you are in the Cat class (and the Cat has a String as its name, a double as the weight. a. Write a compareTo method which returns a negative value if the current object is less in weight than the parameter, a positive value if the current object is heavier than the parameter, and zero if they are close enough in weight to be equal. You may assume there is a EPSILON constant already defined that tells you what is "close enough" to the same to be called equal they should be considered equal if they are within that EPSILON of each other in weight. b. Write a toString method for the Cat class which gives the name followed by the weight (with the weight in parentheses). Make sure the weight is printed with no more than two places after the decimal point. Make sure to also round the 3rd digit after the decimal.
3

9) Implement the method below. The method will print out all the numbers from 1 to 100 with one value on each line, but: for numbers that are odd it will print odd instead of the number; for numbers that are greater than 50 it will print big instead of the number; and for numbers that are both odd and greater than 50 it will print oddbig instead of the number. public static void specialPrint() {

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(); }

Implement the complete class called Apple that extends the class Fruit and implements the hasSeeds interface. It has a single private instance variable of type int, called numSeeds, which represents the number of seeds in the Apple. Your class should contain the following public methods. a. Apple Constructor with one int parameter: This constructor takes one int argument, which represents the number of seeds in the Apple. It will initialize the field numSeeds to be equal to the parameter, and the field fruitType will be set equal to the String Apple. b. Apple Constructor with no parameters: This constructor will initialize an Apple with 13 seeds and fruitType equal to Apple. You must call the constructor from part a in your implementation of this constructor. c. Apple Copy Constructor. You must call the copy constructor of the Fruit class while implementing this one. d. Apple equals: You must follow the example of a well-designed equals method given in the lecture. As we are defining it, to be equal means that both objects must be of the same class (recall the method getClass) and both objects must have the same number of seeds. e. Apple toString: Write a toString method for this class which returns a String containing both the name and the number of seeds in some reasonable format. f. Possibly other method(s): Implement any method(s) that are required because of the fact that this class extends Fruit and/or implements hasSeeds. Note: You will lose points if you implement methods that are not necessary. If you do implement some method(s) here, use simple and reasonable implementation(s).

Das könnte Ihnen auch gefallen