Java Homework #4 Model Solution 1) What is a checked exception, and what is an unchecked exception? Code that throws a check exception must be in a try-block that catches that exception, or the method must list that exception in a "throws" clause. Unchecked exceptions do not requires such handling. Technically, a checked exception extends class Exception, while an unchecked exception extends class RuntimeException instead. 2) What is NullPointerException? A runtime error that occurs when you invoke a method on a reference variable with a null value. 3) Which of the following statements will throw an exception? If no exception is thrown, what is the output? 1. System.out.println( 1 / 0 ); 2. System.out.println( 1.0 / 0 ); Only the first one throws: Exception in thread "main" java.lang.ArithmeticException: / by zero The second one prints "Infinity"! 4) Point out the problem in the following code. Does the code throw any exceptions? 1. long value = Long.MAX_VALUE + 1; 2. System.out.println( value ); The expression will "overflow" (the value becomes "-9223372036854775808"). No exception is generated. 5) What are the differences between constructors and methods? Constructor are a special kind of method. The differences are that constructors do not have a return type, constructors have the same name as the class, and they are only invoked using the new operator, when object is created. 6) What is wrong with each of the following programs? (a) 1 public class ShowErrors { 2 public static void main ( String[] args ) { 3 ShowErrors t = new ShowErrors( 5 ); 4 } 5 } There is no "int" constructor. (b) 1 public class ShowErrors { 2 public static void main ( String[] args ) { 3 ShowErrors t = new ShowErrors(); 4 t.x(); 5 } 6 } There is no method "x" defined. (c) 1 public class ShowErrors { 2 public void method1 () { 3 Circle c; 4 System.out.println( "What is radius " 5 + c.getRadius() ); 6 c = new Circle(); 7 } 8 } The Circle reference "c" needs to be initialized before using. (d) 1 public class ShowErrors { 2 public static void main (String[] args) { 3 C c = new C(5.0); 4 System.out.println(c.value); 5 } 6 } 7 8 class C { 9 int value = 2; 10 } Class "C" has no "double" constructor. 7) Which of the following statements are valid? a. int i = new int(30); // INVALID b. double d[] = new double[30]; // VALID c. char[] r = new char(1..30); // INVALID d. int i[] = (3, 4, 3, 2); // INVALID e. float f[] = {2.3, 4.5, 6.6}; // INVALID (Those literals are doubles) f. char[] c = new char(); // INVALID 8) Given an array of doubles, write Java statements to do the following: a. Assign the value 5.5 to the last element in the array. b. Display the sum of the first two elements. c. Write a loop that computes the sum of all elements in the array. d. Write a loop that finds the minimum element in the array. e. Randomly generate an index and display the element of this index in the array. f. Use an array initializer to create another array with the initial value 3.5, 5.5, 4.52, and 5.6. a. num[num.length - 1] = 5.5; b. System.out.println( num[0] + num[1] ); c. double sum = 0.0; for ( double n : num ) sum += n; OR: for ( int i = 0; i < num.length; ++i) sum += num[i]; d. double min = Double.NEGATIVE_INFINITY; for ( double n : num ) if ( n < min ) min = n; e. System.out.print( num[(int)(Math.random() * num.length)] ); f. double[] another = { 3.5, 5.5, 4.52, 5.6 }; 9) Use the following illustration as an example, show how to apply the binary search approach to a search first for key 10 and then key 12, in the list: [2, 4, 7, 10, 11, 45, 50, 59, 60, 66, 69, 70, 79]. key is 11 0 1 2 3 4 5 6 7 8 9 10 11 12 11<50 [ 2, 4, 7, 10, 11, 45, 50, 59, 60, 66, 69, 70, 79] low=0 mid=6 hi=12 11>7 [ 2, 4, 7, 10, 11, 45, 50, 59, 60, 66, 69, 70, 79] low=0 mid=2 hi=5 11=11 [ 2, 4, 7, 10, 11, 45, 50, 59, 60, 66, 69, 70, 79] low=3 hi=5 mid=4 (Note how binary search eliminates half of the list from further consideration after each comparison.) 0 1 2 3 4 5 6 7 8 9 10 11 12 10<50 [ 2, 4, 7, 10, 11, 45, 50, 59, 60, 66, 69, 70, 79] low=0 mid=6 hi=12 10>7 [ 2, 4, 7, 10, 11, 45, 50, 59, 60, 66, 69, 70, 79] low=0 mid=2 hi=5 10<11 [ 2, 4, 7, 10, 11, 45, 50, 59, 60, 66, 69, 70, 79] low=3 hi=5 mid=4 10=10 [ 2, 4, 7, 10, 11, 45, 50, 59, 60, 66, 69, 70, 79] low=3 mid=3 hi=3 0 1 2 3 4 5 6 7 8 9 10 11 12 12<50 [ 2, 4, 7, 10, 11, 45, 50, 59, 60, 66, 69, 70, 79] low=0 mid=6 hi=12 12>7 [ 2, 4, 7, 10, 11, 45, 50, 59, 60, 66, 69, 70, 79] low=0 mid=2 hi=5 12>11 [ 2, 4, 7, 10, 11, 45, 50, 59, 60, 66, 69, 70, 79] low=3 hi=5 mid=4 10!=45 [ 2, 4, 7, 10, 11, 45, 50, 59, 60, 66, 69, 70, 79] low=5 mid=5 hi=5 (Stop since low=5 and hi=4.) 10) What types of array can be sorted using the Java.util.Arrays.sort method? Does this sort method create a new array? Any primitive type except boolean, and any object type that implements the "Comparable" interface. No new array is created. 11) Which of the following statements are valid? a. int[][] r = new int[2]; // INVALID b. int[] x = new int[]; // INVALID c. int[][] y = new int [3][]; // VALID d. int[][] z = {{1, 2}}; // VALID e. int[][] m = {{1, 2}, {2, 3}}; // VALID f. int[][] n = {{1, 2}, {2, 3}, }; // VALID 12) Describe the difference between passing a parameter of a primitive type and passing a parameter of a reference type. Then show the output of the following program: ANSWER - For primitive value passed to a method, the variable remains unchanged. For references, any changes to the object made by method remain. 1: public class Test { 2: public static void main ( String[] args ) { 3: Count myCount = new Count(); 4: int times = 0; 5: for ( int i = 0; i < 100; i++ ) 6: increment( myCount, times ); 7: System.out.println("count is " + myCount.count); 8: System.out.println("times is " + times); 9: } 10: public static void increment ( Count c, int times ) { 11: c.count++; 12: times++; 13: } 14: } 15: 16: public class Count { 17: public int count; 18: public Count ( int c ) { 19: count = c; 20: } 21: public Count () { 22: count = 1; 23: } 24: } OUTPUT: count is 101 times is 0 13) What is wrong in the following code? 1 public class Test { 2 public static void main ( String [] args ) { 3 java.util.Date[] dates = new java.util.Date[10]; 4 System.out.println( dates[0] ); 5 System.out.println( dates[0].toString() ); 6 } 7 } Line 5 throws a NullPointerException error. 14) If a class contains only private data fields and no set methods, is the class immutable? No; a get method could return a reference to a mutable field. 15) If all the data fields in a class are private and primitive types, and the class doesn't contain any set methods, is the class immutable? Yes 16) What is wrong in the following code? 1: public class C { 2: private int p; 3: 4: public C() { 5: System.out.println( "C's no-arg constructor invoked" ); 6: this(0); 7: } 8: 9: public C ( int p ) { 10: p = p; 11: } 12: 13: public void setP ( int p ) { 14: p = p; 15: } 16: } ANSWER - line 6 needs to be the first line in constructor. 17) What is wrong in the following code? 1: public class Test { 2: private int id; 3: public void m1 () { 4: this.id = 45; 5: } 6: public void m2 () { 7: Test.id = 45; 8: } 9: } ANSWER - Test.id (line 7) is incorrect, since id is an instance member. end