CIS1068 MIDTERM 2 (5) 1. What are you trying to do in Testing? (5) 2. What is a stub? How does it help us? (10) 3. (1) A data member of a class is said to be private. What does it mean? (2) A data member of a class is said to be an instance data member. What does it mean? (10) 5. If we execute double x = scan.nextDouble(); where scan is a Scanner, we may run into an exception if the user enters something like "a23.8". Use the try .. catch statement to deal with this problem. (5) 5. What is the signature of a method? Give an example (10) 6. 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) double z = 3; (d) float w = z; (e) int u = 'x'; (f) char c = u; (15) 7. Define a class with a constant, a static data member, an instance data member, a constructor, an instance method with a formal parameter and a local variable. (10) 8. What is the value of the expression below (show all the steps used in evaluating this expression): (5 > 4 + 3 * 2/3) || ( 3 % 2 != 3) && 1 < 3 (5) 9. What is printed out by the fragment int a = 1; foo(a); System.out.println("a = " + a); where foo is defined as public static void foo(int a) { a = a + 3; System.out.println("a = " + a); } Explain. (10) 10. Given the following method moo: static String moo(String s1, char c, String s2) { String who = ""; for (int k = 0; k < s1.length(); k++) { char d = s1.charAt(k); if (c == d) who += s2; else who += d; } return who; } Show what is returned by the call moo("abracadabra", 'b', "bra"); and more in general what is the purpose of this method. (5) 11. What is the output of int k = 3; while (k > 1) { int r = k % 2; switch (r) { case 0: k = k/2; break; case 1: k = 3*k+1; } System.out.print(k + " "); } (10) 12. After the statement "int[][] m = {{1,2},{3,4,5,6}}; is executed specify the value of each of the following expressions: m[1] = m.length = m[1][m[1].length-1] = m[2][2] = m[0].length + m[1].length = (10) 13. Assuming public static void foo(int[] a) { for (int k = 0; k < a.length; k++) a[k] += 2; } What will be the content of the array int[] b = {1,2,3}; after the call foo(b)? (15) 14. Write a static method isPrime that, applied to a positive integer n returns true if and only if n is a prime. (15) 15. Write a static method avg that returns the average value of a two dimensional array of integers. You may assume no array is null or empty. (15) 16. Remember the Fraction class. Add to it the method public int compareTo(Fraction a); which returns -1, 0, +1 if the subject of the method, i.e. the "this" object, is less, equal, or greater than a. (20) 17. Complete all methods below for the class IntArray public class IntArray { private int[] a; // constructor with given length for a public IntArray(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 IntArrays are same reference public boolean identical(IntArray b) { } // Equivalence test, returns true if 2 arrays have samecontent public boolean equals(IntArray b) { } } (10) 18. Suppose you have to write the linearSearch method to find an integer who in the integer array a. You will need to write a loop. Write the loop invariant of this loop without writing the loop itself. (5) 19. Given an array with content {5,3,2,6,1} show its content at the end of the first pass in the selection sort algorithm (10) 20. Write a method foo with no parameters that returns an ArrayList of Fraction containing two fractions with value respectively, 2/3 and 3/5.