CIS 1068: Practice Final 2 [I assume you remember well the class Fraction dealt with in one of our homeworks]: 1. Multiple Choice For each question below, circle the best answer. i. What is the main difference between a static field and an instance field of a class? a. During program execution, only one memory cell is created for a static field. For an instance field, one memory cell is created for every object of the class. b. The program cannot change the value of a static field, once it has been initialized. c. Static fields can be accessed by methods outside of the class in which they are defined. d. Static fields have a larger scope than instance fields. ii. What is the purpose of the throws clause? a. To raise an exception within a method. b. To indicate that a checked exception may propagate outside a method. c. To handle a specific exception. d. To define a new exception. iii. The keyword super refers to a. The Object class b. The static fields of the current object c. The current object, seen only as an instance of the class it extends d. The default constructor of the current class 2. Evaluating Expressions and Statements For each expression write down its value. Be sure to list a constant of the appropriate type (e.g., 2 represents an int, 2.0 represents a double, and "2" represents a String). If the expression results in an error, write "ERROR". 2.5 + 5 / 3 * (7 % 5) "12".equals(9%8 + "2") (new Fraction(2,3)) == (new Fraction(2,3)) "hello".length() == "g'bye".length() 2 < 3 || 3 < 1 && 1 < 3 "rosalie".substring(3) 3. Reviewing Some Concepts: Short Answer Questions a) What is a polymorphism? b) In Java, methods can be overridden. What does that mean? (2 points) c) What is autoboxing? d) What do we do during the analysis phase of our program development? e) What is the difference,if any, between debugging and testing? 4. Assignments and casting among primitive types a) int x = 2.3; b) char y = 49; c) double z = 3; d) int v = (int)z; e) short w = v; f) boolean u = 0; 5. Assignments, Casting, and Constructors Let Moo be an interface, and let Foo be a concrete class that implements the Moo interface. For each statement below, write whether it is a legal or illegal Java statement. a) Foo x = new Foo(); legal? b) Moo y = new Foo(); legal? c) Moo z = x; legal? d) Moo u = new Moo(); legal? 6. Translating to Java simple English expressions. You can assume that x and y refer to integers, s and v to strings, and a to an integer array a. x is a multiple of y b. the length of s is even c. s occurs in v d. a has 5 elements e. s is the written representation of x 7. Classes and Objects: public class Pair { private int first; private int second; public Pair() {} public Pair(int x, int y) { first = x; second = y; } public Pair(Pair a) { first = a.first; second = a.second; } public int getFirst() { return first; } public int getSecond() { return second; } public Pair add(Pair a) {// it returns a pair with as first the sum of the first data member of the hidden parameter and of a, similarly for second as the sum of seconds } The class Pair defines an object that contains a pair of integers. For the questions below, assume that the variables p1 and p2 are of type Pair. a) Write an expression that is true if pairs referenced by p1 and p2 have the same contents. b) Provide the body of the add method 8. ArrayList Implement the method ArrayList symDif(ArrayList a, ArrayList b) which returns an array list consisting of the elements that are in a or in b but not in both. 9. File Processing Write a method public static int sumInts(String filename) that returns the sum of all integers found in the file filename. For example the file could contain roses 25 dollars per dozen violets 10 dollars per bunch and the value returned should be 35. 10. Grades For the class Grades that has two data members, a String name and an int [] scores, write the Java code for: a) A constructor that accepts as parameters a value for the name and a value for the scores (i.e., a String and an array of integers). public Grades(String newName, int[] newScores) { } b) A deep copy constructor. public Grades (Grades otherGrades) { } c) A (deep) equals method that returns true if both names and scores are equal. public boolean equals(Grades other) { } d) A method totalScore that returns the sum of the values in scores, except the smallest value [i.e if the scores were {7,8,4,9,3,6} it returns 34] 11. Arrays Implement a static method minPos that given a two dimensional integer array a prints out the row:column coordinates of the smallest value in a. You can assume that the array has no null or empty component. 12. Algorithms a) Here is an integer array {7,3,8,1,2} Show its content after each pass of the selection sort b) Given the integer array {2,3,5,8,12,15,16} list the position examined during the binary search for the value 14. c) Apply the merge algorithm to the following two arrays {1,5,3} and {2,6,4}. [I know that the two arrays are not sorted, just apply the algorithm and say what you get.]