CIS1068 Spring 2010 MIDTERM 2 Closed books, closed notes. The points per question are indicated next to the question. The total points for the exam are 175. (5) 1. What is a stub? How does it help us? (5) 2. Give an example of (a) an instance method (b) a static method (10) 3. True/False questions (a) The value of a local variable named x has no direct relationship with the value of a variable named x in the caller (b) Assigning a new value to a parameter changes the value of the corresponding variable in the caller's argument list (c) Local int variables are automatically initialized to 0 (d) Instance String data members are automatically initialized to 0. (e) Instance methods cannot access static data members (10) 4. Multiple choice: i. What is an algorithm? a. An unambiguous specification of a way to solve a problem. b. A Java program. c. An organization of several memory cells into a coherent structure. d. A data type that includes specifications for both procedures and data. ii. Which of the following best defines the scope of a variable? a. The type of values that may be stored in the variable. b. The part of a program where the variable can be legally accessed. c. The largest value that can be stored in the variable. d. The value currently stored in a variable. (10) 5. Here are some assignments. Indicate if they are correct and, if wrong, correct them (don't change the type of variables): (a) int x = 3.4; (b) Integer v = 12; (c) int k = v; (d) double z = k; (e) float w = z; (f) int u = 'x'; (15) 6. This program does not do anything useful. Its aim is to test your ability to trace a program. public class aaa { public static void main(String[] args) { bbb(13); } private void bbb(int x) { int y = 0; for (int k = 1; k < x; k *= 2) y = ccc(y, k); System.out.println("bbb: x = " + x + ", y = " + y); } private int ccc(int x, int y) { x = 10*x+y; System.out.println("ccc: x = " + x + ", y = " + y); return x; } } (10) 7. Define the Fraction class. Show the data members, a constructor, an accessor method and a setter method. (10) 8. What is the value of the expression below (show all the steps used in evaluating this expression): (5 > 4 + 3 / 2 * 2) || ( 3 % 2 != 3) && 1 < 3 (10) 9. Given the following method moo: static void moo(int[] a) { for (int k = 0; k < a.length; k++) { int temp = a[k]; a[k] = a[a.length-k-1]; a[a.length-k-1] = temp; } } Given int[] joe = {1,2,3,4,5,6,7} what is the value of joe after the call moo(joe). More in general what is the purpose of this method. (10)10. After the statement "int[][] m = {{1,2,3},{4,5,6,7}}; is executed specify the value of each of the following expressions: m[1] = m.length = m[0][m[0].length-2] = m[3][3] = m[0].length + m[1].length = (15) 11. Write a static method minVal that returns the smallest value found in a two dimensional array of integers. You may assume no array is null or empty. (15) 12. Implement the method public static String squeeze(String s, String v); which returns the string consisting of all the characters of s that do not occur in v. For example squeeze("rosanna", "ram") is "osnn". (5) 13. This is also a continuation of question 12. State a precondition for the method squeeze. (10) 14. Consider the following method: public static void mystery(String [] arr) { for(int i=1; i < arr.length; i++) { arr[i] = arr[i-1] + arr[i]; } } a. What is the approximate run time of the mystery method in terms of the length of the input array? b. What is the content of the array a = {"roses", "are", "red"}; after the call mistery(a)? (20) 15. Complete all methods below for the class Foo public class Foo { private int[] a; // constructor with given length for a public Foo(int length) { } // accessor returning the length of a public int length() { } // accessor, return the element at position j in a public int elementAt(int j) { } // modifier/setter, set element at position j in a to v public void setElementAt(int j, int v) { } // Equal test, returns true if 2 Foo are same reference public boolean identical(Foo b) { } // Equivalence test, returns true if 2 Foo have same content public boolean equals(Foo b) { } } (15) 16. Write a static method isPowerOf2 that, applied to a positive integer n, returns true if and only if n is a power of 2.